没有合适的资源?快使用搜索试试~ 我知道了~
javaWeb实现事务1
资源详情
资源评论
资源推荐
通过使用 ThreadLocal 来绑定 connection 到每个线程特有的 map 中,在这个线
程中运行的 dao 都可以来获取这个 connection,所以就可以通过一个 connection
来做事务了,写一个 filter 过滤所有的请求,做成事务
@WebFilter("/*")
public class TranactionFilter extends HttpFilter{
@Override
public void doFilter(HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
Connection connection=null;
try {
//1.获取连接
connection=JDBCTools.getConnection();
//2.绑定连接
ConnectionContext.getSingleton().bind(connection);
//3.开启事务
connection.setAutoCommit(false);
//4.做事务
chain.doFilter(request, response);
//5.提交事务
connection.commit();
}catch (Exception e) {
//6.回滚事务
try {
connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally {
//7.解除绑定
ConnectionContext.getSingleton().remove();
//8.关闭连接
JDBCTools.close(connection);
}
}
}
高工-老罗
- 粉丝: 18
- 资源: 314
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0