当前位置:网站首页>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;}}
边栏推荐
- Goodbye Guangzhou paper invoices!The issuance of electronic invoices for accommodation fees will completely replace the invoices of hotels, restaurants and gas stations
- Add support for Textbundle
- A practice arrangement about map GIS (below) GIS practice of Redis
- 代码 Revert 后再次 Merge 会丢失的问题,已解决
- 【DB运营管理/开发解决方案】上海道宁为您提供提高工作便利性的集成开发工具——Orange
- ifconfig与ip命令的比较
- 网络安全笔记第四天day4(kali基本操作)
- MSP430如何给板子下载程序?(IAR MSPFET CCS)
- LitePal操作数据库
- 关于地图GIS的一次实践整理(下) Redis的GIS实践
猜你喜欢
随机推荐
调试技巧总结
【Pdf自动生成书签】
Geogebra 教程之 04 Geogebra 小程序
OpenCV founder: Open source must not be completely free!
A practice arrangement about map GIS (below) GIS practice of Redis
Some work experience after joining the digital ic design
最倒霉与最幸运
A surviving spouse of the opposite sex within large turn paragraph, what for
leetcode: 358. Reorder strings at K distance intervals
花甲的思考
"Beijing-Taiwan high-speed rail" debuted on Baidu map, can it really be built in 2035?
DOM树的遍历-----修改样式,选择元素,创建和删除节点
PIFuHD配置记录
YTU 2411: 谁去参加竞赛?【简单循环】
解决vim与外界的复制粘贴(不用安装插件)
alibaba数据同步组件canal的实践整理
成都纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
BUU刷题记录
[BX]和loop
行业的思考