当前位置:网站首页>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 .
边栏推荐
- mysql 进不去了怎么办
- [C language] named type and anonymous type
- The problem of large fluctuation of hx711 data
- About the declaration and definition of template functions [easy to understand]
- Wechat official account release reminder (wechat official account template message interface)
- datav漂亮数据屏制作体验
- cavans实现静态滚动弹幕
- Tradingview tutorial
- [Halcon vision] morphological corrosion
- 干货likeshop外卖点餐系统开源啦100%开源无加密
猜你喜欢

Okaleido ecological core equity Oka, all in fusion mining mode

3.1 leetcode daily question 6

Dry goods likeshop takeout order system is open source, 100% open source, no encryption

Application of.Net open source framework in industrial production

.net5wtm (asp.net core) PgSQL unpacking operation
![[Halcon vision] affine transformation](/img/f1/32284c71e78e6eea390fdb6058ba0f.png)
[Halcon vision] affine transformation
![[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间

videojs转canvas暂停、播放、切换视频

异常的概念与处理
![Structure of [Halcon vision] operator](/img/d9/e16ea52cea7897e3a1d61d83de472f.png)
Structure of [Halcon vision] operator
随机推荐
Some cutting-edge research work sharing of SAP ABAP NetWeaver containerization
Closure of go (cumulative sum)
异常的概念与处理
Interview questions and answers for the second company (2)
algorithm
.net5wtm (asp.net core) PgSQL unpacking operation
C语言回调函数
.net operation redis list list
Unit test, what is unit test and why is it so difficult to write a single test
少了个分号
数据分析入门 | kaggle泰坦尼克任务
Prevent XSS attacks
[Halcon vision] image filtering
Inheritance method of simplified constructor (I) - combined inheritance
粽子大战 —— 猜猜谁能赢
Tradingview tutorial
.NET操作Redis Hash对象
hx711 数据波动大的问题
Write to esp8266 burning brush firmware
Google与Pixar开发Draco支持USD格式 加速3D对象传输<转发>