首页 > 编程语言 >JavaWeb-----进行事务处理的Filter

JavaWeb-----进行事务处理的Filter

时间:2022-10-01 11:00:06浏览次数:69  
标签:void 事务处理 Filter threadLocal SQLException static ConnUtil ----- public

详细资源:https://heavy_code_industry.gitee.io/code_heavy_industry/pro001-javaweb/lecture/

详细博客:https://blog.csdn.net/jsdoulaoula/article/details/125648785?spm=1001.2014.3001.5502

 

 

 《事务管理》

 

 

 

 为了在一次事务上使用同一个Connection,那么我们必须将这个Connection用一个容器装到,到了要使用的时候在拿出来

 我们可以用到ThreadLocal这个类:

 

 

 

每一个线程维护一个Map容器,这个Map容器中以key-value的形式,key为当前new 出的ThreadLocal

value为想要在这个线程中的这个ThreadLcoal中要公用的值

 

 将获取连接这个操作封装到一个ConnUtil类中:

 1 //保证我们在进行一次事务处理时是拿到的同一个Connection
 2 public class ConnUtil {
 3     private static ThreadLocal<Connection> threadLocal;
 4     private static DataSource dataSource;
 5 
 6     static {
 7         threadLocal = new ThreadLocal<>();
 8         Properties properties = new Properties();
 9         try {
10             FileInputStream fis = new FileInputStream("D:\\JavaWeb_Code\\javaWeb_demo02\\src\\com\\cilinmengye\\Utils\\druider.properties");
11             properties.load(fis);
12             dataSource = DruidDataSourceFactory.createDataSource(properties);
13         } catch (Exception e) {
14             throw new ConnUtilError("在组件ConnUtil中出错!");
15         }
16     }
17 
18     public static Connection getConn() throws SQLException {
19         if (threadLocal.get() == null) {
20             threadLocal.set(dataSource.getConnection());
21         }
22         return threadLocal.get();
23     }
24 
25     public static void closeConn() throws SQLException {
26         if (threadLocal.get() == null) return;
27         threadLocal.get().close();
28         threadLocal.set(null);
29     }
30 }

 将对事务的操作封装:

 1 //这个类主要是进行事务的回滚,提交
 2 public class TranManager {
 3     //要进行事务的操作,就要得到Connection,所以我们还要一个连接的工具类进行帮助
 4     public static void start() throws SQLException {
 5         ConnUtil.getConn().setAutoCommit(false);
 6     }
 7 
 8     public static void commit() throws SQLException {
 9         ConnUtil.getConn().commit();
10         ConnUtil.closeConn();
11     }
12 
13     public static void rollback() throws SQLException {
14         ConnUtil.getConn().rollback();
15         ConnUtil.closeConn();
16     }
17 }

在Filter中进行操作:

 1 //拦截全部的.do请求
 2 @WebFilter("*.do")
 3 public class OpenSessionInViewFilter implements Filter {
 4     @Override
 5     public void init(FilterConfig filterConfig) throws ServletException {
 6 
 7     }
 8 
 9     @Override
10     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
11         //尝试进行放行,如果出现问题会通过catch继续事务的回滚
12         try {
13             TranManager.start();
14             System.out.println("开启事务~~~~~~");
15             //放行的标识:
16             filterChain.doFilter(servletRequest, servletResponse);
17             //放行后进行事务的操作,如果整个过程没有问题则提交事务
18             TranManager.commit();
19             System.out.println("提交事务~~~~~~");
20         } catch (Exception e) {
21             e.printStackTrace();
22             try {
23                 TranManager.rollback();
24                 System.out.println("回滚事务~~~~~~");
25             } catch (SQLException throwables) {
26                 throwables.printStackTrace();
27             }
28         }
29     }
30 
31     @Override
32     public void destroy() {
33 
34     }
35 }

 

标签:void,事务处理,Filter,threadLocal,SQLException,static,ConnUtil,-----,public
From: https://www.cnblogs.com/cilinmengye/p/16746903.html

相关文章