当前位置:网站首页>言简意赅,说说 @Transactional 在项目中的使用
言简意赅,说说 @Transactional 在项目中的使用
2022-08-11 02:31:00 【Java Punk】

前言
更加具体的参数介绍,以及 @Transactional 失效场景介绍,请点击下面的链接学习:
正文
一、异常的分类
- 可查的异常(checked exceptions):Exception下除了RuntimeException外的异常;
- 不可查的异常(unchecked exceptions):RuntimeException及其子类和错误(Error);

二、@Transactional 属性介绍
@Transactional 注解的关键属性大致有九个(如图):
| 属性 | 类型 | 描述 |
|---|---|---|
| value | String | 可选的限定描述符,指定使用的事务管理器 |
| propagation | enum: Propagation | 可选的事务传播行为设置 |
| isolation | enum: Isolation | 可选的事务隔离级别设置 |
| readOnly | boolean | 读写或只读事务,默认读写 |
| timeout | int (in seconds granularity) | 事务超时时间设置 |
| rollbackFor | Class对象数组,必须继承自Throwable | 导致事务回滚的异常类数组 |
| rollbackForClassName | 类名数组,必须继承自Throwable | 导致事务回滚的异常类名字数组 |
| noRollbackFor | Class对象数组,必须继承自Throwable | 不会导致事务回滚的异常类数组 |
| noRollbackForClassName | 类名数组,必须继承自Throwable | 不会导致事务回滚的异常类名字数 |
三、@Transactional 使用
使用@Transaction注解时,阿里巴巴代码规范插件会提醒需要指定rollbackFor以显示的rollback。

默认情况下,Spring框架在使用 @Transaction 处理事务时默认只在 抛出运行时异常(可检查异常 - checked exceptions) 和 unchecked exceptions(不可检查异常) 时才会事务回滚。也就是说,当抛出个RuntimeException 或其子类例的实例时,从事务方法中抛出的 Checked exceptions 将不被标识进行事务回滚。
- 让checked异常回滚:在整个方法前加上
@Transactional(rollbackOn=Exception.class) - 让unchecked异常不回滚:
@Transactional(dontRollbackOn=RunTimeException.class) - 不需要事务管理的(只查询的)方法:
@Transactional(propagation=Propagation.NOT_SUPPORTED)
注意:
- 如果异常被try-catch了,事务就不会回滚,如果想让事务回滚必须再往外抛
try{}catch{throw Exception}。 - Spring团队的建议在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上。也可以在接口上使用 @Transactional 注解,但是这种情况需要设置基于接口的代理时才生效
- @Transactional 注解标识的方法,建议处理过程尽量的简单。尤其是带锁的事务方法,能不放在事务里面的最好不要放在事务里面。可以将常规的数据库查询操作放在事务前面进行,而将增、删、改等操作放在事务中
四、测试
如果 catch 块内的代码不写的话,事务是不会回滚的。因为事务被 catch 捕获到了,所以只能手动的进行事务回滚。
@Slf4j
@Service
public class DemoService {
@Autowired
private DemoMapper demoMapper;
@Transactional
public 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异常,必须手动回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
return insertResult;
}
}边栏推荐
- 0 in the figure, etc. LeetCode565. Array nesting
- HPSO and multi-core LSSVM based network intrusion detection
- install dlib library
- 微信公众号后台管理
- 八.数据的存储
- redis学习五redis的持久化RDB,fork,copyonwrite,AOF,RDB&AOF混合使用
- This Thursday evening at 19:00, Lesson 5 of the sixth phase of knowledge empowerment丨OpenHarmony WiFi subsystem
- AI+医疗:使用神经网络进行医学影像识别分析
- MySQL Basics [Part 1] | Database Overview and Data Preparation, Common Commands, Viewing Table Structure Steps
- TRCX: doping process analysis
猜你喜欢
![[The method of calling the child page from the parent page of the iframe] Stepping on the pit: It is the key to use `[x]` when getting elements. You cannot use `.eq(x)`, otherwise it will not be obtai](/img/ec/0cca8c7011770429c34a6aa1f36460.png)
[The method of calling the child page from the parent page of the iframe] Stepping on the pit: It is the key to use `[x]` when getting elements. You cannot use `.eq(x)`, otherwise it will not be obtai

微信公众号后台管理

MySQL八股文背诵版(续)

Deep Learning - Second Time

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

The practice of alibaba data synchronization component canal

BUU刷题记录

Vim and copy and paste from the outside (don't need to install the plugin)

Inter-process communication method (2) Named pipe

WeChat public account background management
随机推荐
软件测试面试题:性能测试工作?
TRCX: doping process analysis
Logstash日志数据写入异常排查问题总结
2022茶艺师(中级)考试试题及模拟考试
A surviving spouse of the opposite sex within large turn paragraph, what for
MySQL权限控制、分区表、快速复制表
ifconfig与ip命令的比较
JVM类加载机制
Lianshengde W801 series 6-Analyze the Bluetooth communication source code of W801 from the perspective of WeChat applet (indicate method)
①In-depth analysis of CAS SSO single sign-on framework source code
一言(ヒトコト)Hitokoto API
架构篇(二)架构的复杂度来源
[oops-framework] Template project [oops-game-kit] Introduction
软件测试面试题:软件测试的过程的V模型,说出它的缺点?
软件测试面试题:什么是α测试,β测试?
0 in the figure, etc. LeetCode565. Array nesting
The practice of alibaba data synchronization component canal
《如何戒掉坏习惯》读书笔记
Ninjutsu_v3_08_2020-安全渗透系统安装
How to realize the repeatable design of FPGA