@Transactional(rollbackFor)

@Transactional notes there are two, one is the spring provided @org.springframework.transaction.annotation.Transactional the other is jdk offer @javax.transaction.Transactional . On the rollback setup, spring provided that rollbackFor , jdk provided that rollbackOn in the methods of use are consistent.
These two notes in my limited testing in performance is the same.

Where to use

@TransactionalNotes can be written on either method can also be written on the class. Written on the class then all public methods of that class will have affairs. Such methods do not require transaction would clearly generate a transaction, so a better approach is marked on the method you want to add a transaction @Transactional.

rollbackFor

@TransactionalThe trigger can be used to specify rollbackFor exception type of transaction rollbacks, you can specify multiple, separated by commas.
rollbackForThe default value is UncheckedException, including RuntimeException and Error.
When we use directly @Transactionalnot specify rollbackForwhen, Exception and its subclasses will not trigger a rollback.

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");

    Test4 ()voidpublic    @Transactional
    do not roll back//
    }
 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);
    }

One thing to note, in an indirect method in the service call does not trigger affairs

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/11832252.html