@Transactional中代码异常抛出之后导致事务并没有回滚的几种情况:
-
@Transactional 应用在非 public 修饰的方法上。
protected TransactionAttribute computeTransactionAttribute(Method method, Class<?> targetClass) { // Don't allow no-public methods as required. if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) { return null; }
- 同一个类中方法调用,导致@Transactional失效。比如有一个类Test,它的一个方法A,A再调用本类的方法B(不论方法B是用public还是private修饰),但方法A没有声明注解事务,而B方法有。则外部调用方法A之后,方法B的事务是不会起作用的。
-
使用try catch的时候异常被你的 catch“吃了”导致@Transactional失效