当前位置:网站首页>@Propagation property of transactional requires_ New in-depth understanding
@Propagation property of transactional requires_ New in-depth understanding
2022-07-01 06:29:00 【ydfind】
Premise
spring boot project ;sql server;druid Connection pool ;
Conclusion
requires_new Propagation properties , A new transaction will be created ( New connection ), Hanging up old business means , In fact, it is because of the use of ThreadLocal To preserve DB Link information , Need to put ThreadLocal Only when it is processed into the information of the current transaction .
So two transactions There is also a competitive relationship of resources , Here is an example , Outer Affairs A REQUIRED, A new record will be inserted into test_a; Internal affairs B Will inquire about test_a All records , because sql server Default read committed Isolation level , So we need to wait until A After submission , To find out , But the business A Has been suspended , need B You can continue to run after execution .
That is, there is such a situation , Business A need B You can continue to run after execution , And the business B Waiting for business A Submit to continue , So it's locked !!!
If we put things B, namely BService Methods isolation = Isolation.READ_UNCOMMITTED Set the isolation level lower , There will be no deadlock !
Code examples studied
Make a use case first , stay springboot Inside the project
@RestController
@RequestMapping(value = "/test/transational/requiresnew", produces = "application/json;charset=UTF-8")
public class TransactionalRequiresNewController {
@Autowired
private AService aService;
/**
* Outer layer required, Inner layer requires_new
* Insert new record in outer layer key1;
* Execute the inner layer ,selectAll, Insert new record key2;
* Insert a new record twice in the outer layer key3, Trigger error rollback ;
*
* expect :
* 1. Will fall into a dead cycle , Because the insertion of the outer layer was not submitted ; Inner layer selectAll Been waiting ;
* @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() {
// Will deadlock here . Previous transaction A Insert new data , But did not submit ,sql server Read submitted by default , So here requires_new New transaction B Been waiting ;
List<String> strings = keywordDao.selectAllKeywordInA();
return "ok";
}
边栏推荐
猜你喜欢

C语言课设物业费管理系统(大作业)

C语言课设工资管理系统(大作业)

数据库产生死锁了请问一下有没有解决办法

How does the port scanning tool help enterprises?

How did ManageEngine Zhuohao achieve the goal of being selected into Gartner Magic Quadrant for four consecutive years?

Top 10 Free 3D modeling software for beginners in 2022

自开发软件NoiseCreater1.1版本免费试用
![[network security tool] what is the use of USB control software](/img/cc/20fc1f35c139c52c5922727368b835.png)
[network security tool] what is the use of USB control software

【KV260】利用XADC生成芯片温度曲线图
![[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
随机推荐
【#Unity Shader#Amplify Shader Editor(ASE)_第九篇】
C语言课设学生信息管理系统(大作业)
[summary of knowledge points] chi square distribution, t distribution, F distribution
Promise
地宮取寶(記憶化深搜)
1034 Head of a Gang
Record MySQL troubleshooting caused by disk sector damage
Dongle data collection
HCM Beginner (IV) - time
高阶-二叉搜索树详解
C语言课设学生考勤系统(大作业)
Code power is full of quantitative learning | how to find a suitable financial announcement in the financial report
子类调用父类的同名方法和属性
证券类开户有什么影响 开户安全吗
【#Unity Shader#自定义材质面板_第二篇】
【Unity Shader 消融效果_案例分享】
C语言课设学生选修课程系统(大作业)
图片服务器项目测试
【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用
H5网页判断是否安装了某个APP,安装则跳转未安装则下载的方案总结