Annotation Interface Transactional
When this annotation is declared at the class level, it applies as a default to all methods of the declaring class and its subclasses. Note that it does not apply to ancestor classes up the class hierarchy; inherited methods need to be locally redeclared in order to participate in a subclass-level annotation. For details on method visibility constraints, consult the Transaction Management section of the reference manual.
This annotation type is generally directly comparable to Spring's
RuleBasedTransactionAttribute
class, and in fact AnnotationTransactionAttributeSource
will directly
convert the data to the latter class, so that Spring's transaction support code
does not have to know about annotations. If no custom rollback rules apply,
the transaction will roll back on RuntimeException
and Error
but not on checked exceptions.
For specific information about the semantics of this annotation's attributes,
consult the TransactionDefinition
and
TransactionAttribute
javadocs.
This annotation commonly works with thread-bound transactions managed by a
PlatformTransactionManager
, exposing a
transaction to all data access operations within the current execution thread.
Note: This does NOT propagate to newly started threads within the method.
Alternatively, this annotation may demarcate a reactive transaction managed
by a ReactiveTransactionManager
which
uses the Reactor context instead of thread-local variables. As a consequence,
all participating data access operations need to execute within the same
Reactor context in the same reactive pipeline.
- Since:
- 1.2
- Author:
- Colin Sampaleanu, Juergen Hoeller, Sam Brannen, Mark Paluch
- See Also:
-
Optional Element Summary
Modifier and TypeOptional ElementDescriptionThe transaction isolation level.String[]
Defines zero (0) or more transaction labels.String[]
Defines zero (0) or more exception names (for exceptions which must be a subclass ofThrowable
) indicating which exception types must not cause a transaction rollback.The transaction propagation type.boolean
A boolean flag that can be set totrue
if the transaction is effectively read-only, allowing for corresponding optimizations at runtime.String[]
Defines zero (0) or more exception names (for exceptions which must be a subclass ofThrowable
), indicating which exception types must cause a transaction rollback.int
The timeout for this transaction (in seconds).The timeout for this transaction (in seconds).A qualifier value for the specified transaction.Alias fortransactionManager()
.
-
Element Details
-
value
Alias fortransactionManager()
.- See Also:
- Default:
- ""
-
transactionManager
A qualifier value for the specified transaction.May be used to determine the target transaction manager, matching the qualifier value (or the bean name) of a specific
TransactionManager
bean definition.- Since:
- 4.2
- See Also:
- Default:
- ""
-
label
String[] labelDefines zero (0) or more transaction labels.Labels may be used to describe a transaction, and they can be evaluated by individual transaction managers. Labels may serve a solely descriptive purpose or map to pre-defined transaction manager-specific options.
See the documentation of the actual transaction manager implementation for details on how it evaluates transaction labels.
- Since:
- 5.3
- See Also:
- Default:
- {}
-
propagation
Propagation propagationThe transaction propagation type.Defaults to
Propagation.REQUIRED
.- Default:
- REQUIRED
-
isolation
Isolation isolationThe transaction isolation level.Defaults to
Isolation.DEFAULT
.Exclusively designed for use with
Propagation.REQUIRED
orPropagation.REQUIRES_NEW
since 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.- See Also:
- Default:
- DEFAULT
-
timeout
int timeoutThe timeout for this transaction (in seconds).Defaults to the default timeout of the underlying transaction system.
Exclusively designed for use with
Propagation.REQUIRED
orPropagation.REQUIRES_NEW
since it only applies to newly started transactions.- Returns:
- the timeout in seconds
- See Also:
- Default:
- -1
-
timeoutString
String timeoutStringThe timeout for this transaction (in seconds).Defaults to the default timeout of the underlying transaction system.
Exclusively designed for use with
Propagation.REQUIRED
orPropagation.REQUIRES_NEW
since it only applies to newly started transactions.- Returns:
- the timeout in seconds as a String value, e.g. a placeholder
- Since:
- 5.3
- See Also:
- Default:
- ""
-
readOnly
boolean readOnlyA boolean flag that can be set totrue
if the transaction is effectively read-only, allowing for corresponding optimizations at runtime.Defaults to
false
.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 but rather silently ignore the hint.
- See Also:
- Default:
- false
-
rollbackFor
Defines zero (0) or more exceptionclasses
, which must be subclasses ofThrowable
, indicating which exception types must cause a transaction rollback.By default, a transaction will be rolling back on
RuntimeException
andError
but not on checked exceptions (business exceptions). SeeDefaultTransactionAttribute.rollbackOn(Throwable)
for a detailed explanation.This is the preferred way to construct a rollback rule (in contrast to
rollbackForClassName()
), matching the exception class and its subclasses.Similar to
RollbackRuleAttribute(Class clazz)
.- Default:
- {}
-
rollbackForClassName
String[] rollbackForClassNameDefines zero (0) or more exception names (for exceptions which must be a subclass ofThrowable
), indicating which exception types must cause a transaction rollback.This can be a substring of a fully qualified class name, with no wildcard support at present. For example, a value of
"ServletException"
would matchjakarta.servlet.ServletException
and its subclasses.NB: Consider carefully how specific the pattern is and whether to include package information (which isn't mandatory). For example,
"Exception"
will match nearly anything and will probably hide other rules."java.lang.Exception"
would be correct if"Exception"
were meant to define a rule for all checked exceptions. With more unusualException
names such as"BaseBusinessException"
there is no need to use a FQN.Similar to
RollbackRuleAttribute(String exceptionName)
.- Default:
- {}
-
noRollbackFor
Defines zero (0) or more exceptionClasses
, which must be subclasses ofThrowable
, indicating which exception types must not cause a transaction rollback.This is the preferred way to construct a rollback rule (in contrast to
noRollbackForClassName()
), matching the exception class and its subclasses.Similar to
NoRollbackRuleAttribute(Class clazz)
.- Default:
- {}
-
noRollbackForClassName
String[] noRollbackForClassNameDefines zero (0) or more exception names (for exceptions which must be a subclass ofThrowable
) indicating which exception types must not cause a transaction rollback.See the description of
rollbackForClassName()
for further information on how the specified names are treated.Similar to
NoRollbackRuleAttribute(String exceptionName)
.- Default:
- {}
-