当前位置:网站首页>Transaction rollback exception
Transaction rollback exception
2022-07-05 15:58:00 【Ant HJK】
org.springframework.transaction.UnexpectedRollbackException
Spring The transaction propagation mechanisms are summarized as follows :
- PROPAGATION_REQUIRED: If there is no current transaction , Just create a new transaction , If a transaction already exists , Just join in this business . The default policy
- PROPAGATION_SUPPORTS: Support current transaction , If there is no current transaction , Just in a non transactional way .
- PROPAGATION_MANDATORY: Use the current transaction , If there is no current transaction , Throw an exception .
- PROPAGATION_REQUIRES_NEW: New transaction , If there are currently transactions , Suspend current transaction .
- PROPAGATION_NOT_SUPPORTED: Perform operations in a non transactional way , If there are currently transactions , Suspend the current transaction .
- PROPAGATION_NEVER: To execute in a non transactional manner , If there are currently transactions , Throw an exception .
- PROPAGATION_NESTED: If there are currently transactions , Within the nested transaction . If there is no current transaction , Enforcement and PROPAGATION_REQUIRED Similar operation .
nested The sub exception of is thrown If you are catch External insertion succeeded
nested The sub exception of is thrown If not catch External insert failed
nested An exception is thrown outside nest Internal insertion succeeded nest Will roll back
spring The default propagation behavior of transactions :
@Transactional be equal to @Transactional(propagation=Propagation.REQUIRED)
Description of the abnormal scenario :
In the use of Spring In business , In a business A There is another transaction in B( That is, there are nested transactions ), When a transaction B When something goes wrong , Will be abnormal catch Post transaction B Rollback operation will be performed , If the abnormality is eaten directly at this time ( It's business A There is no way to know that an exception has occurred ), The transaction A The above exception will be thrown .
for example :
serviceA Affairs @Transactional
serviceB Affairs @Transactional
serviceA.methodA()
{
doSomethingA();
try {
serviceB.methodB{}; // There are exceptions marked as rollback , doSetRollbackOnly(status);
}
catch {
// Catch exception go to commit when , Because it has been marked for rollback , Roll back and throw a new exception
}
doSomethingB();
}
reason :
because methodB The propagation property of is set to PROPAGATION_REQUIRED,PROPAGATION_REQUIRED It means , There's business at the moment , Use the current transaction , If there is no transaction at present, create a transaction . because methodA The propagation property of is also PROPAGATION_REQUIRED, therefore methodA Will create a transaction , then methodB And methodA Use the same transaction ,methodB After exception , Rollback the current transaction flag bit , Because in methodA I did it. trycatch Handle , The program didn't stop but went on , When a transaction commit when ,check state , Find out , need Transaction rollback , Therefore, unpredictable transaction exceptions will occur : Because the transaction is rolled back by the flag bit , So transaction rollback .
in other words :methodA And methodB Share a transaction ,methodB Mark the transaction as rollback ,methodA in commit This business , then , The transaction has been marked as rolled back (methodB logo ) Exception information for .
Solution
situation 1:methodA And methodB Logically, it should not belong to the same transaction , It will be methodB The transaction propagation attribute of is modified to PROPAGATION_REQUIRES_NEW, such , perform methodB when , A new transaction will be created , No effect methodA The transaction .
situation 2: Business A And business B In business logic, it should belong to the same transaction , It will be methodA Medium try catch Get rid of
situation 3: Business A And business B In business logic, it should belong to the same transaction , however methodB The failure of the cannot affect methodA Transaction commit for , So still methodA in try catch methodB, And will methodB Set to PROPAGATION_NESTED, It means ,methodB It's a sub transaction , There is one savepoint, If it fails, it will be rolled back to savepoint, No effect methodA, If it succeeds A、B Submit... Together ,A And B It's all a business , It's just B It's a sub transaction .
situation 4: Business A Whether there is a transaction has no impact Remove business A Of @Transactional
Summary :
- Deeply understand the meaning of each state of transaction propagation , such as PROPAGATION_REQUIRED、PROPAGATION_SUPPORTS、PROPAGATION_MANDATORY、PROPAGATION_REQUIRES_NEW etc.
- At first I thought it was because spring In profile ,methodA Transaction set to ready-only=true( Read only transactions ) Why , After searching the information, we know that , Read only transactions do not conflict with transaction propagation , There are two mechanisms . Read only transactions , It's a special matter , Within this transaction , It can only be a query statement , Cannot contain modifications 、 UPDATE statement , The database may optimize read-only transactions ; Propagation attribute is the expression of the relationship between parent method and child method .
- Be careful , In the same class , Transaction nesting is subject to the outermost method , Nested transactions fail ; Transactions nested in different classes will take effect ;
边栏推荐
- 复现Thinkphp 2.x 任意代码执行漏洞
- MySQL overview
- Fundamentals of data communication - Principles of IP routing
- Appium automation test foundation - appium basic operation API (II)
- swiper. JS to achieve barrage effect
- 五种常见的咨询公司谈判策略以及如何维护自己的利益
- sql中set标签的使用
- 19.[STM32]HC_ SR04 ultrasonic ranging_ Timer mode (OLED display)
- Data communication foundation - route republication
- Summary of the third class
猜你喜欢

开发中Boolean类型使用遇到的坑

obj集合转为实体集合

vant tabbar遮挡内容的解决方式

Good article inventory

MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!

视觉体验全面升级,豪威集团与英特尔Evo 3.0共同加速PC产业变革

Number protection AXB function! (essence)

Analytic hierarchy process of mathematical modeling (including Matlab code)

Subclasses and superclasses of abstract classes

基于OpenHarmony的智能金属探测器
随机推荐
16. [stm32] starting from the principle, I will show you the DS18B20 temperature sensor - four digit digital tube displays the temperature
OceanBase社区版之OBD方式部署方式本地安装
Data communication foundation OSPF Foundation
Analytic hierarchy process of mathematical modeling (including Matlab code)
vlunhub- BoredHackerBlog Moriarty Corp
Noi / 1.4 07: collect bottle caps to win awards
abstract关键字和哪些关键字会发生冲突呢
OSI 七层模型
Go language programming specification combing summary
超分辨率技术在实时音视频领域的研究与实践
抽象类和接口的区别
Number protection AXB function! (essence)
20. [stm32] realize the function of intelligent garbage can by using ultrasonic module and steering gear
String modification problem solving Report
Appium自动化测试基础 — APPium基础操作API(一)
17.[STM32]仅用三根线带你驱动LCD1602液晶
Interval DP (gravel consolidation)
Replknet: it's not that large convolution is bad, but that convolution is not large enough. 31x31 convolution. Let's have a look at | CVPR 2022
ES6深入—ES6 Class 类
项目中批量update