required有则有无则创建(默认)

说明: 如果当前已经存在事务,那么加入该事务,如果不存在事务,创建一个事务,这是默认的传播属性值。

@Service
public class RequireMethodService {@AutowiredUserService userService;@Transactional(propagation = Propagation.REQUIRED)public void methodSon1() {User user = new User();user.setId(1);user.setUserName("A");userService.save(user);String act = TransactionSynchronizationManager.getCurrentTransactionName();System.out.println("Transaction1-----" + act);}@Transactional(propagation = Propagation.REQUIRED)public void methodSon2() {User user = new User();user.setId(2);user.setUserName("B");userService.save(user);String act = TransactionSynchronizationManager.getCurrentTransactionName();System.out.println("Transaction2-----" + act);}}
@Service
public class RequireService {@AutowiredRequireMethodService requireMethodService;@Transactionalpublic void service() {String act = TransactionSynchronizationManager.getCurrentTransactionName();System.out.println("begin-----" + act);requireMethodService.methodSon1();requireMethodService.methodSon2();} }

执行结果:事务是相同的

begin-----com.cn.service.propagation.require.RequireService.service
Transaction1-----com.cn.service.propagation.require.RequireService.service
Transaction2-----com.cn.service.propagation.require.RequireService.service

supports有则有无则无事务

如果当前已经存在事务,那么加入该事务,否则创建一个所谓的空事务(可以认为无事务执行)。

@Service
public class SupportMethodService {@AutowiredUserService userService;/*** 如果当前有事务则加入事务中,如果没有则什么都不做,相当于没事务。*/@Transactional(propagation = Propagation.SUPPORTS)public void methodSon1() {User user = new User();user.setId(1);user.setUserName("A");userService.save(user);throw new RuntimeException("异常");}}
@Service
public class SupportService {@AutowiredSupportMethodService supportMethodService;/*** @Transactional,开启事务后,service会和methodSon1公用一个事务,此时methodSon1会正常回滚;* * 如果这里没有开启,则methodSon1不会自己创建事务。此时methodSon1会不会回滚,数据插入到数据库;* */@Transactionalpublic void service() {supportMethodService.methodSon1();} }

执行结果:
SupportService在调用SupportMethodService的过程中
1. SupportService没有事务,则methodSon1也没有事务,不会回滚,数据插入到数据库。
2. SupportService有事务,则methodSon1加入到事务中,会回滚。

mandatory必须有事务

当前必须存在一个事务,否则抛出异常。

@Service
public class MandatoryMethodService {@AutowiredUserService userService;@Transactional(propagation = Propagation.MANDATORY)public void methodSon1() {User user = new User();user.setId(1);user.setUserName("A");userService.save(user);}@Transactional(propagation = Propagation.MANDATORY)public void methodSon2() {User user = new User();user.setId(2);user.setUserName("B");userService.save(user);}}
@Service
public class MandatoryService {@AutowiredMandatoryMethodService mandatoryMethodService;/*** 当前必须存在一个事务,否则抛出异常。* */
//  @Transactionalpublic void service() {//没有事务mandatoryMethodService.methodSon1();} }

执行结果:

org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:362) ~[spring-tx-5.3.6.jar:5.3.6]at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:595) ~[spring-tx-5.3.6.jar:5.3.6]at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:382) ~[spring-tx-5.3.6.jar:5.3.6]at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.6.jar:5.3.6]at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.6.jar:5.3.6]at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.6.jar:5.3.6]at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692) ~[spring-aop-5.3.6.jar:5.3.6]

never 必须没有事务

如果当前存在事务,则抛出异常

@Service
public class NeverMethodService {@AutowiredUserService userService;@Transactional(propagation = Propagation.REQUIRED)public void methodSon1() {User user = new User();user.setId(1);user.setUserName("A");userService.save(user);}@Transactional(propagation = Propagation.NEVER)public void methodSon2() {User user = new User();user.setId(2);user.setUserName("B");userService.save(user);}}
@Service
public class NeverService {@AutowiredNeverMethodService neverMethodService;/*** 如果当前存在事务,则抛出异常,否则在无事务环境上执行代码。* * 如果调用方法上已经有事务,被调用方法上会报错。*/@Transactionalpublic void service() {//methodSon1创建了事务,但不会影响methodSon2neverMethodService.methodSon1();//methodSon2此时会报错,因为service()上已经开启了事务neverMethodService.methodSon2();} }

执行结果:

org.springframework.transaction.IllegalTransactionStateException: Existing transaction found for transaction marked with propagation 'never'at org.springframework.transaction.support.AbstractPlatformTransactionManager.handleExistingTransaction(AbstractPlatformTransactionManager.java:413) ~[spring-tx-5.3.6.jar:5.3.6]at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:352) ~[spring-tx-5.3.6.jar:5.3.6]at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:595) ~[spring-tx-5.3.6.jar:5.3.6]at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:382) ~[spring-tx-5.3.6.jar:5.3.6]at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.6.jar:5.3.6]at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.6.jar:5.3.6]at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.6.jar:5.3.6]at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692) ~[spring-aop-5.3.6.jar:5.3.6]

requires_new新事务

REQUIRES_NEW新建一个事务,不管当前有没有事务,都新建一个独立的事务。

@Service
public class RequireNewMethodService {@AutowiredUserService userService;@Transactional(propagation = Propagation.REQUIRED)public void methodSon1() {User user = new User();user.setId(1);user.setUserName("A");userService.save(user);String act = TransactionSynchronizationManager.getCurrentTransactionName();System.out.println("Transaction1-----" + act);}@Transactional(propagation = Propagation.REQUIRES_NEW)public void methodSon2() {User user = new User();user.setId(2);user.setUserName("B");userService.save(user);String act = TransactionSynchronizationManager.getCurrentTransactionName();System.out.println("Transaction2-----" + act);}}
@Service
public class RequireNewService {@AutowiredRequireNewMethodService requireNewMethodService;/*** REQUIRES_NEW新建一个事务,不管当前有没有事务,都新建一个独立的事务。*/@Transactionalpublic void service() {String act = TransactionSynchronizationManager.getCurrentTransactionName();System.out.println("begin-----" + act);requireNewMethodService.methodSon1();requireNewMethodService.methodSon2();} }

执行结果:

begin-----com.cn.service.propagation.requiresnew.RequireNewService.service
Transaction1-----com.cn.service.propagation.requiresnew.RequireNewService.service
Transaction2-----com.cn.service.propagation.requiresnew.RequireNewMethodService.methodSon2

nested嵌套事务

1)methodSon2方法内部报错,则只会回滚methodSon2里面的。

2)methodSon2方法内部不报错,但是外面的调用方报错了,则methodSon2会跟着一起回滚。

3)methodSon2方法内部不报错,外面也不报错,则methodSon2和外面事务一起提交。

@Service
public class NestedMethodService {@AutowiredUserService userService;@Transactional(propagation = Propagation.REQUIRED)public void methodSon1() {User user = new User();user.setId(1);user.setUserName("A");userService.save(user);}@Transactional(propagation = Propagation.NESTED)public void methodSon2() {User user = new User();user.setId(2);user.setUserName("B");userService.save(user);throw new RuntimeException("methodSon2异常");}}

@Service
public class NestedService {@AutowiredNestedMethodService nestedMethodService;/*** methodSon2方法内部报错,则只会回滚methodSon2里面的。*/@Transactionalpublic void service() {nestedMethodService.methodSon1();try {nestedMethodService.methodSon2();} catch (Exception e) {e.printStackTrace();}} /*** methodSon1方法内部不报错,但是外面的methodSon2报错了,则methodSon1会跟着一起回滚。*/@Transactionalpublic void service2() {try {nestedMethodService.methodSon1();} catch (Exception e) {e.printStackTrace();}nestedMethodService.methodSon2();} }

总结:

在methodA()调用methodB()情况下

REQUIRED REQUIRES_NEW NESTED
如果methodB()抛出异常,在methodA()中没有try catch住 A、B都回滚 A、B都回滚 A、B都回滚
如果methodB() 抛出异常,在methodA()中try catch住,没有重新抛出 A、B都回滚 A不回滚 B回滚 A不回滚 B回滚
如果methodB() 抛出异常,在methodA()中try catch住,重新抛出 A、B都回滚 A、B都回滚 A、B都回滚
如果只有methodA抛出异常(先执行B,后执行A) A、B都回滚 A回滚 B不回滚 A、B都回滚

@Transactional传播属性相关推荐

  1. Spring事务的处理流程、传播属性、及部分释疑

    目录 文章目录 一.spring事务利用AOP的拦截和处理流程 必须记住的两个要点 二.事务传播属性 三.为什么很多Exception异常必须配置在rollback-for中才有用 四.事务的传播性在 ...

  2. mysql事务传播属性_Spring事务传播属性和隔离级别详细介绍

    1 事务的传播属性(Propagation) 1) REQUIRED ,这个是默认的属性 Support a current transaction, create a new one if none ...

  3. Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别、不可重复读与幻读的区别

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spr ...

  4. spring 事务隔离级别和传播行为_Java工程师面试1000题146-Spring数据库事务传播属性和隔离级别...

    146.简介一下Spring支持的数据库事务传播属性和隔离级别 介绍Spring所支持的事务和传播属性之前,我们先了解一下SpringBean的作用域,与此题无关,仅做一下简单记录. 在Spring中 ...

  5. 事物的级别_实战分析:事务的隔离级别和传播属性

    什么是事务? 要么全部都要执行,要么就都不执行. 事务所具有的四种特性 原子性,一致性,隔离性,持久性 原子性 个人理解,就是事务执行不可分割,要么全部完成,要么全部拉倒不干. 一致性 关于一致性这个 ...

  6. Spring 事务的传播属性

    Spring 事务的传播属性 事务的传播属性: 1.PROPAGATION_REQUIRED * :如果不存在事务则新建事务,若存在事务则加入事务,默认是这个 2.PROPAGATION_SUPPOR ...

  7. Spring事务传播属性有那么难吗?看这一篇就够了

    点击上方"后端技术精选",选择"置顶公众号" 技术文章第一时间送达! 作者:不学无数的程序员 juejin.im/post/5da6eee2f265da5bb9 ...

  8. Spring 事务传播属性有那么难吗?看这一篇就够了!

    尊重原创,原文链接 学习东西要知行合一,如果只是知道理论而没实践过,那么掌握的也不会特别扎实,估计过几天就会忘记,接下来我们一起实践来学习Spring事务的传播属性. 传播属性 传播属性定义的是当一个 ...

  9. 京东电话面:说说你对Spring事务传播属性的理解?

    点击上方蓝色"java大数据修炼之道", 选择"设为星标" 每晚九点: 技术干货 ???? 必定奉上哈喽,各位新来的小伙伴们,大家好!由于公众号做了改版,为了保 ...

最新文章

  1. [Python Study Notes] Python的安装
  2. 剑指offer:字符流中第一个不重复的字符
  3. 机器学习-Random Forest算法简介
  4. 大流行后的数据中心非接触式技术
  5. 人口问题,怎样的生育率才能保持正常的世代更替?
  6. 04-老马jQuery教程-DOM节点操作及位置和大小
  7. android之phonegap入门
  8. Topcoder Asia Programming Competition
  9. [教程]在Windows 2012 R2上安装Intel I217-V/I218-V网卡驱动
  10. Soft-Masked BERT 一种新的中文纠错模型
  11. 基于品类关系,虚拟类目如何建设?
  12. UNIX linux vi命令
  13. linux连接小米随身wifi密码忘记了,小米随身wifi设置教程
  14. 云计算基础(纯理论)
  15. 数字图像处理·自适应滤波器降低噪声
  16. 电脑远程开机以及控制
  17. vs2008简体中文正式版下载
  18. 不同架构cpu上的c语言编译器,关于c ++:检测CPU架构的编译时
  19. 21点 小游戏 java代码_基于Java的21点扑克游戏的实现
  20. 高博十四讲中第六章非线性优化 由于g2o更新出现的问题解决

热门文章

  1. 自己动手证明向量点乘和叉乘的几何意义
  2. 全球及中国中小银行市场竞争态势及项目可行性研究报告2021-2027年
  3. 使用Python和OpenCV实现身份证识别
  4. 页面数据解析之bs4
  5. dokuwiki搭建手顺
  6. python爬虫笔试题目_Python 爬虫笔试(含答案)
  7. 如何在小公司中发挥影响力
  8. 安全测试工具DoS ToolKit
  9. 阿里云发布计算巢加速器
  10. 第十八届全国大学生智能车竞赛山东赛区成绩与奖项