赞
踩
spring事务的传播行为有7个:
// 测试类
@Test
public void otherAllTransaction() throws MyException {
User user = new User(10, "abcd", "1234567890");
userService.otherAllTransaction(9, user);
}
/** * 如果当前存在事务,则加入当前事务 * @param id * @param user * @throws MyException */ @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) public void otherAllTransaction(Integer id, User user) throws MyException { otherUserService.otherTransactionWithoutException(user); userMapper.deleteUser(id); throw new MyException(-1, "exception..."); } @Transactional(propagation = Propagation.REQUIRED) public void otherTransactionWithoutException(User user) { userMapper.insertUserInfo(user); }
运行结果:
由运行结果可以看到事务2也被回滚,插入失败,所以事务2加入事务1中,与事务1一起提交一起回滚
/** * 只会回滚被事务管理的方法 * * @throws MyException */ @Test public void otherNoTransaction() throws MyException { User user = new User(11, "abcd", "1234567890"); userService.otherNoTransaction(9, user); } public void otherNoTransaction(Integer id, User user) throws MyException { userMapper.deleteUser(id); otherUserService.otherTransaction(user); } @Transactional(rollbackFor = Exception.class) public void otherTransaction(User user) throws MyException { userMapper.insertUserInfo(user); throw new MyException(-1, "exception..."); }
运行结果:
有运行结果可知,方法2被回滚。若当前不存在事务,会新建一个事务
@Test
public void otherNoTransaction() throws MyException {
User user = new User(11, "abcd", "1234567890");
userService.otherNoTransaction(8, user);
}
public void otherNoTransaction(Integer id, User user) throws MyException {
userMapper.deleteUser(id);
otherUserService.otherTransaction(user);
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
public void otherTransaction(User user) throws MyException {
userMapper.insertUserInfo(user);
throw new MyException(-1, "exception...");
}
运行结果:
由运行结果可知,方法2抛出异常后并没有回滚,所以没有使用事务
public void otherNoTransaction(Integer id, User user) throws MyException {
userMapper.deleteUser(id);
otherUserService.otherTransaction(user);
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.MANDATORY)
public void otherTransaction(User user) throws MyException {
userMapper.insertUserInfo(user);
throw new MyException(-1, "exception...");
}
运行结果:
因为方法1没有使用事务,所以抛出了异常
@Test public void otherAllTransaction() throws MyException { User user = new User(10, "abcd", "1234567890"); userService.otherAllTransaction(7, user); } @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) public void otherAllTransaction(Integer id, User user) throws MyException { otherUserService.otherTransactionWithoutException(user); userMapper.deleteUser(id); throw new MyException(-1, "exception..."); } @Transactional(propagation = Propagation.REQUIRES_NEW) public void otherTransactionWithoutException(User user) { userMapper.insertUserInfo(user); }
运行结果:
由运行结果可知,方法2事务成功提交,方法1回滚
@Test public void otherAllTransaction() throws MyException { User user = new User(11, "abcd", "1234567890"); userService.otherAllTransaction(10, user); } @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) public void otherAllTransaction(Integer id, User user) throws MyException { otherUserService.otherTransactionWithoutException(user); userMapper.deleteUser(id); } @Transactional(propagation = Propagation.NOT_SUPPORTED) public void otherTransactionWithoutException(User user) throws MyException { userMapper.insertUserInfo(user); throw new MyException(-1, "exception..."); }
运行结果:
由运行结果可知,方法2没有回滚,所以没有被事务管理,方法1被事务管理,发生了回滚
@Test public void otherAllTransaction() throws MyException { User user = new User(13, "abcd", "1234567890"); userService.otherAllTransaction(10, user); } @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) public void otherAllTransaction(Integer id, User user) throws MyException { otherUserService.otherTransactionWithoutException(user); userMapper.deleteUser(id); } @Transactional(propagation = Propagation.NEVER) public void otherTransactionWithoutException(User user) throws MyException { userMapper.insertUserInfo(user); throw new MyException(-1, "exception..."); }
运行结果:
由于方法1被事务管理,所以运行报错
并没有成功的实现方法2回滚,方法1成功提交的现象,每次都是两个方法同时回滚。
链接:link
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。