当前位置:网站首页>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 .
边栏推荐
- [Halcon vision] image filtering
- [Halcon vision] morphological corrosion
- [leetcode每日一题2021/8/31]1109. 航班预订统计【中等】差分数组
- Tradingview tutorial
- [leetcode每日一题2021/8/30]528. 按权重随机选择【中等】
- Datav beautiful data screen production experience
- Introduction to data analysis | kaggle Titanic mission
- 2022/07/25 ------ arrangement of strings
- .NET操作Redis Set无序集合
- 函数模板参数(函数参数在哪)
猜你喜欢

Introduction to data analysis | kaggle Titanic mission (I) - > data loading and preliminary observation
![[Halcon vision] image filtering](/img/7a/b95f8977f02fab644ef9fb205424e7.png)
[Halcon vision] image filtering
![[leetcode每日一题2021/2/13]448. 找到所有数组中消失的数字](/img/9b/624416fa6a408bf64ca5438273176b.png)
[leetcode每日一题2021/2/13]448. 找到所有数组中消失的数字
The software cannot be opened

数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察

.NET 开源框架在工业生产中的应用

Deduct daily question 838 of a certain day
![[Halcon vision] software programming ideas](/img/9b/a27338689ee4598dac88f6e5d92053.png)
[Halcon vision] software programming ideas
![[leetcode每日一题2021/2/14]765. 情侣牵手](/img/be/8639a05c733638bf0b3fdeb11abccf.png)
[leetcode每日一题2021/2/14]765. 情侣牵手

议程速递 | 7月27日分论坛议程一览
随机推荐
2022/07/25------字符串的排列
2022/07/25 ------ arrangement of strings
Navicat15 MySQL (centos7) connected to local virtual machine
What is wrong about the description of function templates (how to solve link format errors)
string null转空字符串(空字符串是什么意思)
Introduction to data analysis | kaggle Titanic mission
3.1 leetcode daily question 6
12 复制对象时勿忘其每一个成分
Write to esp8266 burning brush firmware
函数模板参数(函数参数在哪)
同步方法中不使用asyncTask<T> 修饰和await获取异步返回值(同步方法中调用异步方法)
Interview questions and answers for the second company (2)
移动端双指缩放事件(原生),e.originalEvent.touches
事务的传播性propagation
.net operation redis string string
13 以对象管理资源
Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)
Google与Pixar开发Draco支持USD格式 加速3D对象传输<转发>
Redis docker instance and data structure
json_object_put: Assertion `jso->_ref_count > 0‘ failed.Aborted (core dumped)