public class DefaultTransactionDefinition extends Object implements TransactionDefinition, Serializable
TransactionDefinition
interface,
offering bean-style configuration and sensible default values
(PROPAGATION_REQUIRED, ISOLATION_DEFAULT, TIMEOUT_DEFAULT, readOnly=false).
Base class for both TransactionTemplate
and
DefaultTransactionAttribute
.
Modifier and Type | Field and Description |
---|---|
static String |
PREFIX_ISOLATION
Prefix for the isolation constants defined in TransactionDefinition.
|
static String |
PREFIX_PROPAGATION
Prefix for the propagation constants defined in TransactionDefinition.
|
static String |
PREFIX_TIMEOUT
Prefix for transaction timeout values in description strings.
|
static String |
READ_ONLY_MARKER
Marker for read-only transactions in description strings.
|
ISOLATION_DEFAULT, ISOLATION_READ_COMMITTED, ISOLATION_READ_UNCOMMITTED, ISOLATION_REPEATABLE_READ, ISOLATION_SERIALIZABLE, PROPAGATION_MANDATORY, PROPAGATION_NESTED, PROPAGATION_NEVER, PROPAGATION_NOT_SUPPORTED, PROPAGATION_REQUIRED, PROPAGATION_REQUIRES_NEW, PROPAGATION_SUPPORTS, TIMEOUT_DEFAULT
Constructor and Description |
---|
DefaultTransactionDefinition()
Create a new
DefaultTransactionDefinition with default settings. |
DefaultTransactionDefinition(int propagationBehavior)
Create a new
DefaultTransactionDefinition with the given
propagation behavior. |
DefaultTransactionDefinition(TransactionDefinition other)
Copy constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other)
This implementation compares the
toString() results. |
protected StringBuilder |
getDefinitionDescription()
Return an identifying description for this transaction definition.
|
int |
getIsolationLevel()
Return the isolation level.
|
String |
getName()
Return the name of this transaction.
|
int |
getPropagationBehavior()
Return the propagation behavior.
|
int |
getTimeout()
Return the transaction timeout.
|
int |
hashCode()
This implementation returns
toString() 's hash code. |
boolean |
isReadOnly()
Return whether to optimize as a read-only transaction.
|
void |
setIsolationLevel(int isolationLevel)
Set the isolation level.
|
void |
setIsolationLevelName(String constantName)
Set the isolation level by the name of the corresponding constant in
TransactionDefinition, e.g.
|
void |
setName(String name)
Set the name of this transaction.
|
void |
setPropagationBehavior(int propagationBehavior)
Set the propagation behavior.
|
void |
setPropagationBehaviorName(String constantName)
Set the propagation behavior by the name of the corresponding constant in
TransactionDefinition, e.g.
|
void |
setReadOnly(boolean readOnly)
Set whether to optimize as read-only transaction.
|
void |
setTimeout(int timeout)
Set the timeout to apply, as number of seconds.
|
String |
toString()
Return an identifying description for this transaction definition.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
withDefaults
public static final String PREFIX_PROPAGATION
public static final String PREFIX_ISOLATION
public static final String PREFIX_TIMEOUT
public static final String READ_ONLY_MARKER
public DefaultTransactionDefinition()
DefaultTransactionDefinition
with default settings.
Can be modified through bean property setters.public DefaultTransactionDefinition(TransactionDefinition other)
public DefaultTransactionDefinition(int propagationBehavior)
DefaultTransactionDefinition
with the given
propagation behavior. Can be modified through bean property setters.propagationBehavior
- one of the propagation constants in the
TransactionDefinition interfacesetIsolationLevel(int)
,
setTimeout(int)
,
setReadOnly(boolean)
public final void setPropagationBehaviorName(String constantName) throws IllegalArgumentException
constantName
- name of the constantIllegalArgumentException
- if the supplied value is not resolvable
to one of the PROPAGATION_
constants or is null
setPropagationBehavior(int)
,
TransactionDefinition.PROPAGATION_REQUIRED
public final void setPropagationBehavior(int propagationBehavior)
Exclusively designed for use with TransactionDefinition.PROPAGATION_REQUIRED
or
TransactionDefinition.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
.
IllegalArgumentException
- if the supplied value is not one of the
PROPAGATION_
constantsTransactionDefinition.PROPAGATION_REQUIRED
public final int getPropagationBehavior()
TransactionDefinition
Must return one of the PROPAGATION_XXX
constants
defined on this interface
.
The default is TransactionDefinition.PROPAGATION_REQUIRED
.
getPropagationBehavior
in interface TransactionDefinition
TransactionDefinition.PROPAGATION_REQUIRED
,
TransactionSynchronizationManager.isActualTransactionActive()
public final void setIsolationLevelName(String constantName) throws IllegalArgumentException
constantName
- name of the constantIllegalArgumentException
- if the supplied value is not resolvable
to one of the ISOLATION_
constants or is null
setIsolationLevel(int)
,
TransactionDefinition.ISOLATION_DEFAULT
public final void setIsolationLevel(int isolationLevel)
Exclusively designed for use with TransactionDefinition.PROPAGATION_REQUIRED
or
TransactionDefinition.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
.
IllegalArgumentException
- if the supplied value is not one of the
ISOLATION_
constantsTransactionDefinition.ISOLATION_DEFAULT
public final int getIsolationLevel()
TransactionDefinition
Must return one of the ISOLATION_XXX
constants defined on
this interface
. Those constants are designed
to match the values of the same constants on Connection
.
Exclusively designed for use with TransactionDefinition.PROPAGATION_REQUIRED
or
TransactionDefinition.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 than TransactionDefinition.ISOLATION_DEFAULT
.
getIsolationLevel
in interface TransactionDefinition
TransactionDefinition.ISOLATION_DEFAULT
,
AbstractPlatformTransactionManager.setValidateExistingTransaction(boolean)
public final void setTimeout(int timeout)
Exclusively designed for use with TransactionDefinition.PROPAGATION_REQUIRED
or
TransactionDefinition.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
.
TransactionDefinition.TIMEOUT_DEFAULT
public final int getTimeout()
TransactionDefinition
Must return a number of seconds, or TransactionDefinition.TIMEOUT_DEFAULT
.
Exclusively designed for use with TransactionDefinition.PROPAGATION_REQUIRED
or
TransactionDefinition.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
.
getTimeout
in interface TransactionDefinition
public final void setReadOnly(boolean readOnly)
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 Hibernate Session
.
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.
public final boolean isReadOnly()
TransactionDefinition
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 Hibernate Session
.
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.
isReadOnly
in interface TransactionDefinition
true
if the transaction is to be optimized as read-only
(false
by default)TransactionSynchronization.beforeCommit(boolean)
,
TransactionSynchronizationManager.isCurrentTransactionReadOnly()
public final void setName(String name)
This will be used as transaction name to be shown in a transaction monitor, if applicable (for example, WebLogic's).
@Nullable public final String getName()
TransactionDefinition
null
.
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).
getName
in interface TransactionDefinition
null
by default}TransactionAspectSupport
,
TransactionSynchronizationManager.getCurrentTransactionName()
public boolean equals(@Nullable Object other)
toString()
results.equals
in class Object
toString()
public int hashCode()
toString()
's hash code.hashCode
in class Object
toString()
public String toString()
The format matches the one used by
TransactionAttributeEditor
,
to be able to feed toString
results into bean properties of type
TransactionAttribute
.
Has to be overridden in subclasses for correct equals
and hashCode
behavior. Alternatively, equals(java.lang.Object)
and hashCode()
can be overridden themselves.
toString
in class Object
getDefinitionDescription()
,
TransactionAttributeEditor
protected final StringBuilder getDefinitionDescription()
Available to subclasses, for inclusion in their toString()
result.