当前位置:网站首页>@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";
}
边栏推荐
猜你喜欢

Promise

Top 10 Free 3D modeling software for beginners in 2022

【ITSM】什么是ITSM,IT部门为什么需要ITSM

C语言课设学生选修课程系统(大作业)

HCM Beginner (I) - Introduction

ManageEngine卓豪助您符合ISO 20000标准(四)

HCM Beginner (II) - information type
![[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

Mongodb: I. what is mongodb? Advantages and disadvantages of mongodb

SQL学习笔记九种连接2
随机推荐
Free trial of self-developed software noisecreater1.1
Forkjoin and stream flow test
Golang panic recover custom exception handling
[unity shader custom material panel part I]
【ManageEngine卓豪】移动终端管理解决方案,助力中州航空产业数字化转型
C language course set up library information management system (big homework)
【网络安全工具】USB控制软件有什么用
H5网页判断是否安装了某个APP,安装则跳转未安装则下载的方案总结
Teach you how to implement a deep learning framework
HCM Beginner (I) - Introduction
High order binary balanced tree
数据库产生死锁了请问一下有没有解决办法
请求模块(requests)
Movable mechanical wall clock
Async and await
json模块
[automatic operation and maintenance] what is the use of the automatic operation and maintenance platform
mysql约束学习笔记
[enterprise data security] upgrade backup strategy to ensure enterprise data security
Mysql 表分区创建方法