Springboot事务使用与回滚

Springboot中事务的使用:

1、启动类加上@EnableTransactionManagement注解,开启事务支持(其实默认是开启的)。

2、在使用事务的public(只有public支持事务)方法(或者类-相当于该类的所有public方法都使用)加上@Transactional注解。

在实际使用中一般是在service中使用@Transactional,那么对于controller->service流程中:

如果controller未开启事务,service中开始了事务,service成功执行,controller在之后的运行中出现异常(错误),不会自动回滚。

也就是说,只有在开启事务的方法中出现异常(默认只有非检测性异常才生效-RuntimeException )(错误-Error)才会自动回滚。

 如果想要对抛出的任何异常都进行自动回滚(而不是只针对RuntimeException),只需要在使用@Transactional(rollbackFor = Exception.class)即可

开启事务的方法中事务回滚的情况:

①未发现的异常,程序运行过程中自动抛出RuntimeException或者其子类,程序终止,自动回滚。

②使用TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();进行手动回滚。

注意:如果在try-catch语句中对可能出现的异常(RuntimeException)进行了处理,没有再手动throw异常,spring认为该方法成功执行,不会进行回滚,此时需要调用②中方法进行手动回滚,如下图:

        //不会自动回滚
        try{
            throw new RuntimeException();
        }catch(RuntimeException e){
            e.printStackTrace();
        }finally{
        }
        //会自动回滚
        try{
            throw new RuntimeException();
        }catch(RuntimeException e){
            e.printStackTrace();
            throw new RuntimeException();
        }finally{
        }

猜你喜欢

转载自www.cnblogs.com/ZTPX/p/10566498.html