Spring的7种事务传播机制,PROPAGATION_REQUIRED,NESTED,PROPAGATION_REQUIRES_NEW

16 篇文章 0 订阅
9 篇文章 0 订阅
package xyz.jangle.transaction.test;
/** 
* @author jangle E-mail: jangle@jangle.xyz
* @version 
* 类说明 
*/
public class TransactionDefinition {
	
	/**
	 * 支持当前事务,(如果该方法出现异常,会回滚当前事务的所有操作)。如果不存在事务,则创建事务。
	 * 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;

	/**
	 * 支持当前事务,(如果该方法出现异常,会回滚当前事务的所有操作)。如果不存在事务,则以非事务方式执行。
	 * 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;

	/**
	 * 支持当前事务,如果不存在事务,则跑出异常。(强制要求有事务。)
	 * 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;

	/**
	 * 创建新事务,如果存在当前事务,则挂起当前事务。(实现独立回滚,独立提交。)
	 * 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 javax.transaction.TransactionManager} to be
	 * made available it to it (which is server-specific in standard Java 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;

	/**
	 * 不支持当前事务,始终以非事务方式执行。
	 * 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 javax.transaction.TransactionManager} to be
	 * made available it to it (which is server-specific in standard Java 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;

	/**
	 * 不支持事务,如果存在事务,则抛出异常。
	 * 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_REQUIRES_NEW的区别:嵌套事务由外层事务决定提交,而PROPAGATION_REQUIRES_NEW独立提交。
	 * 与PROPAGATION_REQUIRED的区别:发生异常时,嵌套事务回滚至嵌套事务开始处(即“保存点”),PROPAGATION_REQUIRED没有保存点。
	 * Execute within a nested transaction if a current transaction exists,
	 * behave like {@link #PROPAGATION_REQUIRED} else. 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;

}

 

 

Spring7种事务类型的说明
事务值事务类型事务说明
0PROPAGATION_REQUIRED支持当前事务,(如果该方法出现异常,会回滚当前事务的所有操作)。如果不存在事务,则创建事务。
1PROPAGATION_SUPPORTS支持当前事务,(如果该方法出现异常,会回滚当前事务的所有操作)。如果不存在事务,则以非事务方式执行。
2PROPAGATION_MANDATORY支持当前事务,如果不存在事务,则跑出异常。(强制要求有事务。)
3PROPAGATION_REQUIRES_NEW创建新事务,如果存在当前事务,则挂起当前事务。(实现独立回滚,独立提交。)
4PROPAGATION_NOT_SUPPORTED不支持当前事务,始终以非事务方式执行。
5PROPAGATION_NEVER不支持事务,如果存在事务,则抛出异常。
6PROPAGATION_NESTED     * 如果不存在事务,创建事务。如果存在事务,则嵌套在事务内,嵌套事务依赖外层事务提交,不进行独立事务提交。
     * 嵌套事务如果发生异常,则抛出异常,回滚嵌套事务的操作,回到开始嵌套事务的“保存点”,由外层事务的逻辑继续执行(外层捕获异常并处理即可)。
     * 嵌套事务如果不发生异常,则继续执行,不提交。由外层事务的逻辑继续执行,若外层事务后续发生异常,则回滚包括嵌套事务在内的所有事务。
     * 与PROPAGATION_REQUIRES_NEW的区别:嵌套事务由外层事务决定提交,而PROPAGATION_REQUIRES_NEW独立提交。
     * 与PROPAGATION_REQUIRED的区别:发生异常时,嵌套事务回滚至嵌套事务开始处(即“保存点”),PROPAGATION_REQUIRED没有保存点。

 

 

 

 

 

 

 

 

 

 

 

 

Java的事务隔离级别:https://blog.csdn.net/Bof_jangle/article/details/113603249

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值