接口 TransactionDefinition
- 所有已知实现类:
DefaultTransactionAttribute,DefaultTransactionDefinition,DelegatingTransactionAttribute,DelegatingTransactionDefinition,RuleBasedTransactionAttribute,TransactionTemplate
public interface TransactionDefinition
Interface that defines Spring-compliant transaction properties. Based on the propagation behavior definitions analogous to EJB CMT attributes.Note that isolation level and timeout settings will not get applied unless an actual new transaction gets started. As only
PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEWandPROPAGATION_NESTEDcan cause that, it usually doesn't make sense to specify those settings in other cases. Furthermore, be aware that not all transaction managers will support those advanced features and thus might throw corresponding exceptions when given non-default values.The
read-only flagapplies to any transaction context, whether backed by an actual resource transaction or operating non-transactionally at the resource level. In the latter case, the flag will only apply to managed resources within the application, such as a HibernateSession.- 从以下版本开始:
- 08.05.2003
- 作者:
- Juergen Hoeller
- 另请参阅:
PlatformTransactionManager.getTransaction(TransactionDefinition),DefaultTransactionDefinition,TransactionAttribute
字段概要
字段 修饰符和类型 字段 说明 static intISOLATION_DEFAULTUse the default isolation level of the underlying datastore.static intISOLATION_READ_COMMITTEDIndicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.static intISOLATION_READ_UNCOMMITTEDIndicates that dirty reads, non-repeatable reads and phantom reads can occur.static intISOLATION_REPEATABLE_READIndicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.static intISOLATION_SERIALIZABLEIndicates that dirty reads, non-repeatable reads and phantom reads are prevented.static intPROPAGATION_MANDATORYSupport a current transaction; throw an exception if no current transaction exists.static intPROPAGATION_NESTEDExecute within a nested transaction if a current transaction exists, behave likePROPAGATION_REQUIREDotherwise.static intPROPAGATION_NEVERDo not support a current transaction; throw an exception if a current transaction exists.static intPROPAGATION_NOT_SUPPORTEDDo not support a current transaction; rather always execute non-transactionally.static intPROPAGATION_REQUIREDSupport a current transaction; create a new one if none exists.static intPROPAGATION_REQUIRES_NEWCreate a new transaction, suspending the current transaction if one exists.static intPROPAGATION_SUPPORTSSupport a current transaction; execute non-transactionally if none exists.static intTIMEOUT_DEFAULTUse the default timeout of the underlying transaction system, or none if timeouts are not supported.
方法概要
所有方法 静态方法 实例方法 默认方法 修饰符和类型 方法 说明 default intgetIsolationLevel()Return the isolation level.default StringgetName()Return the name of this transaction.default intgetPropagationBehavior()Return the propagation behavior.default intgetTimeout()Return the transaction timeout.default booleanisReadOnly()Return whether to optimize as a read-only transaction.static TransactionDefinitionwithDefaults()Return an unmodifiableTransactionDefinitionwith defaults.
字段详细资料
PROPAGATION_REQUIRED
static final int 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.
- 另请参阅:
- 常量字段值
PROPAGATION_SUPPORTS
static final int 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,
PROPAGATION_SUPPORTSis 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 JDBCConnection, a HibernateSession, 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
PROPAGATION_SUPPORTSwith care! In particular, do not rely onPROPAGATION_REQUIREDorPROPAGATION_REQUIRES_NEWwithin aPROPAGATION_SUPPORTSscope (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_MANDATORY
static final int PROPAGATION_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
PROPAGATION_MANDATORYscope will always be driven by the surrounding transaction.- 另请参阅:
- 常量字段值
PROPAGATION_REQUIRES_NEW
static final int PROPAGATION_REQUIRES_NEW
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
JtaTransactionManager, which requires thejavax.transaction.TransactionManagerto be made available it to it (which is server-specific in standard Java EE).A
PROPAGATION_REQUIRES_NEWscope always defines its own transaction synchronizations. Existing synchronizations will be suspended and resumed appropriately.
PROPAGATION_NOT_SUPPORTED
static final int 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
JtaTransactionManager, which requires thejavax.transaction.TransactionManagerto be made available it to it (which is server-specific in standard Java EE).Note that transaction synchronization is not available within a
PROPAGATION_NOT_SUPPORTEDscope. Existing synchronizations will be suspended and resumed appropriately.
PROPAGATION_NEVER
static final int 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
PROPAGATION_NEVERscope.- 另请参阅:
- 常量字段值
PROPAGATION_NESTED
static final int PROPAGATION_NESTED
Execute within a nested transaction if a current transaction exists, behave likePROPAGATION_REQUIREDotherwise. 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
DataSourceTransactionManagerwhen working on a JDBC 3.0 driver. Some JTA providers might support nested transactions as well.
ISOLATION_DEFAULT
static final int ISOLATION_DEFAULT
Use the default isolation level of the underlying datastore. All other levels correspond to the JDBC isolation levels.- 另请参阅:
Connection, 常量字段值
ISOLATION_READ_UNCOMMITTED
static final int ISOLATION_READ_UNCOMMITTED
Indicates that dirty reads, non-repeatable reads and phantom reads can occur.This level allows a row changed by one transaction to be read by another transaction before any changes in that row have been committed (a "dirty read"). If any of the changes are rolled back, the second transaction will have retrieved an invalid row.
ISOLATION_READ_COMMITTED
static final int ISOLATION_READ_COMMITTED
Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.This level only prohibits a transaction from reading a row with uncommitted changes in it.
ISOLATION_REPEATABLE_READ
static final int ISOLATION_REPEATABLE_READ
Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.This level prohibits a transaction from reading a row with uncommitted changes in it, and it also prohibits the situation where one transaction reads a row, a second transaction alters the row, and the first transaction re-reads the row, getting different values the second time (a "non-repeatable read").
ISOLATION_SERIALIZABLE
static final int ISOLATION_SERIALIZABLE
Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.This level includes the prohibitions in
ISOLATION_REPEATABLE_READand further prohibits the situation where one transaction reads all rows that satisfy aWHEREcondition, a second transaction inserts a row that satisfies thatWHEREcondition, and the first transaction re-reads for the same condition, retrieving the additional "phantom" row in the second read.
TIMEOUT_DEFAULT
static final int TIMEOUT_DEFAULT
Use the default timeout of the underlying transaction system, or none if timeouts are not supported.- 另请参阅:
- 常量字段值
方法详细资料
getPropagationBehavior
default int getPropagationBehavior()
Return the propagation behavior.Must return one of the
PROPAGATION_XXXconstants defined onthis interface.The default is
PROPAGATION_REQUIRED.- 返回:
- the propagation behavior
- 另请参阅:
PROPAGATION_REQUIRED,TransactionSynchronizationManager.isActualTransactionActive()
getIsolationLevel
default int getIsolationLevel()
Return the isolation level.Must return one of the
ISOLATION_XXXconstants defined onthis interface. Those constants are designed to match the values of the same constants onConnection.Exclusively designed for use with
PROPAGATION_REQUIREDorPROPAGATION_REQUIRES_NEWsince it only applies to newly started transactions. Consider switching the "validateExistingTransactions" flag to "true" on your transaction manager if you'd like isolation level declarations to get rejected when participating in an existing transaction with a different isolation level.The default is
ISOLATION_DEFAULT. Note that a transaction manager that does not support custom isolation levels will throw an exception when given any other level thanISOLATION_DEFAULT.- 返回:
- the isolation level
- 另请参阅:
ISOLATION_DEFAULT,AbstractPlatformTransactionManager.setValidateExistingTransaction(boolean)
getTimeout
default int getTimeout()
Return the transaction timeout.Must return a number of seconds, or
TIMEOUT_DEFAULT.Exclusively designed for use with
PROPAGATION_REQUIREDorPROPAGATION_REQUIRES_NEWsince it only applies to newly started transactions.Note that a transaction manager that does not support timeouts will throw an exception when given any other timeout than
TIMEOUT_DEFAULT.The default is
TIMEOUT_DEFAULT.- 返回:
- the transaction timeout
isReadOnly
default boolean isReadOnly()
Return whether to optimize as a read-only transaction.The read-only flag applies to any transaction context, whether backed by an actual resource transaction (
PROPAGATION_REQUIRED/PROPAGATION_REQUIRES_NEW) or operating non-transactionally at the resource level (PROPAGATION_SUPPORTS). In the latter case, the flag will only apply to managed resources within the application, such as a HibernateSession.This just serves as a hint for the actual transaction subsystem; it will not necessarily cause failure of write access attempts. A transaction manager which cannot interpret the read-only hint will not throw an exception when asked for a read-only transaction.
- 返回:
trueif the transaction is to be optimized as read-only (falseby default)- 另请参阅:
TransactionSynchronization.beforeCommit(boolean),TransactionSynchronizationManager.isCurrentTransactionReadOnly()
getName
@Nullable default String getName()
Return the name of this transaction. Can benull.This will be used as the transaction name to be shown in a transaction monitor, if applicable (for example, WebLogic's).
In case of Spring's declarative transactions, the exposed name will be the
fully-qualified class name + "." + method name(by default).- 返回:
- the name of this transaction (
nullby default} - 另请参阅:
TransactionAspectSupport,TransactionSynchronizationManager.getCurrentTransactionName()
withDefaults
static TransactionDefinition withDefaults()
Return an unmodifiableTransactionDefinitionwith defaults.For customization purposes, use the modifiable
DefaultTransactionDefinitioninstead.- 从以下版本开始:
- 5.2