spring事务的传播行为
程序员文章站
2022-05-06 21:37:53
...
spring事务总结
spring事务的传播行为
spring事务的传播行为有7个:
- PROPAGATION_REQUIRED:支持当前事务,如果不存在就创建一个
- PROPAGATION_SUPPORTS:支持当前事务,如果不存在就不使用事务
- PROPAGATION_MANDATORY:支持当前事务,如果不存在就抛出异常
- PROPAGATION_REQUIRES_NEW:如果当前存在事务,就将当前事务挂起,新建一个事务
- PROPAGATION_NOT_SUPPORTED:以非事务的方式运行,如果当前存在事务,就将其挂起
- PROPAGATION_NEVER:以非事务的方式运行,如果存在事务,抛出异常
- PROPAGATION_NESTED:如果事务存在,则嵌套事务运行,若事务不存在则与PROPAGATION_REQUIRED的效果相同
数据库信息
PROPAGATION_REQUIRED 运行结果
- 两个方法都被事务修饰
// 测试类
@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一起提交一起回滚
- 方法1没有被事务修饰,方法2被事务修饰
/**
* 只会回滚被事务管理的方法
*
* @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被回滚。若当前不存在事务,会新建一个事务
PROPAGATION_SUPPORTS 运行结果
@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抛出异常后并没有回滚,所以没有使用事务
PROPAGATION_MANDATORY
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没有使用事务,所以抛出了异常
PROPAGATION_REQUIRES_NEW
@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回滚
PROPAGATION_NOT_SUPPORTED
@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被事务管理,发生了回滚
PROPAGATION_NEVER
@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被事务管理,所以运行报错
PROPAGATION_NESTED
并没有成功的实现方法2回滚,方法1成功提交的现象,每次都是两个方法同时回滚。
链接:link
下一篇: Spring事务的传播行为
推荐阅读
-
简单了解Spring中的事务控制
-
基于Spring Cloud Netflix的TCC柔性事务和EDA事件驱动示例
-
浅谈Spring事务传播行为实战
-
Spring的事务机制实例代码
-
JavaScript实现事件的中断传播和行为阻止方法示例
-
Mybaits 源码解析 (十二)----- Mybatis的事务如何被Spring管理?Mybatis和Spring事务中用的Connection是同一个吗?
-
Spring 事务隔离与事务传播的详解与对比
-
spring事务的@Transactional使用事务不生效问题
-
Spring事务的开启原理详解
-
【面试】足够“忽悠”面试官的『Spring事务管理器』源码阅读梳理(建议珍藏)