Spring事务失效原因以及解决方案

1. 抛出检查异常导致事务不能正确回滚

    原因:Spring 默认只会回滚非检查异常

    解决方案:配置 rollbackFor 属性 (@Transactional(rollbackFor = Exception.class

2.业务方法内自己 try-catch 异常导致事务不能正确回滚

    原因:事务通知只有捉到了目标抛出的异常,才能进行后续的回滚处理,如果目标自己处理掉异常,事务通知无法知悉

    解决方案: 1)异常原样抛出,即在 catch 块添加 throw new RuntimeException(e);

       2) 手动设置 TransactionStatus.setRollbackOnly() ,在 catch 块添加 TransactionInterceptor.currentTransactionStatus().setRollbackOnly();

3. aop 切面顺序导致导致事务不能正确回滚

    原因:事务切面优先级最低,但如果自定义的切面优先级和他一样,则还是自定义切面在内层,这时若自定义切面没有正确抛出异常(和失效原因二一样了,自己内部try catch了,没有抛异常)

@Service
public class Service3 {

    @Autowired
    private AccountMapper accountMapper;

    @Transactional(rollbackFor = Exception.class)
    public void transfer(int from, int to, int amount) throws FileNotFoundException {
        int fromBalance = accountMapper.findBalanceBy(from);
        if (fromBalance - amount >= 0) {
            accountMapper.update(from, -1 * amount);
            new FileInputStream("aaa");
            accountMapper.update(to, amount);
        }
    }
}
----------------------------------------------------------------------
@Aspect
public class MyAspect {
    @Around("execution(* transfer(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        LoggerUtils.get().debug("log:{}", pjp.getTarget());
        try {
            return pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }
}

解决方案: 1) 和失效原因二一样

      2) 调整切面顺序,在 MyAspect 上添加 @Order(Ordered.LOWEST_PRECEDENCE - 1) (不推荐)  

4. 非 public 方法导致的事务失效

        原因:Spring 为方法创建代理、添加事务通知、前提条件都是该方法是 public 的

        解决方案:1)使用public方法

                          2)添加 bean 配置如下(不推荐)

@Bean
public TransactionAttributeSource transactionAttributeSource() {
    return new AnnotationTransactionAttributeSource(false);
}

5. 父子容器导致的事务失效 

        原因:子容器扫描范围过大,把未加事务配置的 service 扫描进来

        解决方案:1)各扫描各的,不要图简便

                          2)不要用父子容器,所有 bean 放在同一容器

6. 调用本类方法导致传播行为失效

@Service
public class Service6 {

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void foo() throws FileNotFoundException {
        LoggerUtils.get().debug("foo");
        bar();
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void bar() throws FileNotFoundException {
        LoggerUtils.get().debug("bar");
    }
}

        原因:本类方法调用不经过代理,因此无法增强

        解决方案:1)依赖注入自己(代理)来调用

                          2)通过 AopContext 拿到代理对象,来调用

7.1 @Transactional 没有保证原子行为

        原因:事务的原子性仅涵盖 insert、update、delete、select … for update 语句,select 方法并不阻塞

7.2 @Transactional 方法导致的 synchronized 失效

        原因:synchronized 保证的仅是目标方法的原子性,环绕目标方法的还有 commit 等操作,它们并未处于 sync 块内

解决方案:1)synchronized 范围应扩大至代理方法调用 (java层面)

                  2)使用 select … for update 替换 select (数据库层面)

         

你可能感兴趣的:(ssm,spring,java,mybatis)