Annotation Type Transactional
@Target({METHOD,TYPE}) @Retention(RUNTIME) @Inherited @Documented public @interface Transactional
Describes a transaction attribute on an individual method or on a class.At the class level, this annotation 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; methods need to be locally redeclared in order to participate in a subclass-level annotation.
This annotation type is generally directly comparable to Spring's
RuleBasedTransactionAttributeclass, and in factAnnotationTransactionAttributeSourcewill directly convert the data to the latter class, so that Spring's transaction support code does not have to know about annotations. If no rules are relevant to the exception, it will be treated likeDefaultTransactionAttribute(rolling back onRuntimeExceptionandErrorbut not on checked exceptions).For specific information about the semantics of this annotation's attributes, consult the
TransactionDefinitionandTransactionAttributejavadocs.- Since:
- 1.2
- Author:
- Colin Sampaleanu, Juergen Hoeller, Sam Brannen
- See Also:
TransactionAttribute,DefaultTransactionAttribute,RuleBasedTransactionAttribute
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description IsolationisolationThe transaction isolation level.Class<? extends Throwable>[]noRollbackForString[]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.PropagationpropagationThe transaction propagation type.booleanreadOnlyA boolean flag that can be set totrueif the transaction is effectively read-only, allowing for corresponding optimizations at runtime.Class<? extends Throwable>[]rollbackForString[]rollbackForClassNameDefines zero (0) or more exception names (for exceptions which must be a subclass ofThrowable), indicating which exception types must cause a transaction rollback.inttimeoutThe timeout for this transaction (in seconds).StringtransactionManagerA qualifier value for the specified transaction.StringvalueAlias fortransactionManager().
Element Detail
value
@AliasFor("transactionManager") String value
Alias fortransactionManager().- See Also:
transactionManager()
- Default:
- ""
transactionManager
@AliasFor("value") String 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
PlatformTransactionManagerbean definition.- Since:
- 4.2
- See Also:
value()
- Default:
- ""
propagation
Propagation propagation
The transaction propagation type.Defaults to
Propagation.REQUIRED.- Default:
- org.springframework.transaction.annotation.Propagation.REQUIRED
isolation
Isolation isolation
The transaction isolation level.Defaults to
Isolation.DEFAULT.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.- See Also:
TransactionDefinition.getIsolationLevel(),AbstractPlatformTransactionManager.setValidateExistingTransaction(boolean)
- Default:
- org.springframework.transaction.annotation.Isolation.DEFAULT
timeout
int timeout
The timeout for this transaction (in seconds).Defaults to the default timeout of the underlying transaction system.
Exclusively designed for use with
Propagation.REQUIREDorPropagation.REQUIRES_NEWsince it only applies to newly started transactions.- See Also:
TransactionDefinition.getTimeout()
- Default:
- -1
readOnly
boolean readOnly
A boolean flag that can be set totrueif 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:
TransactionDefinition.isReadOnly(),TransactionSynchronizationManager.isCurrentTransactionReadOnly()
- Default:
- false
rollbackFor
Class<? extends Throwable>[] 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
RuntimeExceptionandErrorbut 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[] rollbackForClassName
Defines 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 matchjavax.servlet.ServletExceptionand 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 unusualExceptionnames such as"BaseBusinessException"there is no need to use a FQN.Similar to
RollbackRuleAttribute(String exceptionName).- Default:
- {}
noRollbackFor
Class<? extends Throwable>[] 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[] noRollbackForClassName
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.See the description of
rollbackForClassName()for further information on how the specified names are treated.Similar to
NoRollbackRuleAttribute(String exceptionName).- Default:
- {}