当前位置:网站首页>Analysis of the transaction problem of chained method call
Analysis of the transaction problem of chained method call
2022-07-26 10:35:00 【Fan Xuebo】
Recently, I have done a distributed project , Of course, there is no design for distributed transaction processing , A logic line is too long , Methods and methods , So the problem of affairs between them is exposed
I'm dead , Code directly
@Service
public class TransactionalServiceImpl implements TransactionalService {
@Autowired
private CgpMngUserbraService cgpMngUserbraService;
@Transactional
@Override
public void method() {
CgpMngUserbra cgpMngUserbra = cgpMngUserbraService.selectUserBraRules(0, 0);
if (null == cgpMngUserbra) {
cgpMngUserbraService.insertUserBraRules(0, 0, "method");
}
method2();
}
// @Transactional// The transaction will not take effect here , The reason can be found in the last blog
// Portal ---> https://blog.csdn.net/fanxb92/article/details/81296005
public void method2() {
CgpMngUserbra cgpMngUserbra = cgpMngUserbraService.selectUserBraRules(0, 0);
if (null == cgpMngUserbra) {
cgpMngUserbraService.insertUserBraRules(0, 0, "method2");
}
TransactionalServiceImpl transactionalService = (TransactionalServiceImpl) AopContext.currentProxy();
transactionalService.method3();
}
@Transactional
// @Transactional(propagation = Propagation.REQUIRES_NEW)
// @Transactional(propagation = Propagation.NESTED)
public void method3() {
CgpMngUserbra cgpMngUserbra = cgpMngUserbraService.selectUserBraRules(0, 0);
if (null == cgpMngUserbra) {
cgpMngUserbraService.insertUserBraRules(0, 0, "method3");
}
}
}First, let's introduce the test logic :
1、method,method2,method3 The three methods will save the same data in a table , And chain call .
2、method2 Adding transactions does not take effect ,method2 Medium transaction and method Consistent in , Refer to the blog link in the code .
3、 Here is the test cascading transaction ,method3 The above three transaction processing methods
- The first one is @Transactional,method3 The transaction in is consistent with the previous (method In the transaction generated by annotation on method ), All done , A piece of data in the database (createdBy = 'method'). It must be all implemented , Break in the middle ,method For inserting into the database , Because the method is not over , not commit.
- The second kind @Transactional(propagation = Propagation.REQUIRES_NEW),method3 Opened a new transaction , Will suspend the existing transaction ,
Again , After all the methods are executed, two pieces of data in the database , The screenshot of the database record is as follows :
so , yes method The storage time of medium data is before , analysis ===>
!!!!!!!!! It's very important here , It may not be concise and accurate , Experience carefully !!!!!!!!!!!!!!!!!!
method In the implementation of insert( The first piece of data will be inserted createdBy = 'method'), When the method is not executed, it is not commit, There is no insert record in the database , But in the cache .
method2 Methods are nested in method In the method , In the same business ,method2 The record will be queried in the method , So no more data will be inserted .
method3 Methods are nested in method2 In the method , Opened a new transaction , Will not find method Data in method transactions , So it will execute insert( Insert the second data createdBy = 'method3').
Here are the details !!!>>>>>>method3 Method to start a new transaction , After the method is executed, it will be submitted ,
Can be found in method3 It's over, but method Where it's not over, break , You will see that the database has records (createdBy = 'method3').
Let go of the breakpoint , All done , The database has two records , As shown in the screenshot above .
It can be seen that it is possible to cascade and start new transactions , But you must call external methods to take effect , Or use AopContext.currentProxy().
- The third kind of @Transactional(propagation = Propagation.NESTED)
Throw an exception JpaDialect does not support savepoints - check your JPA provider's capabilities
public JpaTransactionManager() {setNestedTransactionAllowed(true);}
Only later did I know Hibernate Nor does it support Nested Transaction, The test doesn't work , Only use jdbc Business , Use JdbcTemplate.
ok , Let's talk so much first . Thank you for reading .
边栏推荐
猜你喜欢
随机推荐
畅听,网文流量竞争的下一站?
结构体操作报错:Segmentation fault (core dumped)
[Halcon vision] threshold segmentation
数据分析入门 | kaggle泰坦尼克任务
函数模板与同名的非模板函数不可以重载(重载的定义)
第6期:大学生应该选择哪种主流编程语言
Inheritance method of simplified constructor (II) - class inheritance in ES6
C语言回调函数
.net operation redis hash object
PTA class a 1002
Unit test, what is unit test and why is it so difficult to write a single test
议程速递 | 7月27日分论坛议程一览
Problems encountered in QRcode QR code (C language)
modelsim 安装教程(应用未安装)
干货likeshop外卖点餐系统开源啦100%开源无加密
[socket] the three handshakes are completed in listen, and accept only takes out one connection from the queue that completes the connection
[leetcode每日一题2021/4/23]368. 最大整除子集
构造器、方法重载、对象数组和static
[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数
Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)







