当前位置:网站首页>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;}}边栏推荐
- 添加用户报错useradd: cannot open /etc/passwd
- OpenCV创始人:开源绝不能完全免费!
- Detailed explanation of new features of ES advanced function syntax
- 数据存储全方案----详解持久化技术
- ES6 advanced string processing new features
- Research on the Application of Privacy Computing Fusion
- DOM树的遍历-----修改样式,选择元素,创建和删除节点
- 言简意赅,说说 @Transactional 在项目中的使用
- 2022年G1工业锅炉司炉题库及模拟考试
- [Pdf generated automatically bookmarks]
猜你喜欢

Idea (优选)cherry-pick操作

flink The object probably contains or references non serializable fields.

ESP32的环境配置(arduino arduino2.0 VScode platform哪个好用?)

【DB运营管理/开发解决方案】上海道宁为您提供提高工作便利性的集成开发工具——Orange

"How to kick a bad habit to read notes?

关于地图GIS开发事项的一次实践整理(上)

ES进阶 函数功能语法新特性详解

leetcode: 358. Reorder strings at K distance intervals

①CAS SSO单点登录框架源码深度分析

OpenCV创始人:开源绝不能完全免费!
随机推荐
SIT221 Data Structures and Algorithms课程辅导
ASEMI整流桥GBJ5010参数,GBJ5010电压,GBJ5010电流
成都纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
Goodbye Chongqing paper invoices!The issuance of electronic invoices for accommodation expenses will soon completely replace the invoices of hotels, catering and gas stations
广州纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
MySQL - an SQL in MySQL is how to be performed?
[4G/5G/6G专题基础-154]: 5G无线准入控制RAC(Radio Admission Control)
Mysql_Note3
df和df -lh的意思
flink The object probably contains or references non serializable fields.
Some work experience after joining the digital ic design
What does the sanction of the mixer Tornado mean for the DeFi market?
关于地图GIS的一次实践整理(下) Redis的GIS实践
【Unity入门计划】Unity2D动画(1)-动画系统的组成及功能的使用
The 125th day of starting a business - a note
MySQL的主从复制+读写分离+分库分表,看这一篇文章就够了
MSP430如何给板子下载程序?(IAR MSPFET CCS)
gRPC基础概念:闭包
Entity to Vo conversion
否定语义转化层