|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.transaction.support.AbstractPlatformTransactionManager
Abstract base class that allows for easy implementation of concrete platform transaction managers like JtaTransactionManager and DataSourceTransactionManager.
This base class provides the following workflow handling:
Subclasses have to implement specific template methods for specific states of a transaction, for example begin, suspend, resume, commit, rollback. The most important of them are abstract and must be provided by a concrete implementation; for the rest, defaults are provided, so overriding is optional.
Transaction synchronization is a generic mechanism for registering callbacks that get invoked at transaction completion time. This is mainly used internally by the data access support classes for JDBC, Hibernate, and JDO when running within a JTA transaction: They register resources that are opened within the transaction for closing at transaction completion time, allowing e.g. for reuse of the same Hibernate Session within the transaction. The same mechanism can also be leveraged for custom synchronization needs in an application.
The state of this class is serializable, to allow for serializing the
transaction strategy along with proxies that carry a transaction interceptor.
It is up to subclasses if they wish to make their state to be serializable too.
They should implement the java.io.Serializable
marker interface in
that case, and potentially a private readObject()
method (according
to Java serialization rules) if they need to restore any transient state.
setTransactionSynchronization(int)
,
TransactionSynchronizationManager
,
JtaTransactionManager
,
DataSourceTransactionManager
,
HibernateTransactionManager
,
JdoTransactionManager
Field Summary | |
protected Log |
logger
Transient to optimize serialization |
static int |
SYNCHRONIZATION_ALWAYS
Always activate transaction synchronization, even for "empty" transactions that result from PROPAGATION_SUPPORTS with no existing backend transaction. |
static int |
SYNCHRONIZATION_NEVER
Never active transaction synchronization. |
static int |
SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
Activate transaction synchronization only for actual transactions, i.e. not for empty ones that result from PROPAGATION_SUPPORTS with no existing backend transaction. |
Constructor Summary | |
AbstractPlatformTransactionManager()
|
Method Summary | |
void |
commit(TransactionStatus status)
This implementation of commit handles participating in existing transactions and programmatic rollback requests. |
protected abstract void |
doBegin(Object transaction,
TransactionDefinition definition)
Begin a new transaction with the given transaction definition. |
protected void |
doCleanupAfterCompletion(Object transaction)
Cleanup resources after transaction completion. |
protected abstract void |
doCommit(DefaultTransactionStatus status)
Perform an actual commit of the given transaction. |
protected abstract Object |
doGetTransaction()
Return a transaction object for the current transaction state. |
protected void |
doResume(Object transaction,
Object suspendedResources)
Resume the resources of the current transaction. |
protected abstract void |
doRollback(DefaultTransactionStatus status)
Perform an actual rollback of the given transaction. |
protected void |
doSetRollbackOnly(DefaultTransactionStatus status)
Set the given transaction rollback-only. |
protected Object |
doSuspend(Object transaction)
Suspend the resources of the current transaction. |
TransactionStatus |
getTransaction(TransactionDefinition definition)
This implementation of getTransaction handles propagation behavior. |
int |
getTransactionSynchronization()
Return if this transaction manager should activate the thread-bound transaction synchronization support. |
protected boolean |
isExistingTransaction(Object transaction)
Check if the given transaction object indicates an existing transaction (that is, a transaction which has already started). |
boolean |
isNestedTransactionAllowed()
Return whether nested transactions are allowed. |
boolean |
isRollbackOnCommitFailure()
Return if a rollback should be performed on failure of the commit call. |
void |
rollback(TransactionStatus status)
This implementation of rollback handles participating in existing transactions. |
void |
setNestedTransactionAllowed(boolean nestedTransactionAllowed)
Set whether nested transactions are allowed. |
void |
setRollbackOnCommitFailure(boolean rollbackOnCommitFailure)
Set if doRollback should be performed on failure of the doCommit call. |
void |
setTransactionSynchronization(int transactionSynchronization)
Set when this transaction manager should activate the thread-bound transaction synchronization support. |
void |
setTransactionSynchronizationName(String constantName)
Set the transaction synchronization by the name of the corresponding constant in this class, e.g. |
protected boolean |
useSavepointForNestedTransaction()
Return whether to use a savepoint for a nested transaction. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int SYNCHRONIZATION_ALWAYS
public static final int SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
public static final int SYNCHRONIZATION_NEVER
protected transient Log logger
Constructor Detail |
public AbstractPlatformTransactionManager()
Method Detail |
public void setTransactionSynchronizationName(String constantName)
constantName
- name of the constantSYNCHRONIZATION_ALWAYS
public void setTransactionSynchronization(int transactionSynchronization)
Note that transaction synchronization isn't supported for multiple concurrent transactions by different transaction managers. Only one transaction manager is allowed to activate it at any time.
SYNCHRONIZATION_ALWAYS
,
SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
,
SYNCHRONIZATION_NEVER
,
TransactionSynchronizationManager
,
TransactionSynchronization
public int getTransactionSynchronization()
public void setNestedTransactionAllowed(boolean nestedTransactionAllowed)
Typically initialized with an appropriate default by the concrete transaction manager subclass.
public boolean isNestedTransactionAllowed()
public void setRollbackOnCommitFailure(boolean rollbackOnCommitFailure)
doCommit(org.springframework.transaction.support.DefaultTransactionStatus)
,
doRollback(org.springframework.transaction.support.DefaultTransactionStatus)
public boolean isRollbackOnCommitFailure()
public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException
getTransaction
in interface PlatformTransactionManager
definition
- TransactionDefinition instance (can be null for defaults),
describing propagation behavior, isolation level, timeout etc.
TransactionException
- in case of lookup, creation, or system errorsdoGetTransaction()
,
isExistingTransaction(java.lang.Object)
,
doBegin(java.lang.Object, org.springframework.transaction.TransactionDefinition)
public final void commit(TransactionStatus status) throws TransactionException
commit
in interface PlatformTransactionManager
status
- object returned by the getTransaction
method
TransactionException
- in case of commit or system errorsTransactionStatus.isRollbackOnly()
,
doCommit(org.springframework.transaction.support.DefaultTransactionStatus)
,
rollback(org.springframework.transaction.TransactionStatus)
public final void rollback(TransactionStatus status) throws TransactionException
rollback
in interface PlatformTransactionManager
status
- object returned by the getTransaction
method
TransactionException
- in case of system errorsdoRollback(org.springframework.transaction.support.DefaultTransactionStatus)
,
doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus)
protected abstract Object doGetTransaction() throws TransactionException
The returned object will usually be specific to the concrete transaction manager implementation, carrying corresponding transaction state in a modifiable fashion. This object will be passed into the other template methods (e.g. doBegin and doCommit), either directly or as part of a DefaultTransactionStatus instance.
The returned object should contain information about any existing
transaction, that is, a transaction that has already started before the
current getTransaction
call on the transaction manager.
Consequently, a doGetTransaction
implementation will usually
look for an existing transaction and store corresponding state in the
returned transaction object.
CannotCreateTransactionException
- if transaction support is not available
TransactionException
- in case of lookup or system errorsdoBegin(java.lang.Object, org.springframework.transaction.TransactionDefinition)
,
doCommit(org.springframework.transaction.support.DefaultTransactionStatus)
,
doRollback(org.springframework.transaction.support.DefaultTransactionStatus)
,
DefaultTransactionStatus.getTransaction()
protected boolean isExistingTransaction(Object transaction) throws TransactionException
The result will be evaluated according to the specified propagation behavior for the new transaction. An existing transaction might get suspended (in case of PROPAGATION_REQUIRES_NEW), or the new transaction might participate in the existing one (in case of PROPAGATION_REQUIRED).
Default implementation returns false, assuming that detection of or participating in existing transactions is generally not supported. Subclasses are of course encouraged to provide such support.
transaction
- transaction object returned by doGetTransaction
TransactionException
- in case of system errorsdoGetTransaction()
protected boolean useSavepointForNestedTransaction()
Subclasses can override this to return false, causing a further invocation
of doBegin
despite an already existing transaction.
DefaultTransactionStatus.createAndHoldSavepoint()
,
DefaultTransactionStatus.rollbackToHeldSavepoint()
,
DefaultTransactionStatus.releaseHeldSavepoint()
,
doBegin(java.lang.Object, org.springframework.transaction.TransactionDefinition)
protected abstract void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException
transaction
- transaction object returned by doGetTransactiondefinition
- TransactionDefinition instance, describing
propagation behavior, isolation level, timeout etc.
TransactionException
- in case of creation or system errorsprotected Object doSuspend(Object transaction) throws TransactionException
Default implementation throws a TransactionSuspensionNotSupportedException, assuming that transaction suspension is generally not supported.
transaction
- transaction object returned by doGetTransaction
TransactionSuspensionNotSupportedException
- if suspending is not supported by the transaction manager implementation
TransactionException
- in case of system errorsdoResume(java.lang.Object, java.lang.Object)
protected void doResume(Object transaction, Object suspendedResources) throws TransactionException
Default implementation throws a TransactionSuspensionNotSupportedException, assuming that transaction suspension is generally not supported.
transaction
- transaction object returned by doGetTransactionsuspendedResources
- the object that holds suspended resources,
as returned by doSuspend
TransactionSuspensionNotSupportedException
- if resuming is not supported by the transaction manager implementation
TransactionException
- in case of system errorsdoSuspend(java.lang.Object)
protected abstract void doCommit(DefaultTransactionStatus status) throws TransactionException
An implementation does not need to check the "new transaction" flag or the rollback-only flag; this will already have been handled before. Usually, a straight commit will be performed on the transaction object contained in the passed-in status.
status
- the status representation of the transaction
TransactionException
- in case of commit or system errorsDefaultTransactionStatus.getTransaction()
protected abstract void doRollback(DefaultTransactionStatus status) throws TransactionException
An implementation does not need to check the "new transaction" flag; this will already have been handled before. Usually, a straight rollback will be performed on the transaction object contained in the passed-in status.
status
- the status representation of the transaction
TransactionException
- in case of system errorsDefaultTransactionStatus.getTransaction()
protected void doSetRollbackOnly(DefaultTransactionStatus status) throws TransactionException
Default implementation throws an IllegalTransactionStateException, assuming that participating in existing transactions is generally not supported. Subclasses are of course encouraged to provide such support.
status
- the status representation of the transaction
TransactionException
- in case of system errorsprotected void doCleanupAfterCompletion(Object transaction)
doCommit
and doRollback
execution,
on any outcome. Default implementation does nothing.
Should not throw any exceptions but just issue warnings on errors.
transaction
- transaction object returned by doGetTransaction
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |