当前位置: 首页 > news >正文

【Java】Spring的声明事务在多线程场景中失效问题。

大家都知道Spring的声明式事务在多线程当中会失效,来看一下如下案例。
按照如下方式,b()方法抛出异常,由于父子线程导致事务失效,a()会成功插入,但是b()不会。
因此成功插入一条数据,事务失效。

@Component
public class UserServiceImpl implements UserService{@Transactionalpublic void a(){jdbcTemplate.execute("insert into `user`(`age`,`name`,`city`) values(18,'张三','北京')");UserService userService = (UserService)AopContext.currentProxy();Thread thread = new Thread(()->{userService.b();})}@Transactionalpublic void b(){jdbcTemplate.execute("insert into `user`(`age`,`name`,`city`) values(19,'张三','北京')");throw new RuntimeException();}}

这里就需要了解一下嵌套方法事务的传播行为是怎么实现的?
在这里插入图片描述
如图所示,如果b()方法是在子线程当中的,因为ThreadLocal不是同一个因此子线程又创建了一个事务。由于是各用各的事务所以事务b就会回滚,而事务a的数据就会插入成功。
要保证这种父子线程中的事务传播,则在创建子线程后把父线程中的事务取出来再设置进去。
进行如下改造后保证两个线程使用同一个事务。
在这里插入图片描述
那么接下来的问题就是如何获取外层的connection以及如何设置到子线程的ThreadLocal中。
这里就需要看一下源码了
需要看这个类org.springframework.jdbc.datasource.DataSourceTransactionManager找到doBegin()方法

@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;Connection con = null;try {if (!txObject.hasConnectionHolder() ||txObject.getConnectionHolder().isSynchronizedWithTransaction()) {Connection newCon = obtainDataSource().getConnection();if (logger.isDebugEnabled()) {logger.debug("Acquired Connection [" + newCon + "] for JDBC transaction");}txObject.setConnectionHolder(new ConnectionHolder(newCon), true);}txObject.getConnectionHolder().setSynchronizedWithTransaction(true);con = txObject.getConnectionHolder().getConnection();Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);txObject.setPreviousIsolationLevel(previousIsolationLevel);txObject.setReadOnly(definition.isReadOnly());if (con.getAutoCommit()) {txObject.setMustRestoreAutoCommit(true);if (logger.isDebugEnabled()) {logger.debug("Switching JDBC Connection [" + con + "] to manual commit");}// 这里开启事务con.setAutoCommit(false);}prepareTransactionalConnection(con, definition);txObject.getConnectionHolder().setTransactionActive(true);int timeout = determineTimeout(definition);if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {txObject.getConnectionHolder().setTimeoutInSeconds(timeout);}// 这里将连接存入到ThreadLocalif (txObject.isNewConnectionHolder()) {TransactionSynchronizationManager.bindResource(obtainDataSource(), txObject.getConnectionHolder());}}catch (Throwable ex) {if (txObject.isNewConnectionHolder()) {DataSourceUtils.releaseConnection(con, obtainDataSource());txObject.setConnectionHolder(null, false);}throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", ex);}
}

最终改造

@Component
public class UserServiceImpl implements UserService{@Transactionalpublic void a(){ConnectionHolder connectionHolder = TransactionSynchronizationManager.getResource(dataSource);jdbcTemplate.execute("insert into `user`(`age`,`name`,`city`) values(18,'张三','北京')");UserService userService = (UserService)AopContext.currentProxy();Thread thread = new Thread(()->{// 绑定主线程的connection到子线程TransactionSynchronizationManager.bindResource(dataSource,connectionHolder);userService.b();})}@Transactionalpublic void b(){jdbcTemplate.execute("insert into `user`(`age`,`name`,`city`) values(19,'张三','北京')");throw new RuntimeException();}}
http://www.dtcms.com/a/193540.html

相关文章:

  • 欧拉计划 Project Euler 73(分数有范围计数)题解
  • 第七节第三部分:从JDK8开始接口新增的方法、接口的多继承、注意事项
  • 产品方法论与 AI Agent 技术的深度融合:从决策智能到价值创造
  • 串口共用错误排查指南(2025年5月15日 09:25)
  • AI大模型应用:17个实用场景解锁未来
  • 全国青少年信息素养大赛 Python编程挑战赛初赛 内部集训模拟试卷九及详细答案解析
  • 特种设备事故背后,叉车智能监控系统如何筑牢安全防线
  • 深度学习中--模型调试与可视化
  • 【电子通识】热敏纸的静态发色性能和动态发色性能测试方法
  • 考研408《计算机组成原理》复习笔记,第二章(2)数值数据的表示(浮点数篇)
  • Cursor vs VS Code vs Zed
  • leetcode0621. 任务调度器-medium
  • Linux记录
  • 串行接口与并行接口
  • Scapy库交互式运行
  • 【steganalysis】Enhancing practicality and efficiency of deepfake detection
  • Java随机生成邀请码 (包含字母大小写+数字)
  • 代码随想录 算法训练 Day2:数组
  • 缺乏需求优先级划分时,如何合理分配资源?
  • Python网络请求利器:urllib库深度解析
  • c++ std::deque
  • Linux/Centos7离线安装并配置MySQL 5.7
  • 微服务概述
  • Loki的部署搭建
  • OpenCV 图像直方图:从原理剖析到实战应用
  • C++多态实现的必要条件剖析
  • unordered_set和unordered_map
  • AtCoder 第405场初级竞赛 A~E题解
  • 从理论到实战:模糊逻辑算法的深度解析与应用实践
  • 全面掌握JSR303校验:从入门到实战