Top Description Inners Fields Constructors Methods
org.springframework.transaction.support

public abstract Class AbstractPlatformTransactionManager

extends Object
implements PlatformTransactionManager, ConfigurableTransactionManager, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.springframework.transaction.ConfigurableTransactionManager, org.springframework.transaction.TransactionManager, org.springframework.transaction.PlatformTransactionManager
Known Direct Subclasses
org.springframework.transaction.jta.JtaTransactionManager
Annotations
@SuppressWarnings:serial
Imports
java.io.IOException, .ObjectInputStream, .Serializable, java.util.ArrayList, .Collection, .List, .Map, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.transaction.ConfigurableTransactionManager, .IllegalTransactionStateException, .InvalidTimeoutException, .NestedTransactionNotSupportedException, .PlatformTransactionManager, .TransactionDefinition, .TransactionException, .TransactionExecutionListener, .TransactionStatus, .TransactionSuspensionNotSupportedException, .UnexpectedRollbackException, org.springframework.util.Assert

Abstract base class that implements Spring's standard transaction workflow, serving as basis for concrete platform transaction managers like org.springframework.transaction.jta.JtaTransactionManager.

This base class provides the following workflow handling:

Subclasses have to implement specific template methods for specific states of a transaction, e.g.: 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, JPA, etc 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.

Authors
Juergen Hoeller, Sam Brannen
Since
28.03.2003
See Also
setTransactionSynchronization, TransactionSynchronizationManager, org.springframework.transaction.jta.JtaTransactionManager

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected static class

Field Summary

Modifier and TypeField and Description
pack-priv static final Map<String, Integer>
constants

Map of constant names to constant values for the transaction synchronization constants defined in this class.

private int
private boolean
private boolean
protected transient Log
private boolean
private boolean
public static final int
SYNCHRONIZATION_ALWAYS

Always activate transaction synchronization, even for "empty" transactions that result from PROPAGATION_SUPPORTS with no existing backend transaction.

public static final int
SYNCHRONIZATION_NEVER

Never active transaction synchronization, not even for actual transactions.

public static final int
SYNCHRONIZATION_ON_ACTUAL_TRANSACTION

Activate transaction synchronization only for actual transactions, that is, not for empty ones that result from PROPAGATION_SUPPORTS with no existing backend transaction.

private Collection<TransactionExecutionListener>
private int
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
cleanupAfterCompletion(DefaultTransactionStatus
object representing the transaction
status
)

Clean up after completion, clearing synchronization if necessary, and invoking doCleanupAfterCompletion.

public final void
commit(TransactionStatus
object returned by the getTransaction method
status
)

Implements org.springframework.transaction.PlatformTransactionManager.commit.

This implementation of commit handles participating in existing transactions and programmatic rollback requests.

protected int

Returns:

the actual timeout to use
determineTimeout
(TransactionDefinition
the transaction definition
definition
)

Determine the actual timeout to use for the given definition.

protected abstract void
doBegin(Object
the transaction object returned by doGetTransaction
transaction
,
TransactionDefinition
a TransactionDefinition instance, describing propagation behavior, isolation level, read-only flag, timeout, and transaction name
definition
)

Begin a new transaction with semantics according to the given transaction definition.

protected void
doCleanupAfterCompletion(Object
the transaction object returned by doGetTransaction
transaction
)

Cleanup resources after transaction completion.

protected abstract void
doCommit(DefaultTransactionStatus
the status representation of the transaction
status
)

Perform an actual commit of the given transaction.

protected abstract Object

Returns:

the current transaction object
doGetTransaction
()

Return a transaction object for the current transaction state.

protected void
doResume(Object
the transaction object returned by doGetTransaction
transaction
,
Object
the object that holds suspended resources, as returned by doSuspend
suspendedResources
)

Resume the resources of the current transaction.

private void
doResumeSynchronization(List<TransactionSynchronization>
a List of TransactionSynchronization objects
suspendedSynchronizations
)

Reactivate transaction synchronization for the current thread and resume all given synchronizations.

protected abstract void
doRollback(DefaultTransactionStatus
the status representation of the transaction
status
)

Perform an actual rollback of the given transaction.

private void
doRollbackOnCommitException(DefaultTransactionStatus
object representing the transaction
status
,
Throwable
the thrown application exception or error
ex
)

Invoke doRollback, handling rollback exceptions properly.

protected void
doSetRollbackOnly(DefaultTransactionStatus
the status representation of the transaction
status
)

Set the given transaction rollback-only.

protected Object

Returns:

an object that holds suspended resources (will be kept unexamined for passing it into doResume)
doSuspend
(Object
the transaction object returned by doGetTransaction
transaction
)

Suspend the resources of the current transaction.

private List<TransactionSynchronization>

Returns:

the List of suspended TransactionSynchronization objects
doSuspendSynchronization
()

Suspend all current synchronizations and deactivate transaction synchronization for the current thread.

public final int
getDefaultTimeout()

Return the default timeout that this transaction manager should apply if there is no timeout specified at the transaction level, in seconds.

public final TransactionStatus
getTransaction(TransactionDefinition
the TransactionDefinition instance (can be null for defaults), describing propagation behavior, isolation level, timeout etc.
definition
)

Implements org.springframework.transaction.PlatformTransactionManager.getTransaction.

This implementation handles propagation behavior.

public final Collection<TransactionExecutionListener>
getTransactionExecutionListeners()

Implements org.springframework.transaction.ConfigurableTransactionManager.getTransactionExecutionListeners.

Return the registered transaction execution listeners for this transaction manager.

public final int
getTransactionSynchronization()

Return if this transaction manager should activate the thread-bound transaction synchronization support.

private TransactionStatus
handleExistingTransaction(TransactionDefinition definition, Object transaction, boolean debugEnabled)

Create a TransactionStatus for an existing transaction.

protected final void
invokeAfterCompletion(List<TransactionSynchronization>
a List of TransactionSynchronization objects
synchronizations
,
int
the completion status according to the constants in the TransactionSynchronization interface
completionStatus
)

Actually invoke the afterCompletion methods of the given Spring TransactionSynchronization objects.

protected boolean

Returns:

if there is an existing transaction
isExistingTransaction
(Object
the transaction object returned by doGetTransaction
transaction
)

Check if the given transaction object indicates an existing transaction (that is, a transaction which has already started).

public final boolean
isFailEarlyOnGlobalRollbackOnly()

Return whether to fail early in case of the transaction being globally marked as rollback-only.

public final boolean
isGlobalRollbackOnParticipationFailure()

Return whether to globally mark an existing transaction as rollback-only after a participating transaction failed.

public final boolean
isNestedTransactionAllowed()

Return whether nested transactions are allowed.

public final boolean
isRollbackOnCommitFailure()

Return whether doRollback should be performed on failure of the doCommit call.

public final boolean
isValidateExistingTransaction()

Return whether existing transactions should be validated before participating in them.

private DefaultTransactionStatus
newTransactionStatus(TransactionDefinition definition, Object transaction, boolean newTransaction, boolean newSynchronization, boolean nested, boolean debug, Object suspendedResources)

Create a TransactionStatus instance for the given arguments.

protected void
prepareForCommit(DefaultTransactionStatus
the status representation of the transaction
status
)

Make preparations for commit, to be performed before the beforeCommit synchronization callbacks occur.

protected void
prepareSynchronization(DefaultTransactionStatus status, TransactionDefinition definition)

Initialize transaction synchronization as appropriate.

private DefaultTransactionStatus
prepareTransactionStatus(TransactionDefinition definition, Object transaction, boolean newTransaction, boolean newSynchronization, boolean debug, Object suspendedResources)

Create a new TransactionStatus for the given arguments, also initializing transaction synchronization as appropriate.

private void
processCommit(DefaultTransactionStatus
object representing the transaction
status
)

Process an actual commit.

private void
processRollback(DefaultTransactionStatus
object representing the transaction
status
,
boolean unexpected)

Process an actual rollback.

private void
protected void
registerAfterCompletionWithExistingTransaction(Object
the transaction object returned by doGetTransaction
transaction
,
List<TransactionSynchronization>
a List of TransactionSynchronization objects
synchronizations
)

Register the given list of transaction synchronizations with the existing transaction.

protected final void
resume(Object
the current transaction object
transaction
,
AbstractPlatformTransactionManager.SuspendedResourcesHolder
the object that holds suspended resources, as returned by suspend (or null to just resume synchronizations, if any)
resourcesHolder
)

Resume the given transaction.

private void
resumeAfterBeginException(Object transaction, AbstractPlatformTransactionManager.SuspendedResourcesHolder suspendedResources, Throwable beginEx)

Resume outer transaction after inner transaction begin failed.

public final void
rollback(TransactionStatus
object returned by the getTransaction method
status
)

Implements org.springframework.transaction.PlatformTransactionManager.rollback.

This implementation of rollback handles participating in existing transactions.

public final void
setDefaultTimeout(int defaultTimeout)

Specify the default timeout that this transaction manager should apply if there is no timeout specified at the transaction level, in seconds.

public final void
setFailEarlyOnGlobalRollbackOnly(boolean failEarlyOnGlobalRollbackOnly)

Set whether to fail early in case of the transaction being globally marked as rollback-only.

public final void
setGlobalRollbackOnParticipationFailure(boolean globalRollbackOnParticipationFailure)

Set whether to globally mark an existing transaction as rollback-only after a participating transaction failed.

public final void
setNestedTransactionAllowed(boolean nestedTransactionAllowed)

Set whether nested transactions are allowed.

public final void
setRollbackOnCommitFailure(boolean rollbackOnCommitFailure)

Set whether doRollback should be performed on failure of the doCommit call.

public final void
setTransactionExecutionListeners(Collection<TransactionExecutionListener> listeners)

Implements org.springframework.transaction.ConfigurableTransactionManager.setTransactionExecutionListeners.

Set the transaction execution listeners for begin/commit/rollback callbacks from this transaction manager.

public final void
setTransactionSynchronization(int transactionSynchronization)

Set when this transaction manager should activate the thread-bound transaction synchronization support.

public final void
setTransactionSynchronizationName(String
name of the constant
constantName
)

Set the transaction synchronization by the name of the corresponding constant in this class — for example, "SYNCHRONIZATION_ALWAYS".

public final void
setValidateExistingTransaction(boolean validateExistingTransaction)

Set whether existing transactions should be validated before participating in them.

protected boolean
shouldCommitOnGlobalRollbackOnly()

Return whether to call doCommit on a transaction that has been marked as rollback-only in a global fashion.

private TransactionStatus
startTransaction(TransactionDefinition definition, Object transaction, boolean nested, boolean debugEnabled, AbstractPlatformTransactionManager.SuspendedResourcesHolder suspendedResources)

Start a new transaction.

protected final AbstractPlatformTransactionManager.SuspendedResourcesHolder

Returns:

an object that holds suspended resources (or null if neither transaction nor synchronization active)
suspend
(Object
the current transaction object (or null to just suspend active synchronizations, if any)
transaction
)

Suspend the given transaction.

private void
triggerAfterCommit(DefaultTransactionStatus
object representing the transaction
status
)

Trigger afterCommit callbacks.

private void
triggerAfterCompletion(DefaultTransactionStatus
object representing the transaction
status
,
int
completion status according to TransactionSynchronization constants
completionStatus
)

Trigger afterCompletion callbacks.

protected final void
triggerBeforeCommit(DefaultTransactionStatus
object representing the transaction
status
)

Trigger beforeCommit callbacks.

protected final void
triggerBeforeCompletion(DefaultTransactionStatus
object representing the transaction
status
)

Trigger beforeCompletion callbacks.

protected boolean
useSavepointForNestedTransaction()

Return whether to use a savepoint for a nested transaction.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait