spring 事务传播机制总结

spring 事务传播机制总结spring 事务的传播机制定义在 TransactionD 接口中 定义了如下传播类型 PROPAGATION REQUIRED 支持当前事务 如果没有当前事务就创建一个新的事务 是默认的传播行为 外围方法未开启事务内部会创建新事务 若新事务回滚 不影响外围方法 外围方法开启事务内部方法会加入到外围方法的事务中 使用同一个事务 不管内外谁发生异常 整个事务都将回滚 Supportacurr createa

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. * 

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. * 

NOTE: 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! *

In general, use {@code PROPAGATION_SUPPORTS} with care! In particular, do * not rely on {@code PROPAGATION_REQUIRED} or {@code PROPAGATION_REQUIRES_NEW} * within 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. * 

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事务传播机制icon-default.png?t=M3K6https://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. * 

NOTE: 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). *

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. * 

NOTE: 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). *

Note that transaction synchronization is not 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. * 

Note that transaction synchronization is not 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. * 

NOTE: 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,有就嵌套,没有就玩自己的,外部影响内部,内部不影响外部。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/229126.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月16日 下午5:25
下一篇 2026年3月16日 下午5:25


相关推荐

  • js nextSibling属性和previousSibling属性

    js nextSibling属性和previousSibling属性   1:nextSibling属性       该属性表示当前节点的下一个节点(其后的节点与当前节点同属一个级别);如果其后没有与其同级的节点,则返回null。      需要特别注意的是:该属性在不同的浏览器中的执行结果并不都相同,见下面例示:     先来看一个例子: [javascript] viewplain copy<body>  <div>  <…

    2022年7月15日
    20
  • 15. PARTITIONS「建议收藏」

    15. PARTITIONS「建议收藏」15.PARTITIONSPARTITIONS表提供有关表分区的信息。此表中的每一行对应于分区表的单个分区或子分区。有关分区表的更多信息,请参见分区。PARTITIONS表有以下列:TABLE_CATALOG:表所属目录的名称。该值始终为def。TABLE_SCHEMA:表所属schema(databas…

    2022年4月19日
    44
  • 为什么当程序员?来听听漂亮国程序员的理由

    为什么当程序员?来听听漂亮国程序员的理由看看在国外当程序员的理由,与我们国内有什么不同!

    2022年5月15日
    37
  • Gradle下载慢解决方案

    Gradle下载慢解决方案参考:阿里云公共代理库首先我们来看看google官方的依赖方式:buildscript{repositories{mavenCentral()jcenter()google()}dependencies{classpath’com.android.tools.build:gradle:3.0.1′}}allprojects{repositories{

    2022年6月15日
    29
  • 任务管理器进程中多个chrome.exe的问题

    任务管理器进程中多个chrome.exe的问题

    2021年12月8日
    156
  • 各种加密证书

    各种加密证书证书相关知识 PFX 文件属于数字证书 pfx 数字证书既包含有公钥又包含私钥 cer 数字证书只包含公钥 参考 JKS JavaKeyStore 就是利用 JavaKeytool 工具生成的 Keystore 文件 JKS 文件由公钥和密钥构成 其中的公钥就是我们所说的证书 即 cer 为后缀的文件 而私钥就是密钥 即以 key 为后缀的文件 JKS

    2026年3月19日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号