zoukankan      html  css  js  c++  java
  • (八)Filter&ThreadLocal实现处理事务

    ConnectionContext.java

    package com.aff.bookstore.web;
    
    import java.sql.Connection;
    
    public class ConnectionContext {
        private static ConnectionContext insetance = new ConnectionContext();
    
        public static ConnectionContext getInsetance() {
            return insetance;
        }
    
        private ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();
    
        public void bind(Connection connection) {
            connectionThreadLocal.set(connection);
        }
    
        public Connection get() {
            return connectionThreadLocal.get();
        }
    
        public void remove() {
            connectionThreadLocal.remove();
        }
    }

    TranactionFilter.java

    package com.aff.bookstore.filter;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.aff.bookstore.db.JDBCUtils;
    import com.aff.bookstore.web.ConnectionContext;
    
    @WebFilter("/*")
    public class TranactionFilter implements Filter {
    
        public TranactionFilter() {
        }
    
        public void destroy() {
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            Connection connection = null;
    
            try {
    
                // 1.获取连接
                connection = JDBCUtils.getConnection();
    
                // 2.开启事务
                connection.setAutoCommit(false);
    
                // 3.利用ThreadLocal 把连接和当前线程绑定
                ConnectionContext.getInsetance().bind(connection);
    
                // 4.把请求 转给,目标Servlet
                chain.doFilter(request, response);
    
                // 5.提交事务
                connection.commit();
    
            } catch (Exception e) {
                e.printStackTrace();
    
                // 6.回滚事务
                try {
                    connection.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
                HttpServletResponse resp = (HttpServletResponse) response;
                HttpServletRequest req = (HttpServletRequest) request;
                resp.sendRedirect(req.getContextPath()+"/errror-1.jsp");
    
            } finally {
                // 7.解除连接
                ConnectionContext.getInsetance().remove();
    
                // 8.关闭连接
                JDBCUtils.release(connection);
            }
        }
    
        public void init(FilterConfig fConfig) throws ServletException {
        }
    
    }
    All that work will definitely pay off
  • 相关阅读:
    新建MDK工程
    winform如何打开电脑自带小软件
    Winform界面适应不同分辨率
    c#如何为pictureBox控件写单击事件
    winform如何获取记事本内容显示在listBox控件中
    winform如何单击X按钮弹出对话框
    win10系统如何使用自带的备份功能
    模拟iis账号权限
    (转发)在ASP.NET MVC中以post方式传递数组参数的示例
    .net开发windows服务小结 (转发)
  • 原文地址:https://www.cnblogs.com/afangfang/p/12935338.html
Copyright © 2011-2022 走看看