1,354 字 · 约 5 分钟读完
声明式事务 @Transactional 在 Spring 项目里用得很频繁,但实际开发中经常出现“加了注解却没回滚”的情况。这背后大多数是异常传播方式或代理生效条件没满足,而不是注解本身没生效。本文整理三类最常见的事务失效场景。
事务生效的基本条件
Spring 声明式事务的底层依赖 AOP 代理。方法真正执行时,调用顺序是:
- 先进入事务代理
- 代理开启事务
- 再调用目标方法
- 方法正常结束则提交
- 方法抛出符合回滚条件的异常则回滚
由此可以提炼出两个关键点:
- 异常要能抛到事务代理层
- 方法要满足代理生效的基本条件
下文三类失效场景都围绕这两点展开。
异常被自己捕获处理
失效原因
事务通知只在感知到目标方法抛出异常时才决定是否回滚。如果方法内部 catch 住异常后没有继续向外抛,事务代理看到的就是“方法正常结束”,于是事务被提交。
典型代码
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
@Transactional
public void transfer(Integer from, Integer to, Double money) {
try {
Account fromAccount = accountDao.selectById(from);
fromAccount.setMoney(fromAccount.getMoney() - money);
accountDao.updateById(fromAccount);
// 模拟异常
int a = 1 / 0;
Account toAccount = accountDao.selectById(to);
toAccount.setMoney(toAccount.getMoney() + money);
accountDao.updateById(toAccount);
} catch (Exception e) {
e.printStackTrace();
}
}
}
上面这段代码里:
- 转出账户的钱已经扣掉
- 中间发生了运行时异常
- 异常被
catch住,没有继续抛出 - Spring 认为方法正常结束,事务提交
最终表现为数据只执行了一半,转账不一致。
修复写法
本地如果确实需要记录日志或做补充处理,可以在 catch 之后继续抛出运行时异常:
@Transactional
public void transfer(Integer from, Integer to, Double money) {
try {
Account fromAccount = accountDao.selectById(from);
fromAccount.setMoney(fromAccount.getMoney() - money);
accountDao.updateById(fromAccount);
int a = 1 / 0;
Account toAccount = accountDao.selectById(to);
toAccount.setMoney(toAccount.getMoney() + money);
accountDao.updateById(toAccount);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
处理原则是:可以捕获异常做收尾,但不要把异常“吃掉”。
抛出检查异常导致不回滚
默认回滚规则
Spring 的默认回滚策略是:
- 遇到
RuntimeException回滚 - 遇到
Error回滚 - 遇到检查异常(
Exception及其子类,但不包括运行时异常)默认不回滚
因此当方法抛出 IOException、FileNotFoundException、特定场景下的 SQLException 等检查异常时,即使异常被事务代理感知到,也不会触发回滚。
典型代码
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
@Transactional
public void transfer(Integer from, Integer to, Double money) throws FileNotFoundException {
Account fromAccount = accountDao.selectById(from);
fromAccount.setMoney(fromAccount.getMoney() - money);
accountDao.updateById(fromAccount);
// 模拟检查异常
new FileInputStream("not-exists.txt");
Account toAccount = accountDao.selectById(to);
toAccount.setMoney(toAccount.getMoney() + money);
accountDao.updateById(toAccount);
}
}
这里扣款已经执行,随后读取文件抛出 FileNotFoundException。因为它是检查异常,Spring 默认不会回滚,部分数据仍然被提交。
修复写法
显式指定回滚规则,把检查异常也纳入回滚范围:
@Transactional(rollbackFor = Exception.class)
public void transfer(Integer from, Integer to, Double money) throws FileNotFoundException {
Account fromAccount = accountDao.selectById(from);
fromAccount.setMoney(fromAccount.getMoney() - money);
accountDao.updateById(fromAccount);
new FileInputStream("not-exists.txt");
Account toAccount = accountDao.selectById(to);
toAccount.setMoney(toAccount.getMoney() + money);
accountDao.updateById(toAccount);
}
配置 rollbackFor = Exception.class 之后,只要抛出 Exception 及其子类,就会按规则回滚。
事务方法不是 public
失效原因
Spring 为方法创建代理并织入事务通知时,对方法的可见性有要求。常见结论是:事务方法必须声明为 public,否则事务增强可能不会按预期生效。
典型代码
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
@Transactional(rollbackFor = Exception.class)
void transfer(Integer from, Integer to, Double money) throws FileNotFoundException {
Account fromAccount = accountDao.selectById(from);
fromAccount.setMoney(fromAccount.getMoney() - money);
accountDao.updateById(fromAccount);
new FileInputStream("not-exists.txt");
Account toAccount = accountDao.selectById(to);
toAccount.setMoney(toAccount.getMoney() + money);
accountDao.updateById(toAccount);
}
}
注解写好了,但因为 transfer 不是 public,事务增强可能没有生效。
修复写法
把方法改成 public 即可:
@Transactional(rollbackFor = Exception.class)
public void transfer(Integer from, Integer to, Double money) throws FileNotFoundException {
Account fromAccount = accountDao.selectById(from);
fromAccount.setMoney(fromAccount.getMoney() - money);
accountDao.updateById(fromAccount);
new FileInputStream("not-exists.txt");
Account toAccount = accountDao.selectById(to);
toAccount.setMoney(toAccount.getMoney() + money);
accountDao.updateById(toAccount);
}
三类场景的归因
把上面三种情形对应到前面的两个关键点上,关系如下:
| 场景 | 对应关键点 | 本质 |
|---|---|---|
| 异常被捕获处理 | 异常没传到代理层 | 事务代理没有感知到异常 |
| 抛出检查异常 | 异常类型不满足默认回滚规则 | 异常虽然抛出来了,但不属于默认回滚类型 |
| 事务方法不是 public | 代理没有正常织入 | 事务增强没有按预期生效 |
也就是说,所有“事务失效”基本都可以归结为:异常没有正确传递到事务代理,或者事务代理本身没有成功织入。
排查时的常见切入点
代码里 try/catch 之后调用 e.printStackTrace() 是最容易踩到的反模式。排查时可以按下面顺序快速定位:
- 确认
@Transactional注解是否被 Spring 扫描到(包扫描、AOP 自动代理是否开启) - 确认事务方法可见性是
public - 确认方法抛出的异常属于
RuntimeException/Error,或者在rollbackFor中已经声明 - 确认异常没有被业务代码就地
catch掉
按这个顺序排查,绝大多数“事务没回滚”问题都能定位到具体原因。