ThreadLocal 在jdbc事务的应用

ThreadLocal

优势:

  1. 传递数据:在需要的地方获取数据,避免参数传递带来的代码耦合问题
  2. 线程隔离:在各线程的数据相互隔离又能并发,避免了同步方式带来的性能损失

基本使用

public class Test {
    ThreadLocal<String> t1 = new ThreadLocal<>();
    public String getContent() {
        return t1.get();
    }
    public void setContent(String content) {
        t1.set(content);
    }
}
public class AccountService {
    public boolean transfer(String out, String in, int money) {
        AccountDao dao = new AccountDao();
        Connection conn = null;
        try {
            synchronized (AccountService.class) {
                conn = JdbcUtil.getConnection();
                // 关闭自动提交,开启事务
                conn.setAutoCommit(false);
                /* 
                事务使用注意点:
                	1. 保证两次数据库操作的conn对象一致
                	2. 线程隔离
                常规解决方案:
                	1. 传参:service层的conn传到dao层
                	2. 加锁(别的线程不能在此时getConn,否则可能导致当前线程的conn发生改变)
                常规解决方案的弊端:
                	1. 提高代码耦合度
                	2. 降低程序性能
                */
                dao.out(out, money, conn);
                dao.in(in, money, conn);
                conn.commit();
                conn.close();
                return true;
            }
        } catch (Exception e) {
            conn.rollback();
            conn.close();
            return false;
        }
    }
}
/**
 * 1. 方法添加参数 conn,不能从连接池获取conn
 * 2. dao层不能释放连接
 */
public class AccountDao {
    // 转出
    public void out(String out, int money, Connection conn) {
        String sql = "...";
        PreparedStatement pstm = conn.prepareStatement(sql);
        // ...
        pstm.executeUpdate();
    }
    // 转入
    public void in(String in, int money, Connection conn) {
        String sql = "...";
        PreparedStatement pstm = conn.prepareStatement(sql);
        // ...
        pstm.executeUpdate();
    }
}
改进
public class JdbcUtils {
    ThreadLocal<Connection> threadLocal = new ThreadLocal();
    /*
    	原来:直接从数据池中获取连接
    	现在:1. 直接获取当前线程绑定的conn
    		 2. 如果连接对象是空的再去连接池中获取conn
    		 	在将此conn跟当前对象绑定
    */
    public static Connection getConnection () {
        Connection conn = threadLocal.get();
        if (conn == null) {
            conn = database.getConnection();
            threadLocal.set(conn);
        }
        return conn;
    }
    public static void commitAndClose() {
        conn.commit();
        // 解绑conn
        threadLocal.remove();
        conn.close();
    }
    public static void rollbackAndClose() {
        conn.rollback();
        // 解绑conn
        threadLocal.remove();
        conn.close();
    }
}
public class AccountService {
    public boolean transfer(String out, String in, int money) {
        AccountDao dao = new AccountDao();
        Connection conn = null;
        try {
            synchronized (AccountService.class) {
                // 从数据库获取conn并存入当前线程
                // 其他线程到来会从连接池获取新conn
                conn = JdbcUtils.getConnection();
                conn.setAutoCommit(false);
                dao.out(out, money, conn);
                dao.in(in, money, conn);
                JdbcUtils.commitAndClose();
                return true;
            }
        } catch (Exception e) {
            JdbcUtils.rollbackAndClose();
            return false;
        }
    }
}

public class AccountDao {
    // 转出
    public void out(String out, int money) {
        // 从当前线程获取conn
        Connection conn = JdbcUtils.getConnection();
        String sql = "...";
        PreparedStatement pstm = conn.prepareStatement(sql);
        // ...
        pstm.executeUpdate();
    }
    // 转入
    public void in(String in, int money) {
        // 从当前线程获取conn
        Connection conn = JdbcUtils.getConnection();
        String sql = "...";
        PreparedStatement pstm = conn.prepareStatement(sql);
        // ...
        pstm.executeUpdate();
    }
}
ThreadLocal源码
public class ThreadLocal {
    public void set(T value) {
        ThreadLocalMap map = 
            Thread.currentThread().threadLocals;
        if (map != null) {
            map.set(this, value);
        }
    }
    public T get() {
        ThreadLocalMap map = 
            Thread.currentThread().threadLocals;
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null) {
                T result = e.value;
                return result;
            }
        }
    }
    public void remove() {
        ThreadLocalMap map = 
            Thread.currentThread().threadLocals;
        if (map != null) {
            m.remove(this);
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

beOkWithAnything

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值