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

package xyz.jangle.transaction.test;
/** 
* @author jangle E-mail: [email protected]
* @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.
	 * 

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

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; /** * 支持当前事务,如果不存在事务,则跑出异常。(强制要求有事务。) * 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; /** * 创建新事务,如果存在当前事务,则挂起当前事务。(实现独立回滚,独立提交。) * 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 javax.transaction.TransactionManager} to be * made available it to it (which is server-specific in standard Java 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; /** * 不支持当前事务,始终以非事务方式执行。 * 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 javax.transaction.TransactionManager} to be * made available it to it (which is server-specific in standard Java 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; /** * 不支持事务,如果存在事务,则抛出异常。 * 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_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. *

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; }

 

 

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

 

 

 

 

 

 

 

 

 

 

 

 

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

你可能感兴趣的:(spring,数据库,Java相关,事务,Spring,nested,java)