T
- The resource type@Internal public abstract class AbstractSynchronousTransactionManager<T> extends AbstractSynchronousStateTransactionManager<T> implements SynchronousTransactionManager<T>, java.io.Serializable
AbstractPlatformTransactionManager
modernizing it
to use enums, Slf4j and decoupling from Spring.
Abstract base class that implements standard transaction workflow,
serving as basis for concrete platform transaction managers.
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.
AbstractSynchronousStateTransactionManager.setTransactionSynchronization(io.micronaut.transaction.support.AbstractSynchronousStateTransactionManager.Synchronization)
,
TransactionSynchronizationManager
,
Serialized FormModifier and Type | Class and Description |
---|---|
protected static class |
AbstractSynchronousTransactionManager.SuspendedResourcesHolder
Holder for suspended resources.
|
logger
Constructor and Description |
---|
AbstractSynchronousTransactionManager() |
Modifier and Type | Method and Description |
---|---|
void |
commit(TransactionStatus<T> status)
This implementation of commit handles participating in existing
transactions and programmatic rollback requests.
|
protected void |
doCleanupAfterCompletion(java.lang.Object transaction)
Cleanup resources after transaction completion.
|
protected void |
doCleanupAfterCompletion(SynchronousTransactionState state,
java.lang.Object transaction)
Cleanup resources after transaction completion.
|
protected void |
doDestroyState(SynchronousTransactionState state)
Destroy the state.
|
protected abstract java.lang.Object |
doGetTransaction()
Return a transaction object for the current transaction state.
|
protected java.lang.Object |
doGetTransaction(SynchronousTransactionState state)
Return a transaction object for the current transaction state.
|
<R> R |
execute(TransactionDefinition definition,
TransactionCallback<T,R> callback)
Execute a read-only transaction within the context of the function.
|
<R> R |
executeRead(TransactionCallback<T,R> callback)
Execute a read-only transaction within the context of the function.
|
<R> R |
executeWrite(TransactionCallback<T,R> callback)
Execute a default transaction within the context of the function.
|
protected SynchronousTransactionState |
findOrCreateState()
Find or create a new state.
|
protected abstract T |
getConnection(java.lang.Object transaction)
The connection for the given transaction object.
|
protected T |
getConnection(SynchronousTransactionState state,
java.lang.Object transaction)
The connection for the given transaction object.
|
protected SynchronousTransactionState |
getState()
Return required current transaction state.
|
TransactionStatus<T> |
getTransaction(TransactionDefinition definition)
Return a currently active transaction or create a new one, according to
the specified propagation behavior.
|
protected java.lang.Object |
getTransactionStateKey()
Get the transaction state key that should be used to store the state.
|
protected void |
invokeAfterCompletion(java.util.List<TransactionSynchronization> synchronizations,
TransactionSynchronization.Status completionStatus)
Actually invoke the
afterCompletion methods of the
given Spring TransactionSynchronization objects. |
protected boolean |
isExistingTransaction(java.lang.Object transaction)
Check if the given transaction object indicates an existing transaction
(that is, a transaction which has already started).
|
protected boolean |
isExistingTransaction(SynchronousTransactionState state,
java.lang.Object transaction)
Check if the given transaction object indicates an existing transaction
(that is, a transaction which has already started).
|
protected DefaultTransactionStatus<T> |
newTransactionStatus(SynchronousTransactionState state,
TransactionDefinition definition,
java.lang.Object transaction,
boolean newTransaction,
boolean newSynchronization,
boolean debug,
java.lang.Object suspendedResources)
Create a TransactionStatus instance for the given arguments.
|
protected DefaultTransactionStatus<T> |
newTransactionStatus(TransactionDefinition definition,
java.lang.Object transaction,
boolean newTransaction,
boolean newSynchronization,
boolean debug,
java.lang.Object suspendedResources)
Create a TransactionStatus instance for the given arguments.
|
protected void |
prepareSynchronization(DefaultTransactionStatus<T> status,
TransactionDefinition definition)
Initialize transaction synchronization as appropriate.
|
protected void |
prepareSynchronization(SynchronousTransactionState state,
DefaultTransactionStatus<T> status,
TransactionDefinition definition)
Initialize transaction synchronization as appropriate.
|
protected DefaultTransactionStatus<T> |
prepareTransactionStatus(TransactionDefinition definition,
java.lang.Object transaction,
boolean newTransaction,
boolean newSynchronization,
boolean debug,
java.lang.Object suspendedResources)
Create a new TransactionStatus for the given arguments,
also initializing transaction synchronization as appropriate.
|
protected void |
registerAfterCompletionWithExistingTransaction(java.lang.Object transaction,
java.util.List<TransactionSynchronization> synchronizations)
Register the given list of transaction synchronizations with the existing transaction.
|
protected void |
registerAfterCompletionWithExistingTransaction(SynchronousTransactionState state,
java.lang.Object transaction,
java.util.List<TransactionSynchronization> synchronizations)
Register the given list of transaction synchronizations with the existing transaction.
|
protected void |
resume(java.lang.Object transaction,
AbstractSynchronousTransactionManager.SuspendedResourcesHolder resourcesHolder)
Resume the given transaction.
|
void |
rollback(TransactionStatus<T> status)
This implementation of rollback handles participating in existing
transactions.
|
protected AbstractSynchronousTransactionManager.SuspendedResourcesHolder |
suspend(java.lang.Object transaction)
Suspend the given transaction.
|
protected void |
triggerBeforeCommit(DefaultTransactionStatus<T> status)
Trigger
beforeCommit callbacks. |
protected void |
triggerBeforeCompletion(DefaultTransactionStatus<T> status)
Trigger
beforeCompletion callbacks. |
commit, determineTimeout, doBegin, doCommit, doResume, doRollback, doSetRollbackOnly, doSuspend, execute, executeRead, executeWrite, getDefaultTimeout, getTransaction, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, prepareForCommit, prepareTransactionStatus, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getConnection, hasConnection
public AbstractSynchronousTransactionManager()
@NotNull protected java.lang.Object getTransactionStateKey()
@NonNull protected SynchronousTransactionState getState()
@NonNull protected SynchronousTransactionState findOrCreateState()
protected void doDestroyState(SynchronousTransactionState state)
doDestroyState
in class AbstractSynchronousStateTransactionManager<T>
state
- The statepublic <R> R execute(@NonNull TransactionDefinition definition, @NonNull TransactionCallback<T,R> callback)
TransactionOperations
execute
in interface TransactionOperations<T>
R
- The resultdefinition
- The transaction definitioncallback
- The call backpublic <R> R executeRead(@NonNull TransactionCallback<T,R> callback)
TransactionOperations
executeRead
in interface TransactionOperations<T>
R
- The resultcallback
- The call backpublic <R> R executeWrite(@NonNull TransactionCallback<T,R> callback)
TransactionOperations
executeWrite
in interface TransactionOperations<T>
R
- The resultcallback
- The call back@NonNull public final TransactionStatus<T> getTransaction(@Nullable TransactionDefinition definition) throws TransactionException
SynchronousTransactionManager
Note that parameters like isolation level or timeout will only be applied to new transactions, and thus be ignored when participating in active ones.
Furthermore, not all transaction definition settings will be supported by every transaction manager: A proper transaction manager implementation should throw an exception when unsupported settings are encountered.
An exception to the above rule is the read-only flag, which should be ignored if no explicit read-only mode is supported. Essentially, the read-only flag is just a hint for potential optimization.
getTransaction
in interface SynchronousTransactionManager<T>
definition
- the TransactionDefinition instance (can be null
for defaults),
describing propagation behavior, isolation level, timeout etc.TransactionException
- in case of lookup, creation, or system errorsIllegalTransactionStateException
- if the given transaction definition
cannot be executed (for example, if a currently active transaction is in
conflict with the specified propagation behavior)TransactionDefinition.getPropagationBehavior()
,
TransactionDefinition.getIsolationLevel()
,
TransactionDefinition.getTimeout()
,
TransactionDefinition.isReadOnly()
protected final DefaultTransactionStatus<T> prepareTransactionStatus(TransactionDefinition definition, @Nullable java.lang.Object transaction, boolean newTransaction, boolean newSynchronization, boolean debug, @Nullable java.lang.Object suspendedResources)
definition
- The definitiontransaction
- The transaction objectnewTransaction
- Is this is a new transactionnewSynchronization
- Is this a new synchronizationdebug
- Is debug enabledsuspendedResources
- Any suspended resourcesnewTransactionStatus(io.micronaut.transaction.support.SynchronousTransactionState, io.micronaut.transaction.TransactionDefinition, java.lang.Object, boolean, boolean, boolean, java.lang.Object)
,
prepareTransactionStatus(io.micronaut.transaction.TransactionDefinition, java.lang.Object, boolean, boolean, boolean, java.lang.Object)
protected DefaultTransactionStatus<T> newTransactionStatus(SynchronousTransactionState state, TransactionDefinition definition, java.lang.Object transaction, boolean newTransaction, boolean newSynchronization, boolean debug, java.lang.Object suspendedResources)
AbstractSynchronousStateTransactionManager
newTransactionStatus
in class AbstractSynchronousStateTransactionManager<T>
state
- The transaction statedefinition
- The definitiontransaction
- The transaction objectnewTransaction
- Is this is a new transactionnewSynchronization
- Is this a new synchronizationdebug
- Is debug enabledsuspendedResources
- Any suspended resourcesprotected DefaultTransactionStatus<T> newTransactionStatus(TransactionDefinition definition, @Nullable java.lang.Object transaction, boolean newTransaction, boolean newSynchronization, boolean debug, @Nullable java.lang.Object suspendedResources)
definition
- The definitiontransaction
- The transaction objectnewTransaction
- Is this is a new transactionnewSynchronization
- Is this a new synchronizationdebug
- Is debug enabledsuspendedResources
- Any suspended resourcesprotected T getConnection(SynchronousTransactionState state, java.lang.Object transaction)
AbstractSynchronousStateTransactionManager
getConnection
in class AbstractSynchronousStateTransactionManager<T>
state
- The transaction statetransaction
- The transaction@Nullable protected abstract T getConnection(java.lang.Object transaction)
transaction
- The transactionprotected void prepareSynchronization(SynchronousTransactionState state, DefaultTransactionStatus<T> status, TransactionDefinition definition)
AbstractSynchronousStateTransactionManager
prepareSynchronization
in class AbstractSynchronousStateTransactionManager<T>
state
- The transaction statestatus
- The statusdefinition
- The definitionprotected void prepareSynchronization(@NonNull DefaultTransactionStatus<T> status, @NonNull TransactionDefinition definition)
status
- The statusdefinition
- The definition@Nullable protected final AbstractSynchronousTransactionManager.SuspendedResourcesHolder suspend(@Nullable java.lang.Object transaction) throws TransactionException
doSuspend
template method.transaction
- the current transaction object
(or null
to just suspend active synchronizations, if any)null
if neither transaction nor synchronization active)TransactionException
- Thrown if an error occurs suspending the transactionAbstractSynchronousStateTransactionManager.doSuspend(java.lang.Object)
,
resume(java.lang.Object, io.micronaut.transaction.support.AbstractSynchronousTransactionManager.SuspendedResourcesHolder)
protected final void resume(@Nullable java.lang.Object transaction, @Nullable AbstractSynchronousTransactionManager.SuspendedResourcesHolder resourcesHolder) throws TransactionException
doResume
template method first, then resuming transaction synchronization.transaction
- the current transaction objectresourcesHolder
- the object that holds suspended resources,
as returned by suspend
(or null
to just
resume synchronizations, if any)TransactionException
- Thrown if an error occurs resuming the transactionAbstractSynchronousStateTransactionManager.doResume(java.lang.Object, java.lang.Object)
,
suspend(java.lang.Object)
public final void commit(TransactionStatus<T> status) throws TransactionException
isRollbackOnly
, doCommit
and rollback
.commit
in interface SynchronousTransactionManager<T>
status
- object returned by the getTransaction
methodUnexpectedRollbackException
- in case of an unexpected rollback
that the transaction coordinator initiatedHeuristicCompletionException
- in case of a transaction failure
caused by a heuristic decision on the side of the transaction coordinatorTransactionSystemException
- in case of commit or system errors
(typically caused by fundamental resource failures)IllegalTransactionStateException
- if the given transaction
is already completed (that is, committed or rolled back)TransactionException
- if something goes wrong during commitTransactionExecution.isRollbackOnly()
,
AbstractSynchronousStateTransactionManager.doCommit(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
,
rollback(io.micronaut.transaction.TransactionStatus<T>)
public final void rollback(TransactionStatus<T> status) throws TransactionException
doRollback
and
doSetRollbackOnly
.rollback
in interface SynchronousTransactionManager<T>
status
- object returned by the getTransaction
methodTransactionSystemException
- in case of rollback or system errors
(typically caused by fundamental resource failures)IllegalTransactionStateException
- if the given transaction
is already completed (that is, committed or rolled back)TransactionException
- if something goes wrong during rollbackAbstractSynchronousStateTransactionManager.doRollback(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
,
AbstractSynchronousStateTransactionManager.doSetRollbackOnly(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
protected final void triggerBeforeCommit(DefaultTransactionStatus<T> status)
beforeCommit
callbacks.status
- object representing the transactionprotected final void triggerBeforeCompletion(DefaultTransactionStatus<T> status)
beforeCompletion
callbacks.status
- object representing the transactionprotected java.lang.Object doGetTransaction(@NonNull SynchronousTransactionState state) throws TransactionException
AbstractSynchronousStateTransactionManager
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.
doGetTransaction
in class AbstractSynchronousStateTransactionManager<T>
state
- The transaction stateCannotCreateTransactionException
- if transaction support is not availableTransactionException
- in case of lookup or system errorsAbstractSynchronousStateTransactionManager.doBegin(java.lang.Object, io.micronaut.transaction.TransactionDefinition)
,
AbstractSynchronousStateTransactionManager.doCommit(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
,
AbstractSynchronousStateTransactionManager.doRollback(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
,
DefaultTransactionStatus.getTransaction()
protected boolean isExistingTransaction(@NonNull SynchronousTransactionState state, @NonNull java.lang.Object transaction) throws TransactionException
AbstractSynchronousStateTransactionManager
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).
The default implementation returns false
, assuming that
participating in existing transactions is generally not supported.
Subclasses are of course encouraged to provide such support.
isExistingTransaction
in class AbstractSynchronousStateTransactionManager<T>
state
- The transaction statetransaction
- transaction object returned by doGetTransactionTransactionException
- in case of system errorsAbstractSynchronousStateTransactionManager.doGetTransaction(io.micronaut.transaction.support.SynchronousTransactionState)
protected final void invokeAfterCompletion(java.util.List<TransactionSynchronization> synchronizations, TransactionSynchronization.Status completionStatus)
afterCompletion
methods of the
given Spring TransactionSynchronization objects.
To be called by this abstract manager itself, or by special implementations
of the registerAfterCompletionWithExistingTransaction
callback.
synchronizations
- a List of TransactionSynchronization objectscompletionStatus
- the completion status according to the
constants in the TransactionSynchronization interfaceregisterAfterCompletionWithExistingTransaction(SynchronousTransactionState, Object, List)
,
TransactionSynchronization.Status.COMMITTED
,
TransactionSynchronization.Status.ROLLED_BACK
,
TransactionSynchronization.Status.UNKNOWN
@NonNull protected abstract java.lang.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 availableTransactionException
- in case of lookup or system errorsAbstractSynchronousStateTransactionManager.doBegin(java.lang.Object, io.micronaut.transaction.TransactionDefinition)
,
AbstractSynchronousStateTransactionManager.doCommit(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
,
AbstractSynchronousStateTransactionManager.doRollback(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
,
DefaultTransactionStatus.getTransaction()
protected boolean isExistingTransaction(@NonNull java.lang.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).
The default implementation returns false
, assuming that
participating in existing transactions is generally not supported.
Subclasses are of course encouraged to provide such support.
transaction
- transaction object returned by doGetTransactionTransactionException
- in case of system errorsdoGetTransaction(io.micronaut.transaction.support.SynchronousTransactionState)
protected void registerAfterCompletionWithExistingTransaction(SynchronousTransactionState state, java.lang.Object transaction, java.util.List<TransactionSynchronization> synchronizations) throws TransactionException
AbstractSynchronousStateTransactionManager
Invoked when the control of the transaction manager and thus all transaction synchronizations end, without the transaction being completed yet. This is for example the case when participating in an existing JTA or EJB CMT transaction.
The default implementation simply invokes the afterCompletion
methods
immediately, passing in "STATUS_UNKNOWN". This is the best we can do if there's no
chance to determine the actual outcome of the outer transaction.
registerAfterCompletionWithExistingTransaction
in class AbstractSynchronousStateTransactionManager<T>
state
- The transaction statetransaction
- transaction object returned by doGetTransaction
synchronizations
- a List of TransactionSynchronization objectsTransactionException
- in case of system errorsAbstractSynchronousStateTransactionManager.invokeAfterCompletion(SynchronousTransactionState, List, TransactionSynchronization.Status)
,
TransactionSynchronization.afterCompletion(TransactionSynchronization.Status)
,
TransactionSynchronization.Status.UNKNOWN
protected void registerAfterCompletionWithExistingTransaction(java.lang.Object transaction, java.util.List<TransactionSynchronization> synchronizations) throws TransactionException
Invoked when the control of the Spring transaction manager and thus all Spring transaction synchronizations end, without the transaction being completed yet. This is for example the case when participating in an existing JTA or EJB CMT transaction.
The default implementation simply invokes the afterCompletion
methods
immediately, passing in "STATUS_UNKNOWN". This is the best we can do if there's no
chance to determine the actual outcome of the outer transaction.
transaction
- transaction object returned by doGetTransaction
synchronizations
- a List of TransactionSynchronization objectsTransactionException
- in case of system errorsinvokeAfterCompletion(List, TransactionSynchronization.Status)
,
TransactionSynchronization.afterCompletion(TransactionSynchronization.Status)
,
TransactionSynchronization.Status.UNKNOWN
protected void doCleanupAfterCompletion(SynchronousTransactionState state, java.lang.Object transaction)
AbstractSynchronousStateTransactionManager
Called after doCommit
and doRollback
execution,
on any outcome. The default implementation does nothing.
Should not throw any exceptions but just issue warnings on errors.
doCleanupAfterCompletion
in class AbstractSynchronousStateTransactionManager<T>
state
- The statetransaction
- transaction object returned by doGetTransaction
protected void doCleanupAfterCompletion(java.lang.Object transaction)
Called after doCommit
and doRollback
execution,
on any outcome. The default implementation does nothing.
Should not throw any exceptions but just issue warnings on errors.
transaction
- transaction object returned by doGetTransaction