点击上方“芋道源码”,选择“设为星标”
管她前浪,还是后浪?
能浪的浪,才是好浪!
每天 10:33 更新文章,每天掉亿点点头发...
源码精品专栏
Java 后端面试的时候,面试官经常会问到 @Transactional 的原理,以及容易踩的坑,今天就带大家把这几块知识吃透。
这篇文章,会先讲述 @Transactional 的 4 种不生效的 Case,然后再通过源码解读,分析 @Transactional 的执行原理,以及部分 Case 不生效的真正原因。
![6fc8f2a8da3d50d2c82b79ab34f86464.png](https://img-blog.csdnimg.cn/img_convert/6fc8f2a8da3d50d2c82b79ab34f86464.png)
项目准备
下面是 DB 数据和 DB 操作接口:
uid | uname | usex |
---|---|---|
1 | 张三 | 女 |
2 | 陈恒 | 男 |
3 | 楼仔 | 男 |
// 提供的接口
public interface UserDao {
// select * from user_test where uid = "#{uid}"
public MyUser selectUserById(Integer uid);
// update user_test set uname =#{uname},usex = #{usex} where uid = #{uid}
public int updateUser(MyUser user);
}
基础测试代码,testSuccess() 是事务生效的情况:
@Service
public class UserController {
@Autowired
private UserDao userDao;
public void update(Integer id) {
MyUser user = new MyUser();
user.setUid(id);
user.setUname("张三-testing");
user.setUsex("女");
userDao.updateUser(user);
}
public MyUser query(Integer id) {
MyUser user = userDao.selectUserById(id);
return user;
}
// 正常情况
@Transactional(rollbackFor = Exception.class)
public void testSuccess() throws Exception {
Integer id = 1;
MyUser user = query(id);
System.out.println("原记录:" + user);
update(id);
throw new Exception("事务生效");
}
}
基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://gitee.com/zhijiantianya/ruoyi-vue-pro
视频教程:https://doc.iocoder.cn/video/
事务不生效的几种 Case
主要讲解 4 种事务不生效的 Case:
类内部访问 :A 类的 a1 方法没有标注 @Transactional,a2 方法标注 @Transactional,在 a1 里面调用 a2;
私有方法 :将 @Transactional 注解标注在非 public 方法上;
异常不匹配 :@Transactional 未设置 rollbackFor 属性,方法返回 Exception 等异常;
多线程 :主线程和子线程的调用,线程抛出异常。
Case 1: 类内部访问
我们在类 UserController 中新增一个方法 testInteralCall():
public void testInteralCall() throws Exception {
testSuccess();
throw new Exception("事务不生效:类内部访问");
}
这里 testInteralCall() 没有标注 @Transactional,我们再看一下测试用例:
public static void main(String[] args) throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserController uc = (UserController) applicationContext.getBean("userController");
try {
uc.testSuccess();
} finally {
MyUser user = uc.query(1);
System.out.println("修改后的记录:" + user);
}
}
// 输出:
// 原记录:MyUser(uid=1, uname=张三, usex=女)
// 修改后的记录:MyUser(uid=1, uname=张三-testing, usex=女)
从上面的输出可以看到,事务并没有回滚,这个是什么原因呢?
因为 @Transactional 的工作机制是基于 AOP 实现,AOP 是使用动态代理实现的,如果通过代理直接调用 testSuccess(),通过 AOP 会前后进行增强,增强的逻辑其实就是在 testSuccess() 的前后分别加上开启、提交事务的逻辑,后面的源码会进行剖析。
现在是通过 testInteralCall() 去调用 testSuccess(),testSuccess() 前后不会进行任何增强操作,也就是类内部调用,不会通过代理方式访问。
如果还是不太清楚,推荐再看看这篇文章,里面有完整示例,非常完美诠释“类内部访问”不能前后增强的原因:https://blog.csdn.net/Ahuuua/article/details/123877835
Case 2: 私有方法
在私有方法上,添加 @Transactional 注解也不会生效:
@Transactional(rollbackFor = Exception.class)
private void testPirvateMethod() throws Exception {
Integer id = 1;
MyUser user = query(id);
System.out.println("原记录:" + user);
update(id);
throw new Exception("测试事务生效");
}
直接使用时,下面这种场景不太容易出现,因为 IDEA 会有提醒,文案为: Methods annotated with '@Transactional' must be overridable,至于深层次的原理,源码部分会给你解读。
Case 3: 异常不匹配
这里的 @Transactional 没有设置 rollbackFor = Exception.class 属性:
@Transactional
public void testExceptionNotMatch() throws Exception {
Integer id = 1;
MyUser user = query(id);
System.out.println("原记录:" + user);
update(id);
throw new Exception("事务不生效:异常不匹配");
}
测试方法:同 Case1
// 输出:
// 原记录:User[uid=1,uname=张三,usex=女]
// 修改后的记录:User[uid=1,uname=张三-test,usex=女]
@Transactional 注解默认处理运行时异常,即只有抛出运行时异常时,才会触发事务回滚,否则并不会回滚,至于深层次的原理,源码部分会给你解读。
Case 4: 多线程
下面给出两个不同的姿势,一个是子线程抛异常,主线程 ok;一个是子线程 ok,主线程抛异常。
父线程抛出异常
父线程抛出异常,子线程不抛出异常:
public void testSuccess() throws Exception {
Integer id = 1;
MyUser user = query(id);
System.out.println("原记录:" + user);
update(id);
}
@Transactional(rollbackFor = Exception.class)
public void testMultThread() throws Exception {
new Thread(new Runnable() {
@SneakyThrows
@Override
public void run() {
testSuccess();
}
}).start();
throw new Exception("测试事务不生效");
}
父线程抛出线程,事务回滚,因为子线程是独立存在,和父线程不在同一个事务中,所以子线程的修改并不会被回滚,
子线程抛出异常
父线程不抛出异常,子线程抛出异常:
public void testSuccess() throws Exception {
Integer id = 1;
MyUser user = query(id);
System.out.println("原记录:" + user);
update(id);
throw new Exception("测试事务不生效");
}
@Transactional(rollbackFor = Exception.class)
public void testMultThread() throws Exception {
new Thread(new Runnable() {
@SneakyThrows
@Override
public void run() {
testSuccess();
}
}).start();
}
由于子线程的异常不会被外部的线程捕获,所以父线程不抛异常,事务回滚没有生效。
基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://gitee.com/zhijiantianya/yudao-cloud
视频教程:https://doc.iocoder.cn/video/
源码解读
下面我们从源码的角度,对 @Transactional 的执行机制和事务不生效的原因进行解读。
@Transactional 执行机制
我们只看最核心的逻辑,代码中的 interceptorOrInterceptionAdvice 就是 TransactionInterceptor 的实例,入参是 this 对象。
红色方框有一段注释,大致翻译为 “它是一个拦截器,所以我们只需调用即可:在构造此对象之前,将静态地计算切入点。”
![fdac0634b7c33fee6801b433a5fee294.png](https://img-blog.csdnimg.cn/img_convert/fdac0634b7c33fee6801b433a5fee294.png)
this 是 ReflectiveMethodInvocation 对象,成员对象包含 UserController 类、testSuccess() 方法、入参和代理对象等。
![e7f42dbcf4af6f615bd01b5dd8801006.png](https://img-blog.csdnimg.cn/img_convert/e7f42dbcf4af6f615bd01b5dd8801006.png)
进入 invoke() 方法后:
![e4c708f31a5f213aa0a5ea172cff720b.png](https://img-blog.csdnimg.cn/img_convert/e4c708f31a5f213aa0a5ea172cff720b.png)
前方高能!!!这里就是事务的核心逻辑,包括判断事务是否开启、目标方法执行、事务回滚、事务提交。
![e1e5d0a703cc491f1ec4a621f0c9f2c7.png](https://img-blog.csdnimg.cn/img_convert/e1e5d0a703cc491f1ec4a621f0c9f2c7.png)
private 导致事务不生效原因
在上面这幅图中,第一个红框区域调用了方法 getTransactionAttribute(),主要是为了获取 txAttr 变量,它是用于读取 @Transactional 的配置,如果这个 txAttr = null,后面就不会走事务逻辑,我们看一下这个变量的含义:
![e3d28b5490863954d1bc3120a47a3fa9.png](https://img-blog.csdnimg.cn/img_convert/e3d28b5490863954d1bc3120a47a3fa9.png)
我们直接进入 getTransactionAttribute(),重点关注获取事务配置的方法。
![6c3f8b7c2c19cff0b28f9978203dc980.png](https://img-blog.csdnimg.cn/img_convert/6c3f8b7c2c19cff0b28f9978203dc980.png)
前方高能!!!这里就是 private 导致事务不生效的原因所在 ,allowPublicMethodsOnly() 一直返回 false,所以重点只关注 isPublic() 方法。
![701b34ecdbb9962c3b8d699d4afaf217.png](https://img-blog.csdnimg.cn/img_convert/701b34ecdbb9962c3b8d699d4afaf217.png)
下面通过位与计算,判断是否为 Public,对应的几类修饰符如下:
PUBLIC: 1
PRIVATE: 2
PROTECTED: 4
![eff849e7e8d1cf4af196aee9e63eac17.png](https://img-blog.csdnimg.cn/img_convert/eff849e7e8d1cf4af196aee9e63eac17.png)
看到这里,是不是豁然开朗了,有没有觉得很有意思呢~~
异常不匹配原因
我们继续回到事务的核心逻辑,因为主方法抛出 Exception() 异常,进入事务回滚的逻辑:
![7006a6a1f4ba1249a5f5093c84014a38.png](https://img-blog.csdnimg.cn/img_convert/7006a6a1f4ba1249a5f5093c84014a38.png)
进入 rollbackOn() 方法,判断该异常是否能进行回滚,这个需要判断主方法抛出的 Exception() 异常,是否在 @Transactional 的配置中:
![afbb37bd16942e3f67d49e1e21e6d5e9.png](https://img-blog.csdnimg.cn/img_convert/afbb37bd16942e3f67d49e1e21e6d5e9.png)
我们进入 getDepth() 看一下异常规则匹配逻辑,因为我们对 @Transactional 配置了 rollbackFor = Exception.class,所以能匹配成功:
![1ac56283b925c6d4b068258de7374490.png](https://img-blog.csdnimg.cn/img_convert/1ac56283b925c6d4b068258de7374490.png)
示例中的 winner 不为 null,所以会跳过下面的环节。但是当 winner = null 时,也就是没有设置 rollbackFor 属性时,会走默认的异常捕获方式。
![d1bb220e2130ff3854820f4b8c4e928e.png](https://img-blog.csdnimg.cn/img_convert/d1bb220e2130ff3854820f4b8c4e928e.png)
前方高能!!!这里就是异常不匹配原因的原因所在 ,我们看一下默认的异常捕获方式:
![4cce2fccf235ee631064e1f6d8f72abf.png](https://img-blog.csdnimg.cn/img_convert/4cce2fccf235ee631064e1f6d8f72abf.png)
是不是豁然开朗,当没有设置 rollbackFor 属性时,默认只对 RuntimeException 和 Error 的异常执行回滚。
欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢:
已在知识星球更新源码解析如下:
最近更新《芋道 SpringBoot 2.X 入门》系列,已经 101 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。
提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。
文章有帮助的话,在看,转发吧。
谢谢支持哟 (*^__^*)