使用ThreadLocal实现对JDBC的事务处理

一、准备工作
在应用ThreadLocal对JDBC的事务处理前,可以先在项目中导入数据库连接池的jar包,然后创建c3p0-config.xml并配置。


二、创建使用ThreadLocal的事务处理工具类

public class JDBCUtils3 {

    private static DataSource dataSource=null;
    private static ThreadLocal<Connection> threadLocal=new ThreadLocal<>();//使用ThreadLocal存储当前线程中的Connection对象

    static{
        dataSource=new ComboPooledDataSource();//创建C3P0数据库连接池

    }

    public static Connection getConnection()  throws Exception{

         //从当前线程中获取Connection
        Connection conn = threadLocal.get();
        if(conn==null){
            //从数据源中获取数据库连接
            conn = getDataSource().getConnection();
            //将conn绑定到当前线程
            threadLocal.set(conn);
        }
        return conn;


    }
    //开启事务的操作
    public static void startTransaction(){

        Connection connection=threadLocal.get();
        if(null == connection){
            try {
                connection=getDataSource().getConnection();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
            threadLocal.set(connection); //把数据库连接绑定到当前线程上

        }

        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }               
    }

    //事务提交
    public static void commit(){

        Connection connection = threadLocal.get();
        if(null != connection){
            try {
                connection.commit();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    //事务回滚
    public static void rollback(){
        Connection connection = threadLocal.get();
        if(null != connection){

            try {
                connection.rollback();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    //关闭的方法
    public static void close(){
        try{
            //从当前线程中获取Connection
            Connection conn = threadLocal.get();
            if(conn!=null){             
                threadLocal.remove(); //解除当前线程上绑定的连接
                conn.close();
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
      //从数据源中获取数据库连接
    public static DataSource getDataSource(){
        return dataSource;
    }   
}

三、业务层使用工具类实现事务处理

public void buyFund(String uname, Integer mid, Integer buynum) {
        User user = fundDaoImpl.getUserByName(uname);
        Integer uid = user.getUid();        
        try {
            JDBCUtils3.startTransaction();
            fundDaoImpl.buyFundOnUser(uid,mid,buynum);

            fundDaoImpl.buyFundOnMoney(mid,buynum);
            JDBCUtils3.commit();            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            JDBCUtils3.rollback();

        }finally {
            JDBCUtils3.close();
        }

    }

猜你喜欢

转载自blog.csdn.net/chenbingbing111/article/details/80752497