@Transactional事务失效的几种常见的情况

@Transactional中代码异常抛出之后导致事务并没有回滚的几种情况:
  1. @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;
    }

     

  2.  同一个类中方法调用,导致@Transactional失效。比如有一个类Test,它的一个方法A,A再调用本类的方法B(不论方法B是用public还是private修饰),但方法A没有声明注解事务,而B方法有。则外部调用方法A之后,方法B的事务是不会起作用的。
  3. 使用try   catch的时候异常被你的 catch“吃了”导致@Transactional失效


版权声明:本文为cold_long原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。