@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
@Transactional
Notes 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
@Transactional
The trigger can be used to specify rollbackFor exception type of transaction rollbacks, you can specify multiple, separated by commas.
rollbackFor
The default value is UncheckedException, including RuntimeException and Error.
When we use directly @Transactional
not specify rollbackFor
when, 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