Interface TransactionDefinition
- All Known Implementing Classes:
DefaultTransactionDefinition
- the propagation, which says what to do when a transaction is or is
not already active (see
TransactionDefinition.Propagation; the default isTransactionDefinition.Propagation.REQUIRED); - the isolation level requested from the datastore;
- the timeout after which the transaction is rolled back;
- the read-only flag, a hint that the work does not modify data;
- the rollback rule that decides whether an exception thrown by the work causes a rollback, and a name used in logs.
@Transactional annotation, or created with
of(Propagation) and DefaultTransactionDefinition. The
propagation values mirror the transaction attributes of Jakarta EE (REQUIRED, REQUIRES_NEW,
MANDATORY, SUPPORTS, NOT_SUPPORTED, NEVER) plus NESTED.
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_NEW and TransactionDefinition.Propagation.NESTED can 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 flag applies 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 Hibernate Session.
This type is derived from the Spring Framework's TransactionDefinition (Apache License 2.0).
- Since:
- 08.05.2003
- Author:
- Juergen Hoeller, graemerocher
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumIsolation levels.static enumDefines what happens when a transactional unit of work starts while a transaction is, or is not, already active in the current context. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final TransactionDefinitionThe default transaction definition.static final TransactionDefinitionA read only definition.static final DurationUse the default timeout of the underlying transaction system, or none if timeouts are not supported. -
Method Summary
Modifier and TypeMethodDescriptiondefault ConnectionDefinitionIn some cases the transaction can require a new connection or alter the existing connection properties.default @NonNull Collection<Class<? extends Throwable>> Collection of exception classes that shouldn't cause the rollback.default @NonNull Optional<TransactionDefinition.Isolation> Return the isolation level.default @Nullable StringgetName()Return the name of this transaction.default @NonNull TransactionDefinition.PropagationReturn the propagation behavior.Additional transaction properties that may be interpreted by specific transaction managers.default @NonNull Collection<Class<? extends Throwable>> Collection of exception classes that should cause the rollback.Return the transaction timeout.Return whether to optimize as a read-only transaction.static @NonNull TransactionDefinitionCreate a newTransactionDefinitionwith a given name.static @NonNull TransactionDefinitionof(@NonNull TransactionDefinition.Propagation propagationBehaviour) Create a newTransactionDefinitionfor the given behaviour.default booleanCheck of the transaction should roll back when exception occurs.
-
Field Details
-
DEFAULT
The default transaction definition. -
READ_ONLY
A read only definition. -
TIMEOUT_DEFAULT
Use the default timeout of the underlying transaction system, or none if timeouts are not supported.
-
-
Method Details
-
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:
-
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. When participating in an existing transaction, the setting of the existing transaction applies and this one is ignored.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:
-
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
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)
-
getName
Return the name of this transaction. Can benull.The name identifies the transaction in logs and monitoring tools. For methods annotated with
@Transactionalthe name is taken from the annotation'snamemember if present, and is otherwisesimple class name + "." + method name.- Returns:
- the name of this transaction (
nullby default)
-
of
static @NonNull TransactionDefinition of(@NonNull TransactionDefinition.Propagation propagationBehaviour) Create a newTransactionDefinitionfor the given behaviour.- Parameters:
propagationBehaviour- The behaviour- Returns:
- The definition
-
named
Create a newTransactionDefinitionwith a given name.- Parameters:
name- The name- Returns:
- The definition
- Since:
- 3.5.0
-
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
Collection of exception classes that shouldn't cause the rollback.- Returns:
- the exception classes
- Since:
- 3.5.0
-
getProperties
-
rollbackOn
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
-
getConnectionDefinition
In some cases the transaction can require a new connection or alter the existing connection properties.- Returns:
- The connection definition that is required for this transaction.
-