spring事务的传播机制定义在 TransactionDefinition 接口中,定义了如下传播类型

PROPAGATION_REQUIRED

支持当前事务,如果没有当前事务就创建一个新的事务,是默认的传播行为。

外围方法未开启事务

内部会创建新事务,若新事务回滚,不影响外围方法。

外围方法开启事务

内部方法会加入到外围方法的事务中,使用同一个事务,不管内外谁发生异常,整个事务都将回滚。

/*** Support a current transaction; create a new one if none exists.* Analogous to the EJB transaction attribute of the same name.* <p>This is typically the default setting of a transaction definition,* and typically defines a transaction synchronization scope.*/int PROPAGATION_REQUIRED = 0;

PROPAGATION_SUPPORTS

支持当前事务,如果没有事务就以非事务方式执行。

外围方法未开启事务

以非事务方法运行。

外围方法开启事务

内部方法会加入到外围方法的事务中,使用同一个事务,不管内外谁发生异常,整个事务都将回滚。

    /*** Support a current transaction; execute non-transactionally if none exists.* Analogous to the EJB transaction attribute of the same name.* <p><b>NOTE:</b> For transaction managers with transaction synchronization,* {@code PROPAGATION_SUPPORTS} is slightly different from no transaction* at all, as it defines a transaction scope that synchronization might apply to.* As a consequence, the same resources (a JDBC {@code Connection}, a* Hibernate {@code Session}, etc) will be shared for the entire specified* scope. Note that the exact behavior depends on the actual synchronization* configuration of the transaction manager!* <p>In general, use {@code PROPAGATION_SUPPORTS} with care! In particular, do* not rely on {@code PROPAGATION_REQUIRED} or {@code PROPAGATION_REQUIRES_NEW}* <i>within</i> a {@code PROPAGATION_SUPPORTS} scope (which may lead to* synchronization conflicts at runtime). If such nesting is unavoidable, make sure* to configure your transaction manager appropriately (typically switching to* "synchronization on actual transaction").* @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization* @see org.springframework.transaction.support.AbstractPlatformTransactionManager#SYNCHRONIZATION_ON_ACTUAL_TRANSACTION*/int PROPAGATION_SUPPORTS = 1;

PROPAGATION_MANDATORY

mandatory是强制的意思,支持当前事务,如果没有事务,就抛出一个异常

外围方法未开启事务

抛出一个异常。

外围方法开启事务

内部方法会加入到外围方法的事务中,使用同一个事务,不管内外谁发生异常,整个事务都将回滚。

 /*** Support a current transaction; throw an exception if no current transaction* exists. Analogous to the EJB transaction attribute of the same name.* <p>Note that transaction synchronization within a {@code PROPAGATION_MANDATORY}* scope will always be driven by the surrounding transaction.*/int PROPAGATION_MANDATORY = 2;

PROPAGATION_REQUIRES_NEW

不管当前是否存在事务,都创建一个新事务,如果存在事务,将当前事务暂停(挂起)

外围方法未开启事务

内部会创建新事务,若内部事务回滚,不影响外围方法。

外围方法开启事务

内部方法依然会单独开启独立事务,且与外部方法事务也独立,内部方法之间、内部方法和外部方法事务均相互独立,互不干扰。

代码验证REQUIRES_NEW事务传播机制

代码验证REQUIRES_NEW事务传播机制https://blog.csdn.net/leisure_life/article/details/124478838

    /*** Create a new transaction, suspending the current transaction if one exists.* Analogous to the EJB transaction attribute of the same name.* <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box* on all transaction managers. This in particular applies to* {@link org.springframework.transaction.jta.JtaTransactionManager},* which requires the {@code jakarta.transaction.TransactionManager} to be* made available it to it (which is server-specific in standard Jakarta EE).* <p>A {@code PROPAGATION_REQUIRES_NEW} scope always defines its own* transaction synchronizations. Existing synchronizations will be suspended* and resumed appropriately.* @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager*/int PROPAGATION_REQUIRES_NEW = 3;

PROPAGATION_NOT_SUPPORTED

不支持事务,不管当前是否存在事务都以非事务方式执行。

外围方法未开启事务

以非事务方式执行。

外围方法开启事务

以非事务方式执行。

    /*** Do not support a current transaction; rather always execute non-transactionally.* Analogous to the EJB transaction attribute of the same name.* <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box* on all transaction managers. This in particular applies to* {@link org.springframework.transaction.jta.JtaTransactionManager},* which requires the {@code jakarta.transaction.TransactionManager} to be* made available it to it (which is server-specific in standard Jakarta EE).* <p>Note that transaction synchronization is <i>not</i> available within a* {@code PROPAGATION_NOT_SUPPORTED} scope. Existing synchronizations* will be suspended and resumed appropriately.* @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager*/int PROPAGATION_NOT_SUPPORTED = 4;

PROPAGATION_NEVER

不支持事务,如果存在事务,会抛出一个异常

外围方法未开启事务

以非事务方式执行。

外围方法开启事务

抛出一个异常。

/*** Do not support a current transaction; throw an exception if a current transaction* exists. Analogous to the EJB transaction attribute of the same name.* <p>Note that transaction synchronization is <i>not</i> available within a* {@code PROPAGATION_NEVER} scope.*/int PROPAGATION_NEVER = 5;

PROPAGATION_NESTED

如果存在当前事务,则在嵌套事务中执行,如果不存在事务,则和PROPAGATION_REQUIRED一样,会新建一个事务。

外围方法未开启事务

与Propagation.REQUIRED 作用相同,内部会创建新事务,若内部事务回滚,不影响外围方法。

外围方法开启事务

修饰的内部方法属于外部事务的子事务,外围主事务回滚,子事务一定回滚,而内部子事务可以单独回滚而不影响外围主事务和其他子事务。

 /*** Execute within a nested transaction if a current transaction exists,* behave like {@link #PROPAGATION_REQUIRED} otherwise. There is no* analogous feature in EJB.* <p><b>NOTE:</b> Actual creation of a nested transaction will only work on* specific transaction managers. Out of the box, this only applies to the JDBC* {@link org.springframework.jdbc.datasource.DataSourceTransactionManager}* when working on a JDBC 3.0 driver. Some JTA providers might support* nested transactions as well.* @see org.springframework.jdbc.datasource.DataSourceTransactionManager*/int PROPAGATION_NESTED = 6;

总结

required,有就加入,没有就自己动手

required_new,不求人,自己动手,丰衣足食,互不干扰

supports,有就加入,没有也无所谓。

not_supports,有我也不用。

never,必须没有,有了就抛异常。

mandatory 与 never是一对反义词,mandatory 是必须共用一个事物,否则就抛异常;而never必须无事务,有事务就抛异常。

nested,有就嵌套,没有就玩自己的,外部影响内部,内部不影响外部。

spring 事务传播机制总结相关推荐

  1. spring上下文是什么意思_Java程序员只会CRUD连Spring事务传播机制都不懂?

    AQS到底有什么用?难道就真的只是为了面试吗? 当然不是说AQS没用,如果你不是做基础架构或者中间件开发,你很难感受到AQS的威力.当然,学习很多时候,需要的是正向反馈,学了太多造火箭的东西,面试完就 ...

  2. 原创 | CRUD更要知道的Spring事务传播机制

    来自:肥朝 AQS到底有什么用?难道就真的只是为了面试吗? 当然不是说AQS没用,如果你不是做基础架构或者中间件开发,你很难感受到AQS的威力.当然,学习很多时候,需要的是正向反馈,学了太多造火箭的东 ...

  3. Spring事务传播机制以及事务嵌套

    Spring事务传播机制以及事务嵌套 Spring事务传播机制 事务嵌套场景 情景0: 场景1:不同类中,开启事务的方法调用没有开启事务的方法 场景2:不同类中,methodA方法嵌套methodB方 ...

  4. Spring事务传播机制和隔离级别

    Spring有5种隔离级别,7种传播行为.这是面试常问的内容,也是代码中经常碰到的知识点.这些知识枯燥而且乏味,其中有些非常的绕.如果栽在这上面,就实在是太可惜了. @Transactional(is ...

  5. Spring事务传播机制大白话(使用springboot,注解演示)

    1. 我对传播机制的理解 为什么需要传播机制? 因为事务之间可能存在相互调用,例如service业务层的方法存在相互调用,如果相互调用的方法都开启了事务(对应到springboot就是在方法上要添加@ ...

  6. Spring 事务传播机制 实例讲解

    事务传播机制 对于SQL事务的概念以及ACID性质,可以参见我的另一篇博文 http://kingj.iteye.com/admin/blogs/1675011 spring的管理的事务可以分为如下2 ...

  7. Spring事务传播机制与隔离机制

    详情查看 https://www.jianshu.com/p/249f2cd42692 转载于:https://www.cnblogs.com/alan319/p/10937089.html

  8. Spring: 事务传播机制

    文章目录 1.美图 2.@Transactional 注解的属性信息 3.案例 3.1 建表 3.2 项目结构 3.3 配置 3.4 config 3.5 实体类 3.5 业务类 3.6 测试类 3. ...

  9. Spring事务与事务传播机制

    目录 1.事务的基本概念 2.Spring事务的实现 3.事务隔离级别 4.事务传播机制 1.事务的基本概念 关于事务的一些基础概念我已经在MYSQL中讲解过了,有不了解的可以移步至此篇文章: MyS ...

  10. 【Spring事务】事务和事务传播机制

    事务 事务主要有三种操作: 开始事务 start transaction 提交事务 commit 回滚事务 rollback Spring 中事务的实现 Spring 中的事务操作分为两类: ⼿动操作 ...

最新文章

  1. linux vi编辑 整理
  2. 2021年春季学期-信号与系统-第十次作业参考答案-第四小题
  3. 【运筹学】线性规划 单纯形法 ( 基矩阵 | 基变量 | 非基矩阵 | 非基变量 | 矩阵分块形式 | 逆矩阵 | 基解 | 基可行解 )
  4. 排优解难 网上邻居常遇故障解决方法
  5. PAT甲级1096 Consecutive Factors :[C++题解]连续的因子、约数
  6. flare3d_clone
  7. g++能过,c++过不了
  8. radiobutton 设置为不能点击_谷歌要求:安卓 11 相机默认不能设置为“美颜”模式...
  9. Java Lock的使用
  10. Membership学习(三)Membership Providers介绍
  11. 学习Java适合参加哪些工作?Java需要掌握的技术
  12. Java 面试——字符串操作、值传递、重载与重写
  13. 主机通过网络访问虚拟机VirtualBox的WEB服务器
  14. JSON.stringify用法
  15. 用人话说说希尔伯特空间??
  16. 美国十大毕业典礼演讲:记着你总会死去……
  17. 高等数学 极限存在 与 极限不存在
  18. 神卓互联是什么?优秀的内网穿透
  19. matplotlib生成没有留白的图片
  20. 发票样板 html+css

热门文章

  1. 安装最好用的计算机软件,装机软件哪个好?教您最好的装机软件推荐
  2. 设计模式-模板方法模式-以简历模板为例
  3. 设计模式——Facade(外观)模式
  4. 命令行安装卸载驱动服务
  5. Win10安装jdk11及环境变量配置
  6. python如何截长图_python 截长图、H5页面截长图
  7. Mysql 数据库(一)—— 初识 Mysql
  8. 通达oa考勤可以代打吗_OA让考勤更方便
  9. 消息队列原理及activeMQ基本知识点
  10. python批量打印word_Python操作Word批量生成文章的方法