Interface TransactionDefinition
-
- All Known Subinterfaces:
TransactionAttribute
- All Known Implementing Classes:
DefaultTransactionAttribute,DefaultTransactionDefinition
public interface TransactionDefinitionNOTICE: This is a fork of Spring'sPlatformTransactionManagermodernizing it to use enums, Slf4j and decoupling from Spring. 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
TransactionDefinition.Propagation.REQUIRED,TransactionDefinition.Propagation.REQUIRES_NEWandTransactionDefinition.Propagation.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.- Since:
- 08.05.2003
- Author:
- Juergen Hoeller, graemerocher
- See Also:
SynchronousTransactionManager.getTransaction(TransactionDefinition)
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classTransactionDefinition.IsolationIsolation levels.static classTransactionDefinition.PropagationPossible propagation values.
-
Field Summary
Fields Modifier and Type Field Description static TransactionDefinitionDEFAULTThe default transaction definition.static TransactionDefinitionREAD_ONLYA read only definition.static java.time.DurationTIMEOUT_DEFAULTUse the default timeout of the underlying transaction system, or none if timeouts are not supported.
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description default java.util.Collection<java.lang.Class<? extends java.lang.Throwable>>getDontRollbackOn()Collection of exception classes that shouldn't cause the rollback.default TransactionDefinition.IsolationgetIsolationLevel()Return the isolation level.default java.lang.StringgetName()Return the name of this transaction.default TransactionDefinition.PropagationgetPropagationBehavior()Return the propagation behavior.default java.util.Collection<java.lang.Class<? extends java.lang.Throwable>>getRollbackOn()Collection of exception classes that should cause the rollback.default java.time.DurationgetTimeout()Return the transaction timeout.default booleanisReadOnly()Return whether to optimize as a read-only transaction.static TransactionDefinitionnamed(java.lang.String name)Create a newTransactionDefinitionwith a given name.static TransactionDefinitionof(TransactionDefinition.Propagation propagationBehaviour)Create a newTransactionDefinitionfor the given behaviour.default booleanrollbackOn(java.lang.Throwable e)Check of the transaction should roll back when exception occurs.
-
-
-
Field Detail
-
DEFAULT
static final TransactionDefinition DEFAULT
The default transaction definition.
-
READ_ONLY
static final TransactionDefinition READ_ONLY
A read only definition.
-
TIMEOUT_DEFAULT
static final java.time.Duration TIMEOUT_DEFAULT
Use the default timeout of the underlying transaction system, or none if timeouts are not supported.
-
-
Method Detail
-
getPropagationBehavior
@NonNull default TransactionDefinition.Propagation getPropagationBehavior()
Return the propagation behavior.Must return one of the
PROPAGATION_XXXconstants defined onthis interface.The default is
TransactionDefinition.Propagation.REQUIRED.- Returns:
- the propagation behavior
- See Also:
TransactionDefinition.Propagation.REQUIRED
-
getIsolationLevel
@NonNull default TransactionDefinition.Isolation 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
TransactionDefinition.Propagation.REQUIREDorTransactionDefinition.Propagation.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
TransactionDefinition.Isolation.DEFAULT. Note that a transaction manager that does not support custom isolation levels will throw an exception when given any other level thanTransactionDefinition.Isolation.DEFAULT.- Returns:
- the isolation level
- See Also:
TransactionDefinition.Isolation.DEFAULT
-
getTimeout
@NonNull default java.time.Duration getTimeout()
Return the transaction timeout.Must return a number of seconds, or
TIMEOUT_DEFAULT.Exclusively designed for use with
TransactionDefinition.Propagation.REQUIREDorTransactionDefinition.Propagation.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.- Returns:
- 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 (
TransactionDefinition.Propagation.REQUIRED/TransactionDefinition.Propagation.REQUIRES_NEW) or operating non-transactionally at the resource level (TransactionDefinition.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.
- Returns:
trueif the transaction is to be optimized as read-only (falseby default)- See Also:
TransactionSynchronization.beforeCommit(boolean),TransactionSynchronizationManager.isCurrentTransactionReadOnly()
-
getName
@Nullable default java.lang.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).- Returns:
- the name of this transaction (
nullby default} - See Also:
TransactionSynchronizationManager.getCurrentTransactionName()
-
of
@NonNull static TransactionDefinition of(@NonNull TransactionDefinition.Propagation propagationBehaviour)
Create a newTransactionDefinitionfor the given behaviour.- Parameters:
propagationBehaviour- The behaviour- Returns:
- The definition
-
named
@NonNull static TransactionDefinition named(@NonNull java.lang.String name)
Create a newTransactionDefinitionwith a given name.- Parameters:
name- The name- Returns:
- The definition
- Since:
- 3.5.0
-
getRollbackOn
@NonNull default java.util.Collection<java.lang.Class<? extends java.lang.Throwable>> getRollbackOn()
Collection of exception classes that should cause the rollback. Empty if all exception should cause the rollback.- Returns:
- the exception classes
- Since:
- 3.5.0
-
getDontRollbackOn
@NonNull default java.util.Collection<java.lang.Class<? extends java.lang.Throwable>> getDontRollbackOn()
Collection of exception classes that shouldn't cause the rollback.- Returns:
- the exception classes
- Since:
- 3.5.0
-
rollbackOn
default boolean rollbackOn(java.lang.Throwable e)
Check of the transaction should roll back when exception occurs.- Parameters:
e- The exception- Returns:
- true if the transaction should roll back
- Since:
- 3.5.0
-
-