就这?Spring 事务失效场景及解决方案

2022-12-21,,,,

小明:靓仔,我最近遇到了很邪门的事。

靓仔:哦?说来听听。

小明:上次看了你的文章《就这?一篇文章让你读懂 Spring 事务》,对事务有了详细的了解,但是在项目中还是遇到了问题,明明加了事务注解 @Transactional,却没有生效。

靓仔:那今天我就给你总结下哪些场景下事务会失效

1、数据库引擎不支持事务

Mysql 常用的数据库引擎有 InnoDB 和 MyISAM,其中前者是支持事务的,而后者并不支持,MySQL 5.5.5 以前的默认存储引擎是:MyISAM,之前的版本默认的都是:InnoDB ,所以一定要注意自己使用的数据库支不支持事务。

2、没有被 Spring 管理

事务方法所在的类没有被注入Spring 容器,比如下面这样:

public class OrderServiceImpl implements OrderService {
@Autowired
AccountMapper accountMapper;
@Autowired
ProductMapper productMapper; @Transactional
@Override
public void placeOrder() {
// 此处省略一堆逻辑 // 修用户改余额和商品库存
accountMapper.update();
productMapper.update();
}
}

这个类没有加 @service 注解,事务是不会生效的。

3、不是 public 方法

官方文档上已经说的很清楚了,@Transactional 注解只能用于 public 方法,如果要用在非 public 方法上,可以开启 AspectJ 代理模式。

4、异常被捕获

比如下面这个例子:

@Service
public class OrderServiceImpl implements OrderService {
@Autowired
AccountMapper accountMapper;
@Autowired
ProductMapper productMapper; @Transactional
@Override
public void placeOrder() {
try{
// 此处省略一堆逻辑 // 修用户改余额和商品库存
accountMapper.update();
productMapper.update();
} catch (Exception e) { }
}
}

当该方法发生异常的时候,由于异常被捕获,并没有抛出来,所以事务会失效,那这种情况下该怎么解决呢?别急,往下看

@Service
public class OrderServiceImpl implements OrderService {
@Autowired
AccountMapper accountMapper;
@Autowired
ProductMapper productMapper; @Transactional
@Override
public void placeOrder() {
try{
// 此处省略一堆逻辑 // 修用户改余额和商品库存
accountMapper.update();
productMapper.update();
} catch (Exception e) {
// 手动回滚
TransactionAspectSupport.crrentTransactionStatus().setRollbackOnly();
}
}
}

可以通过

TransactionAspectSupport.crrentTransactionStatus().setRollbackOnly();

手动进行回滚操作。

5、异常类型错误

@Transactional 注解默认只回滚 RuntimeException 类型的异常,所以在使用的时候建议修改成 Exception 类型

@Transactional(rollbackFor = Exception.class)

6、内部调用事务方法

这应该是最常见的事务失效的的场景了吧,也是我要重点讲的情况。

有些业务逻辑比较复杂的操作,比如前面例子中的下单方法,往往在写操作之前会有一堆逻辑,如果所有操作都放在一个方法里,并且加上事务,那么很可能会因为事务执行时间过长,导致事务超时,就算没超时也会影响下单接口的性能。这时可以将写操作提取出来,只对写操作加上事务,那么压力就会小很多。

请看下面这个例子:

@Service
public class OrderServiceImpl implements OrderService {
@Autowired
AccountMapper accountMapper;
@Autowired
ProductMapper productMapper; @Override
public void placeOrder() {
// 此处省略一堆逻辑
this.updateByTransactional();
} @Transactional
public void updateByTransactional() {
// 修用户改余额和商品库存
accountMapper.update();
productMapper.update();
}
}

由于发生了内部调用,而没有经过 Spring 的代理,事务就不会生效,官方文档中也有说明:

那这种情况下该怎么办呢?

方案一:改为外部调用

内部调用不行,那我改成外部调用不就行了么

@Service
public class OrderServiceImpl implements OrderService {
@Autowired
OrderTransactionService orderTransactionService; @Override
public void placeOrder() {
// 此处省略一堆逻辑 orderTransactionService.updateByTransactional();
}
} @Service
public class OrderTransactionService {
@Autowired
AccountMapper accountMapper;
@Autowired
ProductMapper productMapper; @Transactional
public void updateByTransactional() {
// 修用户改余额和商品库存
accountMapper.update();
productMapper.update();
}
}

这是比较容易理解的一种方法

方案二:使用编程式事务

既然声明式事务有问题,那我换成编程式事务可还行?

@Service
public class OrderServiceImpl implements OrderService {
@Autowired
AccountMapper accountMapper;
@Autowired
ProductMapper productMapper;
@Autowired
TransactionTemplate transactionTemplate; @Override
public void placeOrder() {
// 此处省略一堆逻辑 // TransactionCallbackWithoutResult 无返回参数
// TransactionCallback 有返回参数
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
try {
this.updateByTransactional();
} catch (Exception e) {
log.error("下单失败", e);
transactionStatus.setRollbackOnly();
}
}
});
} public void updateByTransactional() {
// 修用户改余额和商品库存
accountMapper.update();
productMapper.update();
}
}

甭管他黑猫白猫,能抓住老鼠的就是好猫

方案三:通过外部方法调回来

这个是我看到网友提供的一种方法,又想用注解,又想自调用,那么可以参考编程式事务的方式来实现。

@Component
public class TransactionComponent {
public interface Callback<T>{
T run() throws Exception;
} public interface CallbackWithOutResult {
void run() throws Exception;
} // 带返回参数
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Nullable
public <T> T doTransactional(Callback<T> callback) throws Exception {
return callback.run();
} // 无返回参数
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Nullable
public void doTransactionalWithOutResult(CallbackWithOutResult callbackWithOutResult) throws Exception {
callbackWithOutResult.run();
}
}

这样通过 TransactionComponent 调用内部方法,就可以解决失效问题了。

@Service
public class OrderServiceImpl implements OrderService {
@Autowired
AccountMapper accountMapper;
@Autowired
ProductMapper productMapper;
@Autowired
TransactionComponent transactionComponent; @Override
public void placeOrder() {
// 此处省略一堆逻辑 transactionComponent.doTransactionalWithOutResult(() -> this.updateByTransactional());
} public void updateByTransactional() {
// 修用户改余额和商品库存
accountMapper.update();
productMapper.update();
}
}

总结

本文总结了比较常见的几种事务失效的场景,以及一些解决方案,不一定很全。你还遇到了哪些我没提到的场景,欢迎分享,有不足之处,也欢迎指正。

END

往期推荐

就这?一篇文章让你读懂 Spring 事务

SpringBoot+Redis 实现消息订阅发布

最详细的图文解析Java各种锁(终极篇)

常见代码重构技巧,你一定用得上

图文详解 23 种设计模式

就这?Spring 事务失效场景及解决方案的相关教程结束。

《就这?Spring 事务失效场景及解决方案.doc》

下载本文的Word格式文档,以方便收藏与打印。