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

QRcode二维码(C语言)遇到的问题

The problem of large fluctuation of hx711 data

【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening

vscode上使用anaconda(已经配置好环境)

hx711 数据波动大的问题
![Structure of [Halcon vision] operator](/img/d9/e16ea52cea7897e3a1d61d83de472f.png)
Structure of [Halcon vision] operator
![[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间

PLC overview

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

STM32 Alibaba cloud mqtt esp8266 at command
随机推荐
.net operation redis sorted set ordered set
.NET操作Redis Set无序集合
Dynamically determine file types through links
Dry goods likeshop takeout order system is open source, 100% open source, no encryption
Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
议程速递 | 7月27日分论坛议程一览
[leetcode每日一题2021/4/29]403. 青蛙过河
Redis特殊数据类型使用场景
About the declaration and definition of template functions [easy to understand]
canvas上传图片base64-有裁剪功能-Jcrop.js
js 获得当前时间,时间与时间戳的转换
将json文件中数组转换为struct
Agenda express | list of sub forum agenda on July 27
Tradingview tutorial
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
Modelsim installation tutorial (application not installed)
上传图片获取宽高
第8期:云原生—— 大学生职场小白该如何学
Write to esp8266 burning brush firmware
[Halcon vision] image gray change