当前位置:网站首页>Briefly, talk about the use of @Transactional in the project
Briefly, talk about the use of @Transactional in the project
2022-08-11 02:56:00 【Java Punk】

Foreword
More specific parameter introduction, and introduction of @Transactional failure scenarios, please click the link below to learn:
- @Transactional annotation parameters detailed explanation, as well as the use of annotation features (collector's edition)
- @TransactionalThe problem that annotations cause cross-database queries to fail
Body
I. Classification of exceptions
- Checked exceptions: exceptions other than RuntimeException under Exception;
- Unchecked exceptions: RuntimeException and its subclasses and Errors;

Second, @Transactional attribute introduction
There are roughly nine key attributes of the @Transactional annotation (as shown in the figure):
| Properties | Type | Description |
|---|---|---|
| value | String | optional qualifying descriptor specifying the transaction manager to use |
| propagation | enum: Propagation | Optional transaction propagation behavior settings |
| isolation | enum: Isolation | Optional transaction isolation level settings |
| readOnly | boolean | Read-write or read-only transaction, default read-write |
| timeout | int (in seconds granularity) | Transaction timeout setting |
| rollbackFor | Array of Class objects, must inherit from Throwable | Array of exception classes that cause transaction rollback |
| rollbackForClassName | Array of class names, must inherit from Throwable | Array of exception class names that caused the transaction to be rolled back |
| noRollbackFor | Array of Class objects, must inherit from Throwable | Array of exception classes that will not cause transaction rollback |
| noRollbackForClassName | Array of class names, must inherit from Throwable | Number of exception class names that do not cause transaction rollback |
Three, @Transactional use
When using the @Transaction annotation, the Alibaba Code Specification plugin will remind you to specify rollbackFor to display the rollback.

By default, the Spring framework only throws runtime exceptions (checked exceptions - checked exceptions) and unchecked exceptions (unchecked exceptions) when using @Transaction to handle transactions by default. the transaction is rolled back.That is, when an instance of RuntimeException or its subclasses is thrown, Checked exceptions thrown from transaction methods will not be flagged for transaction rollback.
- To rollback the checked exception: add
@Transactional(rollbackOn=Exception.class) before the entire method - Make unchecked exception not rollback:
@Transactional(dontRollbackOn=RunTimeException.class) - Methods that do not require transaction management (query only):
@Transactional(propagation=Propagation.NOT_SUPPORTED)
Note:
- If the exception is try-catch, the transaction will not be rolled back. If you want the transaction to be rolled back, you must throw
try{}catch{throw Exception}. - The Spring team's recommendation is to use the @Transactional annotation on concrete classes (or class methods), not on any interfaces that the class implements.You can also use the @Transactional annotation on the interface, but this will only take effect when you need to set an interface-based proxy
- The method identified by the @Transactional annotation, it is recommended that the processing process be as simple as possible.Especially the transaction method with lock, it is best not to put it in the transaction if it can not be placed in the transaction.Regular database query operations can be carried out in front of the transaction, and operations such as addition, deletion, and modification can be placed in the transaction
Four. Test
If the code in the catch block is not written, the transaction will not be rolled back.Because the transaction is caught by catch, the transaction can only be rolled back manually.
@[email protected] class DemoService {@Autowiredprivate DemoMapper demoMapper;@Transactionalpublic Integer insert(MemberEntity memberEntity) {Integer insertResult = 0;try {insertResult = demoMapper.save(memberEntity);log.info("insertResult:{}", insertResult);int result = 1 / memberEntity.getAge();} catch (Exception e) {log.error("errorMsg:{}", e.getMessage());// catch exception, must roll back manuallyTransactionAspectSupport.currentTransactionStatus().setRollbackOnly();}return insertResult;}}边栏推荐
猜你喜欢
随机推荐
YTU 2418: C语言习题 矩阵元素变换
Summary of Logstash log data write exception troubleshooting
成都纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
调试技巧总结
关于地图GIS的一次实践整理(下) Redis的GIS实践
A surviving spouse of the opposite sex within large turn paragraph, what for
Detailed explanation of new features of ES advanced function syntax
ASEMI整流桥GBJ5010参数,GBJ5010电压,GBJ5010电流
Detailed explanation of new features of ES advanced array function syntax
【PHP】入门知识
求和、计数的窗口函数应用
Typescript学习笔记 | 字节青训营笔记
Mysql_Note5
JVM类加载机制
多线程之ThreadPoolExecutor
重庆纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
PIFuHD配置记录
Goodbye Chongqing paper invoices!The issuance of electronic invoices for accommodation expenses will soon completely replace the invoices of hotels, catering and gas stations
2022年G1工业锅炉司炉题库及模拟考试
架构篇(二)架构的复杂度来源









![[BX]和loop](/img/3c/1be08db6898613c3a1c0bcd39e73be.png)