Spring事务的传播行为

Spring事务的传播行为(Propagation Behavior)在接口TransactionDefinition中定义,共七种传播行为。

PROPAGATION_REQUIRED

org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRED
org.springframework.transaction.annotation.Propagation.REQUIRED

Support a current transaction; create a new one if none exists. Analogous to the EJB transaction attribute of the same name.

支持当前的事务,如果不存在则创建一个新事务。类似于同名的EJB事务属性。

This is typically the default setting of a transaction definition, and typically defines a transaction synchronization scope.

这是事务定义的默认设置,并且定义了事务同步作用域。

PROPAGATION_SUPPORTS

org.springframework.transaction.TransactionDefinition.PROPAGATION_SUPPORTS
org.springframework.transaction.annotation.Propagation.SUPPORTS

Support a current transaction; execute non-transactionally if none exists. Analogous to the EJB transaction attribute of the same name.

支持当前的事务,如果不存在则以非事务的方式执行。类似于同名的EJB事务属性。

NOTE: For transaction managers with transaction synchronization, 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 Connection, a Hibernate 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!

注意:对于带有事务同步的事务管理器,PROPAGATION_SUPPORTS与没有事务稍有不同,因为它定义了同步可能会应用到的事务作用域。因而,对于整个指定的作用域,相同的资源(JDBC Connection、Hibernate Session等)将是共享的。注意,确切的行为依赖于事务管理器实际的同步配置。

In general, use PROPAGATION_SUPPORTS with care! In particular, do not rely on PROPAGATION_REQUIRED or PROPAGATION_REQUIRES_NEW within a 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").

一般,使用PROPAGATION_SUPPORTS要谨慎!尤其是,在PROPAGATION_SUPPORTS作用域内不要依赖PROPAGATION_REQUIRED或PROPAGATION_REQUIRES_NEW,这可能会导致运行时事务同步冲突。如果这种嵌套是不可避免的,要确保恰当地配置事务管理器(通常切换到"synchronization on actual transaction")。

PROPAGATION_MANDATORY

org.springframework.transaction.TransactionDefinition.PROPAGATION_MANDATORY
org.springframework.transaction.annotation.Propagation.MANDATORY

Support a current transaction; throw an exception if no current transaction exists. Analogous to the EJB transaction attribute of the same name.

支持当前的事务,如果不存在则抛出异常。类似于同名的EJB事务属性。

Note that transaction synchronization within a PROPAGATION_MANDATORY scope will always be driven by the surrounding transaction.

注意:在PROPAGATION_MANDATORY作用域内的事务同步总是由外围的事务驱动。

PROPAGATION_REQUIRES_NEW

org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRES_NEW
org.springframework.transaction.annotation.Propagation.REQUIRES_NEW

Create a new transaction, suspending the current transaction if one exists. Analogous to the EJB transaction attribute of the same name.

创建新事务,如果存在当前事务则挂起。类似于同名的EJB事务属性。

NOTE: Actual transaction suspension will not work out-of-the-box on all transaction managers. This in particular applies to org.springframework.transaction.jta.JtaTransactionManager, which requires the javax.transaction.TransactionManager to be made available it to it (which is server-specific in standard J2EE).

注意:实际的事务挂起不是对所有的事务管理器都是开箱即用。尤其是应用到JtaTransactionManager,它要求javax.transaction.TransactionManager来使这个可用。

A PROPAGATION_REQUIRES_NEW scope always defines its own transaction synchronizations. Existing synchronizations will be suspended and resumed appropriately.

PROPAGATION_REQUIRES_NEW作用域总是定义自己的事务同步。已存在的同步将被恰当地挂起和恢复。

PROPAGATION_NOT_SUPPORTED

org.springframework.transaction.TransactionDefinition.PROPAGATION_NOT_SUPPORTED
org.springframework.transaction.annotation.Propagation.NOT_SUPPORTED

Do not support a current transaction; rather always execute non-transactionally. Analogous to the EJB transaction attribute of the same name.

不支持当前事务,总是以非事务方式执行。类似于同名的EJB事务属性。

NOTE: Actual transaction suspension will not work out-of-the-box on all transaction managers. This in particular applies to org.springframework.transaction.jta.JtaTransactionManager, which requires the javax.transaction.TransactionManager to be made available it to it (which is server-specific in standard J2EE).

注意:实际的事务挂起不是对所有的事务管理器都是开箱即用。尤其是应用到JtaTransactionManager,它要求javax.transaction.TransactionManager来使这个可用。

Note that transaction synchronization is not available within a PROPAGATION_NOT_SUPPORTED scope. Existing synchronizations will be suspended and resumed appropriately.

在PROPAGATION_NOT_SUPPORTED作用域内,事务同步是不可用的。已存在的同步将被恰当地挂起和恢复。

PROPAGATION_NEVER

org.springframework.transaction.TransactionDefinition.PROPAGATION_NEVER
org.springframework.transaction.annotation.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.

不支持当前事务,如果存在当前事务则抛出异常。类似于同名的EJB事务属性。

Note that transaction synchronization is not available within a PROPAGATION_NEVER scope.

注意:在PROPAGATION_NEVER作用域内,事务同步是不可用的。

PROPAGATION_NESTED

org.springframework.transaction.TransactionDefinition.PROPAGATION_NESTED
org.springframework.transaction.annotation.Propagation.NESTED

Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.

如果存在当前事务,则在嵌套事务中执行,行为像PROPAGATION_REQUIRED。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 org.springframework.jdbc.datasource.DataSourceTransactionManager when working on a JDBC 3.0 driver. Some JTA providers might support nested transactions as well.

注意:嵌套事务的实际创建仅在特定事务管理器上起作用。仅当在JDBC 3.0驱动上工作时,应用于JDBC DataSourceTransactionManager。一些JTA提供商可能也支持嵌套事务。

总结

TransactionDefinition接口 Propagation枚举 描述
PROPAGATION_REQUIRED REQUIRED 支持当前的事务,如果不存在则创建一个新事务。
PROPAGATION_SUPPORTS SUPPORTS 支持当前的事务,如果不存在则以非事务的方式执行。
PROPAGATION_MANDATORY MANDATORY 支持当前的事务,如果不存在则抛出异常。
PROPAGATION_REQUIRES_NEW REQUIRES_NEW 创建新事务,如果存在当前事务则挂起。
PROPAGATION_NOT_SUPPORTED NOT_SUPPORTED 不支持当前事务,总是以非事务方式执行。
PROPAGATION_NEVER NEVER 不支持当前事务,如果存在当前事务则抛出异常。
PROPAGATION_NESTED NESTED 如果存在当前事务,则在嵌套事务中执行。

猜你喜欢

转载自gitzhangyl.iteye.com/blog/2304239