Class DefaultTransactionDefinition
- java.lang.Object
-
- io.micronaut.transaction.support.DefaultTransactionDefinition
-
- All Implemented Interfaces:
TransactionDefinition
,java.io.Serializable
- Direct Known Subclasses:
DefaultTransactionAttribute
public class DefaultTransactionDefinition extends java.lang.Object implements TransactionDefinition, java.io.Serializable
Default implementation of theTransactionDefinition
interface, offering bean-style configuration and sensible default values (PROPAGATION_REQUIRED, ISOLATION_DEFAULT, TIMEOUT_DEFAULT, readOnly=false).- Since:
- 08.05.2003
- Author:
- Juergen Hoeller
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.micronaut.transaction.TransactionDefinition
TransactionDefinition.Isolation, TransactionDefinition.Propagation
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
PREFIX_ISOLATION
Deprecated.not used field will be removed in the future major versionstatic java.lang.String
PREFIX_PROPAGATION
Deprecated.not used field will be removed in the future major versionstatic java.lang.String
PREFIX_TIMEOUT
Deprecated.not used field will be removed in the future major versionstatic java.lang.String
READ_ONLY_MARKER
Deprecated.not used field will be removed in the future major version-
Fields inherited from interface io.micronaut.transaction.TransactionDefinition
DEFAULT, READ_ONLY, TIMEOUT_DEFAULT
-
-
Constructor Summary
Constructors Constructor Description DefaultTransactionDefinition()
Create a new DefaultTransactionDefinition, with default settings.DefaultTransactionDefinition(TransactionDefinition other)
Copy constructor.DefaultTransactionDefinition(TransactionDefinition.Propagation propagationBehavior)
Create a new DefaultTransactionDefinition with the given propagation behavior.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object other)
This implementation compares thetoString()
results.java.util.Collection<java.lang.Class<? extends java.lang.Throwable>>
getDontRollbackOn()
Collection of exception classes that shouldn't cause the rollback.TransactionDefinition.Isolation
getIsolationLevel()
Return the isolation level.java.lang.String
getName()
Return the name of this transaction.TransactionDefinition.Propagation
getPropagationBehavior()
Return the propagation behavior.java.util.Collection<java.lang.Class<? extends java.lang.Throwable>>
getRollbackOn()
Collection of exception classes that should cause the rollback.java.time.Duration
getTimeout()
Return the transaction timeout.int
hashCode()
This implementation returnstoString()
's hash code.boolean
isReadOnly()
Return whether to optimize as a read-only transaction.void
setDontRollbackOn(java.util.Collection<java.lang.Class<? extends java.lang.Throwable>> dontRollbackOn)
Set exception classes which shouldn't cause the rollback.void
setIsolationLevel(TransactionDefinition.Isolation isolationLevel)
Set the isolation level.void
setName(java.lang.String name)
Set the name of this transaction.void
setPropagationBehavior(TransactionDefinition.Propagation propagationBehavior)
Set the propagation behavior.void
setReadOnly(boolean readOnly)
Set whether to optimize as read-only transaction.void
setRollbackOn(java.util.Collection<java.lang.Class<? extends java.lang.Throwable>> rollbackOn)
Set exception classes which should cause the rollback.void
setTimeout(java.time.Duration timeout)
Set the timeout to apply, as number of seconds.java.lang.String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.micronaut.transaction.TransactionDefinition
rollbackOn
-
-
-
-
Field Detail
-
PREFIX_PROPAGATION
@Deprecated public static final java.lang.String PREFIX_PROPAGATION
Deprecated.not used field will be removed in the future major versionPrefix for the propagation constants defined in TransactionDefinition.- See Also:
- Constant Field Values
-
PREFIX_ISOLATION
@Deprecated public static final java.lang.String PREFIX_ISOLATION
Deprecated.not used field will be removed in the future major versionPrefix for the isolation constants defined in TransactionDefinition.- See Also:
- Constant Field Values
-
PREFIX_TIMEOUT
@Deprecated public static final java.lang.String PREFIX_TIMEOUT
Deprecated.not used field will be removed in the future major versionPrefix for transaction timeout values in description strings.- See Also:
- Constant Field Values
-
READ_ONLY_MARKER
@Deprecated public static final java.lang.String READ_ONLY_MARKER
Deprecated.not used field will be removed in the future major versionMarker for read-only transactions in description strings.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DefaultTransactionDefinition
public DefaultTransactionDefinition()
Create a new DefaultTransactionDefinition, with default settings. Can be modified through bean property setters.
-
DefaultTransactionDefinition
public DefaultTransactionDefinition(TransactionDefinition other)
Copy constructor. Definition can be modified through bean property setters.- Parameters:
other
- The other definition- See Also:
setPropagationBehavior(io.micronaut.transaction.TransactionDefinition.Propagation)
,setIsolationLevel(io.micronaut.transaction.TransactionDefinition.Isolation)
,setTimeout(java.time.Duration)
,setReadOnly(boolean)
,setName(java.lang.String)
-
DefaultTransactionDefinition
public DefaultTransactionDefinition(@NonNull TransactionDefinition.Propagation propagationBehavior)
Create a new DefaultTransactionDefinition with the given propagation behavior. Can be modified through bean property setters.- Parameters:
propagationBehavior
- one of the propagation constants in the TransactionDefinition interface- See Also:
setIsolationLevel(io.micronaut.transaction.TransactionDefinition.Isolation)
,setTimeout(java.time.Duration)
,setReadOnly(boolean)
-
-
Method Detail
-
setPropagationBehavior
public final void setPropagationBehavior(@NonNull TransactionDefinition.Propagation propagationBehavior)
Set the propagation behavior. Must be one of the propagation constants in the TransactionDefinition interface. Default is PROPAGATION_REQUIRED.Exclusively designed for use with
TransactionDefinition.Propagation.REQUIRED
orTransactionDefinition.Propagation.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.Note that a transaction manager that does not support custom isolation levels will throw an exception when given any other level than
TransactionDefinition.Isolation.DEFAULT
.- Parameters:
propagationBehavior
- The propagation behaviour to use.- Throws:
java.lang.IllegalArgumentException
- if the supplied value is not one of thePROPAGATION_
constants- See Also:
TransactionDefinition.Propagation.REQUIRED
-
getPropagationBehavior
@NonNull public final TransactionDefinition.Propagation getPropagationBehavior()
Description copied from interface:TransactionDefinition
Return the propagation behavior.Must return one of the
PROPAGATION_XXX
constants defined onthis interface
.The default is
TransactionDefinition.Propagation.REQUIRED
.- Specified by:
getPropagationBehavior
in interfaceTransactionDefinition
- Returns:
- the propagation behavior
- See Also:
TransactionDefinition.Propagation.REQUIRED
-
setIsolationLevel
public final void setIsolationLevel(@NonNull TransactionDefinition.Isolation isolationLevel)
Set the isolation level. Must be one of the isolation constants in the TransactionDefinition interface. Default is ISOLATION_DEFAULT.Exclusively designed for use with
TransactionDefinition.Propagation.REQUIRED
orTransactionDefinition.Propagation.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.Note that a transaction manager that does not support custom isolation levels will throw an exception when given any other level than
TransactionDefinition.Isolation.DEFAULT
.- Parameters:
isolationLevel
- The isolation level- Throws:
java.lang.IllegalArgumentException
- if the supplied value is not one of theISOLATION_
constants- See Also:
TransactionDefinition.Isolation.DEFAULT
-
getIsolationLevel
@NonNull public final TransactionDefinition.Isolation getIsolationLevel()
Description copied from interface:TransactionDefinition
Return the isolation level.Must return one of the
ISOLATION_XXX
constants defined onthis interface
. Those constants are designed to match the values of the same constants onConnection
.Exclusively designed for use with
TransactionDefinition.Propagation.REQUIRED
orTransactionDefinition.Propagation.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.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
.- Specified by:
getIsolationLevel
in interfaceTransactionDefinition
- Returns:
- the isolation level
- See Also:
TransactionDefinition.Isolation.DEFAULT
-
setTimeout
public final void setTimeout(@NonNull java.time.Duration timeout)
Set the timeout to apply, as number of seconds. Default is TIMEOUT_DEFAULT (-1).Exclusively designed for use with
TransactionDefinition.Propagation.REQUIRED
orTransactionDefinition.Propagation.REQUIRES_NEW
since 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
TransactionDefinition.TIMEOUT_DEFAULT
.- Parameters:
timeout
- The timeout to use- See Also:
TransactionDefinition.TIMEOUT_DEFAULT
-
getTimeout
@NonNull public final java.time.Duration getTimeout()
Description copied from interface:TransactionDefinition
Return the transaction timeout.Must return a number of seconds, or
TransactionDefinition.TIMEOUT_DEFAULT
.Exclusively designed for use with
TransactionDefinition.Propagation.REQUIRED
orTransactionDefinition.Propagation.REQUIRES_NEW
since 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
TransactionDefinition.TIMEOUT_DEFAULT
.The default is
TransactionDefinition.TIMEOUT_DEFAULT
.- Specified by:
getTimeout
in interfaceTransactionDefinition
- Returns:
- the transaction timeout
-
setReadOnly
public final void setReadOnly(boolean readOnly)
Set whether to optimize as read-only transaction. Default is "false".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.
- Parameters:
readOnly
- Whether the transaction is read only
-
isReadOnly
public final boolean isReadOnly()
Description copied from interface:TransactionDefinition
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.
- Specified by:
isReadOnly
in interfaceTransactionDefinition
- Returns:
true
if the transaction is to be optimized as read-only (false
by default)- See Also:
TransactionSynchronization.beforeCommit(boolean)
,TransactionSynchronizationManager.isCurrentTransactionReadOnly()
-
setName
public final void setName(java.lang.String name)
Set the name of this transaction. Default is none.This will be used as transaction name to be shown in a transaction monitor, if applicable (for example, WebLogic's).
- Parameters:
name
- The name of the transaction
-
getName
@Nullable public final java.lang.String getName()
Description copied from interface:TransactionDefinition
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).- Specified by:
getName
in interfaceTransactionDefinition
- Returns:
- the name of this transaction (
null
by default} - See Also:
TransactionSynchronizationManager.getCurrentTransactionName()
-
setRollbackOn
public void setRollbackOn(@Nullable java.util.Collection<java.lang.Class<? extends java.lang.Throwable>> rollbackOn)
Set exception classes which should cause the rollback. Empty if all should cause it.- Parameters:
rollbackOn
- The exception classes- Since:
- 3.5.0
-
setDontRollbackOn
public void setDontRollbackOn(@Nullable java.util.Collection<java.lang.Class<? extends java.lang.Throwable>> dontRollbackOn)
Set exception classes which shouldn't cause the rollback.- Parameters:
dontRollbackOn
- The exception classes- Since:
- 3.5.0
-
getRollbackOn
public java.util.Collection<java.lang.Class<? extends java.lang.Throwable>> getRollbackOn()
Description copied from interface:TransactionDefinition
Collection of exception classes that should cause the rollback. Empty if all exception should cause the rollback.- Specified by:
getRollbackOn
in interfaceTransactionDefinition
- Returns:
- the exception classes
-
getDontRollbackOn
public java.util.Collection<java.lang.Class<? extends java.lang.Throwable>> getDontRollbackOn()
Description copied from interface:TransactionDefinition
Collection of exception classes that shouldn't cause the rollback.- Specified by:
getDontRollbackOn
in interfaceTransactionDefinition
- Returns:
- the exception classes
-
equals
public boolean equals(@Nullable java.lang.Object other)
This implementation compares thetoString()
results.- Overrides:
equals
in classjava.lang.Object
- See Also:
toString()
-
hashCode
public int hashCode()
This implementation returnstoString()
's hash code.- Overrides:
hashCode
in classjava.lang.Object
- See Also:
toString()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-