首页 > 其他 > 详细

@Transactional(rollbackFor)

时间:2019-11-10 23:56:35      阅读:120      评论:0      收藏:0      [点我收藏+]
@Transactional注解有两个,一个是spring提供的@org.springframework.transaction.annotation.Transactional 另一个是jdk提供@javax.transaction.Transactional。在回滚的设置上,spring提供的是rollbackFor,jdk提供的是rollbackOn,在使用方法上是一致的。
这两个注解在我有限的测试里表现是一样的。

用在哪里

@Transactional注解既可以写在方法上也可以写在类上。写在类上则这个类的所有public方法都将会有事务。这样显然会使一些不需要事务的方法产生事务,所以更好的做法是在想要添加事务的方法上标注@Transactional

rollbackFor

@Transactional的rollbackFor用于指定能够触发事务回滚的异常类型,可以指定多个,用逗号分隔。
rollbackFor默认值为UncheckedException,包括了RuntimeException和Error.
当我们直接使用@Transactional不指定rollbackFor时,Exception及其子类都不会触发回滚。

public class BusinessException extends Exception {
    public BusinessException(String msg) {
        super(msg);
    }
}
@Autowired
    private UserRepository userRepository;
    
    // 不回滚
    @Transactional
    public void test1() throws Exception {
        User user = new User(1, "15000000000", "d243ewa", "Comma");
        saveUser(user);
        throw new Exception("test1 error");
    }

    // 不回滚
    @Transactional
    public void test11() throws Exception {
        User user = new User(1, "15000000000", "d243ewa", "Comma");
        saveUser(user);
        throw new BusinessException("test11 error");
    }

    // 回滚
    @Transactional(rollbackOn = Exception.class)
    public void test2() throws Exception {
        User user = new User(1, "15000000000", "d243ewa", "Comma");
        saveUser(user);
        throw new Exception("test2 error");
    }

    // 回滚
    @Transactional(rollbackOn = Exception.class)
    public void test21() throws Exception {
        User user = new User(1, "15000000000", "d243ewa", "Comma");
        saveUser(user);
        throw new BusinessException("test21 error");
    }

    // 回滚
    @Transactional
    public void test3() {
        User user = new User(1, "15000000000", "d243ewa", "Comma");
        saveUser(user);
        throw new RuntimeException("test3 runtime error");
    }

    // 不回滚
    @Transactional
    public void test4() throws Exception {
        User user = new User(1, "15000000000", "d243ewa", "Comma");
        test41(user);
        throw new Exception("test4 error");
    }

    @Transactional(rollbackOn = Exception.class)
    public void test41(User user) {
        saveUser(user);
    }

    // 不回滚
    public void test5() throws BusinessException {
        test6();
    }

    // 回滚
    @Transactional(rollbackOn = Exception.class)
    public void test6() throws BusinessException {
        User user = new User(1, "15000000000", "d243ewa", "Comma");
        saveUser(user);
        throw new BusinessException("test6 error");
    }

    // 回滚
    @Transactional(rollbackOn = Exception.class)
    public void test7() throws BusinessException {
        test8();
    }

    public void test8() throws BusinessException {
        User user = new User(1, "15000000000", "d243ewa", "Comma");
        saveUser(user);
        throw new BusinessException("test8 error");
    }

    public User saveUser(User user) {
        return userRepository.save(user);
    }

有一点需要注意,在service中间接调用的方法不会触发事务

@Transactional(rollbackFor)

原文:https://www.cnblogs.com/qingmuchuanqi48/p/11832252.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!