当前位置:网站首页>@Transactional 注解使用详解
@Transactional 注解使用详解
2022-07-28 22:23:00 【心动的偏执】
@Transactional 的作用范围
- 方法 :推荐将注解使用于方法上,不过需要注意的是:该注解只能应用到 public 方法上,否则不生效。
- 类 :如果这个注解使用在类上的话,表明该注解对该类中所有的 public 方法都生效。
- 接口 :不推荐在接口上使用。
@Transactional 的常用配置参数总结(只列出了 5 个我平时比较常用的):
属性名
说明
propagation
事务的传播行为,默认值为 REQUIRED,可选的值在上面介绍过
isolation
事务的隔离级别,默认值采用 DEFAULT,可选的值在上面介绍过
timeout
事务的超时时间,默认值为-1(不会超时)。如果超过该时间限制但事务还没有完成,则自动回滚事务。
readOnly
指定事务是否为只读事务,默认值为 false。
rollbackFor
用于指定能够触发事务回滚的异常类型,并且可以指定多个异常类型。
@Transactional 事务注解原理
面试中在问 AOP 的时候可能会被问到的一个问题。简单说下吧!
我们知道,@Transactional 的工作机制是基于 AOP 实现的,AOP 又是使用动态代理实现的。如果目标对象实现了接口,默认情况下会采用 JDK 的动态代理,如果目标对象没有实现了接口,会使用 CGLIB 动态代理。
如果一个类或者一个类中的 public 方法上被标注@Transactional 注解的话,Spring 容器就会在启动的时候为其创建一个代理类,在调用被@Transactional 注解的 public 方法的时候,实际调用的是,TransactionInterceptor 类中的 invoke()方法。这个方法的作用就是在目标方法之前开启事务,方法执行过程中如果遇到异常的时候回滚事务,方法调用完成之后提交事务。
TransactionInterceptor类中的invoke()方法内部实际调用的是TransactionAspectSupport类的invokeWithinTransaction()方法。
Spring AOP 自调用问题
若同一类中的其他没有 @Transactional 注解的方法内部调用有 @Transactional 注解的方法,有@Transactional 注解的方法的事务会失效。
这是由于Spring AOP代理的原因造成的,因为只有当 @Transactional 注解的方法在类以外被调用的时候,Spring 事务管理才生效。
MyService 类中的method1()调用method2()就会导致method2()的事务失效。
@Service
public class MyService {
private void method1() {
method2();
//......
}
@Transactional
public void method2() {
//......
}
}
解决办法就是避免同一类中自调用或者使用 AspectJ 取代 Spring AOP 代理。
@Transactional 的使用注意事项总结
@Transactional注解只有作用到 public 方法上事务才生效,不推荐在接口上使用;- 避免同一个类中调用
@Transactional注解的方法,这样会导致事务失效; - 正确的设置
@Transactional的rollbackFor和propagation属性,否则事务可能会回滚失败; - 被
@Transactional注解的方法所在的类必须被 Spring 管理,否则不生效; - 底层使用的数据库必须支持事务机制,否则不生效;
边栏推荐
- Real time data warehouse: Didi's real-time data warehouse landing practice
- 基于 FPGA 实现数字时钟详细原理讲解及验证结果
- 【C】 Introduction and Simulation Implementation of ATOI and offsetof
- Multi sensor fusion positioning (I) -- 3D laser odometer
- Visual full link log tracking
- 动态规划问题(一)
- Real time data warehouse: meituan's implementation of real-time data warehouse construction based on Flink
- Introduction and solution of common security vulnerabilities in web system CSRF attack
- "Method not allowed", 405 problem analysis and solution
- #{}和${}的区别
猜你喜欢

JS高级 之 ES6~ES13 新特性

"Method not allowed", 405 problem analysis and solution

Google browser, no installation required

Idea2021.2 installation and configuration (continuous update)

【TA-霜狼_may-《百人计划》】美术2.2 模型基础

Sword finger offer 64. find 1+2+... +n, logical operator short circuit effect

Have passed hcip and joined the company of your choice, and share the learning experience and experience of Huawei certification

Advanced area of attack and defense world web masters warmup

Event extraction and documentation (2018)
![[applet project development -- JD mall] uni app commodity classification page (first)](/img/6c/5b92fc1f18d58e0fdf6f1896188fcd.png)
[applet project development -- JD mall] uni app commodity classification page (first)
随机推荐
递归/回溯刷题(中)
Attack and defense world web master advanced area php2
Erc20 Standard Code
Leetcode64. Minimum path sum
[MySQL series] MySQL database foundation
研发效能的道法术器
Advanced area of attack and defense world web masters supersqli
跳表的原理
How NAT configures address translation
Type 1-5 components
【C】 Drink soda and find a single dog
Those "experiences and traps" in the data center
PHP poster QR code synthesis
curl (7) Failed connect to localhost8080; Connection refused
Application of Devops in Internet of things solutions
1-7 solve the problem of this pointing of methods in classes
递归/回溯刷题(下)
【MySQL 8】Generated Invisible Primary Keys(GIPK)
CV target detection model sketch (2)
mysql索引失效的常见9种原因详解