当前位置:网站首页>@Transactional的传播属性REQUIRES_NEW深入理解
@Transactional的传播属性REQUIRES_NEW深入理解
2022-07-01 06:25:00 【ydfind】
前提
spring boot项目;sql server;druid连接池;
结论
requires_new传播属性,会新建一个事务(即新连接),挂起旧事务的意思,其实是因为用到了ThreadLocal来保存DB链接信息,需要将ThreadLocal处理成当前事务的信息才行。
因此两个事务 也是有资源的竞争关系,下面举个例子来说明,外层事务A REQUIRED, 会插入一条新记录到test_a; 内层事务B会查询 test_a的所有记录,因为sql server默认读已提交 隔离级别,故需要等到A提交后,才能查询到,但事务A已经被挂起,需要B执行完才能继续运行。
即出现这样的情况,事务A需要B执行完才能继续运行, 而事务B等着事务A提交才能继续运行,故死锁了!!!
假如我们把事务B,即BService的方法isolation = Isolation.READ_UNCOMMITTED隔离级别设置低一些,就不会产生死锁了!
研究的代码例子
先做个用例, 在springboot项目里面
@RestController
@RequestMapping(value = "/test/transational/requiresnew", produces = "application/json;charset=UTF-8")
public class TransactionalRequiresNewController {
@Autowired
private AService aService;
/**
* 外层required,内层requires_new
* 外层插入新记录key1;
* 执行内层,selectAll,插入新纪录key2;
* 外层两次插入新纪录key3,触发报错回滚;
*
* 期望:
* 1.会陷入死循环,因为外层的插入未提交;内层selectAll一直在等待;
* @return
*/
@GetMapping("/deadLock")
public String requiresNewDeadLock() {
try {
String s = aService.requiresNewDeadLock();
System.out.println("requiresNewDeadLock: " + s);
return s;
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}
}
// AService
@Autowired
private BService bService;
@Transactional(propagation = Propagation.REQUIRED)
@Override
public String requiresNewDeadLock() {
keywordDao.insertToA("requiresNewDeadLock1");
return bService.requiresNewDeadLock();
}
// BService
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public String requiresNewDeadLock() {
// 会死锁在这里。前一个事务A插入新数据,但未提交,sql server默认是读已提交,故这里requires_new新建的事务B一直在等待;
List<String> strings = keywordDao.selectAllKeywordInA();
return "ok";
}
边栏推荐
- HCM Beginner (I) - Introduction
- lxml模块(数据提取)
- 【ManageEngine卓豪】局域网监控的作用
- [ManageEngine Zhuohao] mobile terminal management solution, helping the digital transformation of Zhongzhou aviation industry
- DML statement in SQL (data operation language)
- Minio error correction code, construction and startup of distributed Minio cluster
- 存储函数学习笔记
- [unity shader amplify shader editor (ASE) Chapter 9]
- C language course set up property fee management system (big work)
- Detailed steps for installing redis on Windows system
猜你喜欢
![[unity shader amplify shader editor (ASE) Chapter 9]](/img/f5/f0f6786406e149187e71c8e12cde0d.png)
[unity shader amplify shader editor (ASE) Chapter 9]

端口扫描工具对企业有什么帮助?

HCM Beginner (IV) - time

【Unity Shader 描边效果_案例分享第一篇】
![[postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map](/img/c0/299a406efea51f24b1701b66adc1e3.png)
[postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map

自开发软件NoiseCreater1.1版本免费试用

FPGA - 7 Series FPGA internal structure clocking-01-clock Architecture Overview

Mongodb: I. what is mongodb? Advantages and disadvantages of mongodb
![[automatic operation and maintenance] what is the use of the automatic operation and maintenance platform](/img/14/756d566744d6e4a988a284c5b30130.png)
[automatic operation and maintenance] what is the use of the automatic operation and maintenance platform

C language course set up library information management system (big homework)
随机推荐
交换机配置软件具有的作用
FPGA - 7 Series FPGA internal structure clocking-01-clock Architecture Overview
SQL语句
JSON module
【自动化运维】自动化运维平台有什么用
[unity shader custom material panel part I]
SystemVerilog learning-08-random constraints and thread control
启牛学堂合作的证券公司是哪家?开户安全吗?
存储过程学习笔记
伪装请求头库: anti-useragent
[enterprise data security] upgrade backup strategy to ensure enterprise data security
C language course set up salary management system (big homework)
[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
Although pycharm is marked with red in the run-time search path, it does not affect the execution of the program
端口扫描工具是什么?端口扫描工具有什么用
webapck打包原理--启动过程分析
【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用
idea 好用插件汇总!!!
To sort out the anomaly detection methods, just read this article!
SystemVerilog learning-06-class encapsulation