T
- The resource type@Internal
public abstract class AbstractSynchronousStateTransactionManager<T>
extends java.lang.Object
implements 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.
setTransactionSynchronization(io.micronaut.transaction.support.AbstractSynchronousStateTransactionManager.Synchronization)
,
TransactionSynchronizationManager
,
Serialized FormModifier and Type | Class and Description |
---|---|
protected static class |
AbstractSynchronousStateTransactionManager.SuspendedResourcesHolder
Holder for suspended resources.
|
Modifier and Type | Field and Description |
---|---|
protected org.slf4j.Logger |
logger |
Constructor and Description |
---|
AbstractSynchronousStateTransactionManager() |
Modifier and Type | Method and Description |
---|---|
void |
commit(SynchronousTransactionState state,
TransactionStatus<T> status)
Commit the given transaction, with regard to its status.
|
protected java.time.Duration |
determineTimeout(TransactionDefinition definition)
Determine the actual timeout to use for the given definition.
|
protected abstract void |
doBegin(java.lang.Object transaction,
TransactionDefinition definition)
Begin a new transaction with semantics according to the given transaction
definition.
|
protected void |
doCleanupAfterCompletion(SynchronousTransactionState state,
java.lang.Object transaction)
Cleanup resources after transaction completion.
|
protected abstract void |
doCommit(DefaultTransactionStatus<T> status)
Perform an actual commit of the given transaction.
|
protected void |
doDestroyState(SynchronousTransactionState state)
Destroy the state.
|
protected abstract java.lang.Object |
doGetTransaction(SynchronousTransactionState state)
Return a transaction object for the current transaction state.
|
protected void |
doResume(java.lang.Object transaction,
java.lang.Object suspendedResources)
Resume the resources of the current transaction.
|
protected abstract void |
doRollback(DefaultTransactionStatus<T> status)
Perform an actual rollback of the given transaction.
|
protected void |
doSetRollbackOnly(DefaultTransactionStatus<T> status)
Set the given transaction rollback-only.
|
protected java.lang.Object |
doSuspend(java.lang.Object transaction)
Suspend the resources of the current transaction.
|
<R> R |
execute(SynchronousTransactionState state,
TransactionDefinition definition,
TransactionCallback<T,R> callback)
Execute a read-only transaction within the context of the function.
|
<R> R |
executeRead(SynchronousTransactionState state,
TransactionCallback<T,R> callback)
Execute a read-only transaction within the context of the function.
|
<R> R |
executeWrite(SynchronousTransactionState state,
TransactionCallback<T,R> callback)
Execute a default transaction within the context of the function.
|
protected abstract T |
getConnection(SynchronousTransactionState state,
java.lang.Object transaction)
The connection for the given transaction object.
|
java.time.Duration |
getDefaultTimeout()
Return the default timeout that this transaction manager should apply
if there is no timeout specified at the transaction level, in seconds.
|
TransactionStatus<T> |
getTransaction(SynchronousTransactionState state,
TransactionDefinition definition)
This implementation handles propagation behavior.
|
io.micronaut.transaction.support.AbstractSynchronousStateTransactionManager.Synchronization |
getTransactionSynchronization()
Return if this transaction manager should activate the thread-bound
transaction synchronization support.
|
protected void |
invokeAfterCompletion(SynchronousTransactionState state,
java.util.List<TransactionSynchronization> synchronizations,
TransactionSynchronization.Status completionStatus)
Actually invoke the
afterCompletion methods of the
given Spring TransactionSynchronization objects. |
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).
|
boolean |
isFailEarlyOnGlobalRollbackOnly() |
boolean |
isGlobalRollbackOnParticipationFailure() |
boolean |
isNestedTransactionAllowed() |
boolean |
isRollbackOnCommitFailure() |
boolean |
isValidateExistingTransaction()
Return whether existing transactions should be validated before participating
in them.
|
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 void |
prepareForCommit(DefaultTransactionStatus<T> status)
Make preparations for commit, to be performed before the
beforeCommit synchronization callbacks occur. |
protected void |
prepareSynchronization(SynchronousTransactionState state,
DefaultTransactionStatus<T> status,
TransactionDefinition definition)
Initialize transaction synchronization as appropriate.
|
protected DefaultTransactionStatus<T> |
prepareTransactionStatus(SynchronousTransactionState state,
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(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(SynchronousTransactionState state,
java.lang.Object transaction,
AbstractSynchronousStateTransactionManager.SuspendedResourcesHolder resourcesHolder)
Resume the given transaction.
|
void |
rollback(SynchronousTransactionState state,
TransactionStatus<T> status)
Perform a rollback of the given transaction.
|
void |
setDefaultTimeout(java.time.Duration defaultTimeout)
Specify the default timeout that this transaction manager should apply
if there is no timeout specified at the transaction level, in seconds.
|
void |
setFailEarlyOnGlobalRollbackOnly(boolean failEarlyOnGlobalRollbackOnly)
Set whether to fail early in case of the transaction being globally marked
as rollback-only.
|
void |
setGlobalRollbackOnParticipationFailure(boolean globalRollbackOnParticipationFailure)
Set whether to globally mark an existing transaction as rollback-only
after a participating transaction failed.
|
void |
setNestedTransactionAllowed(boolean nestedTransactionAllowed)
Set whether nested transactions are allowed.
|
void |
setRollbackOnCommitFailure(boolean rollbackOnCommitFailure)
Set whether
doRollback should be performed on failure of the
doCommit call. |
void |
setTransactionSynchronization(io.micronaut.transaction.support.AbstractSynchronousStateTransactionManager.Synchronization transactionSynchronization)
Set when this transaction manager should activate the thread-bound
transaction synchronization support.
|
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. |
protected AbstractSynchronousStateTransactionManager.SuspendedResourcesHolder |
suspend(SynchronousTransactionState state,
java.lang.Object transaction)
Suspend the given transaction.
|
protected void |
triggerBeforeCommit(SynchronousTransactionState state,
DefaultTransactionStatus<T> status)
Trigger
beforeCommit callbacks. |
protected void |
triggerBeforeCompletion(SynchronousTransactionState state,
DefaultTransactionStatus<T> status)
Trigger
beforeCompletion callbacks. |
protected boolean |
useSavepointForNestedTransaction()
Return whether to use a savepoint for a nested transaction.
|
public AbstractSynchronousStateTransactionManager()
public <R> R execute(@NonNull SynchronousTransactionState state, @NonNull TransactionDefinition definition, @NonNull TransactionCallback<T,R> callback)
TransactionStateOperations
R
- The resultstate
- The transaction statedefinition
- The transaction definitioncallback
- The call backpublic <R> R executeRead(@NonNull SynchronousTransactionState state, @NonNull TransactionCallback<T,R> callback)
TransactionStateOperations
R
- The resultstate
- The transaction statecallback
- The call backpublic <R> R executeWrite(@NonNull SynchronousTransactionState state, @NonNull TransactionCallback<T,R> callback)
TransactionStateOperations
R
- The resultstate
- The transaction statecallback
- The call backpublic final void setTransactionSynchronization(@NonNull io.micronaut.transaction.support.AbstractSynchronousStateTransactionManager.Synchronization 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.
transactionSynchronization
- the synchronization to useSynchronization#ALWAYS
,
Synchronization#ON_ACTUAL_TRANSACTION
,
Synchronization#NEVER
,
TransactionSynchronization
@NonNull public final io.micronaut.transaction.support.AbstractSynchronousStateTransactionManager.Synchronization getTransactionSynchronization()
public final void setDefaultTimeout(@NonNull java.time.Duration defaultTimeout)
Default is the underlying transaction infrastructure's default timeout,
e.g. typically 30 seconds in case of a JTA provider, indicated by the
TransactionDefinition.TIMEOUT_DEFAULT
value.
defaultTimeout
- The default timeoutTransactionDefinition.TIMEOUT_DEFAULT
@NonNull public final java.time.Duration getDefaultTimeout()
Returns TransactionDefinition.TIMEOUT_DEFAULT
to indicate
the underlying transaction infrastructure's default timeout.
public final void setNestedTransactionAllowed(boolean nestedTransactionAllowed)
Typically initialized with an appropriate default by the concrete transaction manager subclass.
nestedTransactionAllowed
- Whether a nested transaction is allowedpublic final boolean isNestedTransactionAllowed()
public final void setValidateExistingTransaction(boolean validateExistingTransaction)
When participating in an existing transaction (e.g. with PROPAGATION_REQUIRED or PROPAGATION_SUPPORTS encountering an existing transaction), this outer transaction's characteristics will apply even to the inner transaction scope. Validation will detect incompatible isolation level and read-only settings on the inner transaction definition and reject participation accordingly through throwing a corresponding exception.
Default is "false", leniently ignoring inner transaction settings, simply overriding them with the outer transaction's characteristics. Switch this flag to "true" in order to enforce strict validation.
validateExistingTransaction
- Whether to validate an existing transactionpublic final boolean isValidateExistingTransaction()
public final void setGlobalRollbackOnParticipationFailure(boolean globalRollbackOnParticipationFailure)
Default is "true": If a participating transaction (e.g. with PROPAGATION_REQUIRED or PROPAGATION_SUPPORTS encountering an existing transaction) fails, the transaction will be globally marked as rollback-only. The only possible outcome of such a transaction is a rollback: The transaction originator cannot make the transaction commit anymore.
Switch this to "false" to let the transaction originator make the rollback decision. If a participating transaction fails with an exception, the caller can still decide to continue with a different path within the transaction. However, note that this will only work as long as all participating resources are capable of continuing towards a transaction commit even after a data access failure: This is generally not the case for a Hibernate Session, for example; neither is it for a sequence of JDBC insert/update/delete operations.
Note:This flag only applies to an explicit rollback attempt for a
subtransaction, typically caused by an exception thrown by a data access operation
(where TransactionInterceptor will trigger a PlatformTransactionManager.rollback()
call according to a rollback rule). If the flag is off, the caller can handle the exception
and decide on a rollback, independent of the rollback rules of the subtransaction.
This flag does, however, not apply to explicit setRollbackOnly
calls on a TransactionStatus
, which will always cause an eventual
global rollback (as it might not throw an exception after the rollback-only call).
The recommended solution for handling failure of a subtransaction is a "nested transaction", where the global transaction can be rolled back to a savepoint taken at the beginning of the subtransaction. PROPAGATION_NESTED provides exactly those semantics; however, it will only work when nested transaction support is available. This is the case with DataSourceTransactionManager, but not with JtaTransactionManager.
globalRollbackOnParticipationFailure
- Whether to globally mark transaction as rollback onlysetNestedTransactionAllowed(boolean)
public final boolean isGlobalRollbackOnParticipationFailure()
public final void setFailEarlyOnGlobalRollbackOnly(boolean failEarlyOnGlobalRollbackOnly)
Default is "false", only causing an UnexpectedRollbackException at the outermost transaction boundary. Switch this flag on to cause an UnexpectedRollbackException as early as the global rollback-only marker has been first detected, even from within an inner transaction boundary.
failEarlyOnGlobalRollbackOnly
- Sets whether to fail early on global rollbackUnexpectedRollbackException
public final boolean isFailEarlyOnGlobalRollbackOnly()
public final void setRollbackOnCommitFailure(boolean rollbackOnCommitFailure)
doRollback
should be performed on failure of the
doCommit
call. Typically not necessary and thus to be avoided,
as it can potentially override the commit exception with a subsequent
rollback exception.
Default is "false".
rollbackOnCommitFailure
- Sets whether to rollback on commit failuredoCommit(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
,
doRollback(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
public final boolean isRollbackOnCommitFailure()
doRollback
should be performed on failure of the
doCommit
call.@NonNull public final TransactionStatus<T> getTransaction(@NonNull SynchronousTransactionState state, @Nullable TransactionDefinition definition) throws TransactionException
doGetTransaction
, isExistingTransaction
and doBegin
.state
- the transaction statedefinition
- 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)doGetTransaction(io.micronaut.transaction.support.SynchronousTransactionState)
,
isExistingTransaction(io.micronaut.transaction.support.SynchronousTransactionState, java.lang.Object)
,
doBegin(java.lang.Object, io.micronaut.transaction.TransactionDefinition)
protected final DefaultTransactionStatus<T> prepareTransactionStatus(@NonNull SynchronousTransactionState state, TransactionDefinition definition, @Nullable java.lang.Object transaction, boolean newTransaction, boolean newSynchronization, boolean debug, @Nullable java.lang.Object suspendedResources)
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 resourcesnewTransactionStatus(io.micronaut.transaction.support.SynchronousTransactionState, io.micronaut.transaction.TransactionDefinition, java.lang.Object, boolean, boolean, boolean, java.lang.Object)
,
prepareTransactionStatus(io.micronaut.transaction.support.SynchronousTransactionState, io.micronaut.transaction.TransactionDefinition, java.lang.Object, boolean, boolean, boolean, java.lang.Object)
protected DefaultTransactionStatus<T> newTransactionStatus(@NonNull SynchronousTransactionState state, TransactionDefinition definition, @Nullable java.lang.Object transaction, boolean newTransaction, boolean newSynchronization, boolean debug, @Nullable java.lang.Object suspendedResources)
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 resources@Nullable protected abstract T getConnection(@NonNull SynchronousTransactionState state, java.lang.Object transaction)
state
- The transaction statetransaction
- The transactionprotected void prepareSynchronization(@NonNull SynchronousTransactionState state, @NonNull DefaultTransactionStatus<T> status, @NonNull TransactionDefinition definition)
state
- The transaction statestatus
- The statusdefinition
- The definitionprotected java.time.Duration determineTimeout(TransactionDefinition definition)
definition
- the transaction definitionTransactionDefinition.getTimeout()
,
setDefaultTimeout(java.time.Duration)
@Nullable protected final AbstractSynchronousStateTransactionManager.SuspendedResourcesHolder suspend(@NonNull SynchronousTransactionState state, @Nullable java.lang.Object transaction) throws TransactionException
doSuspend
template method.state
- The transaction statetransaction
- 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 transactiondoSuspend(java.lang.Object)
,
resume(io.micronaut.transaction.support.SynchronousTransactionState, java.lang.Object, io.micronaut.transaction.support.AbstractSynchronousStateTransactionManager.SuspendedResourcesHolder)
protected final void resume(@NonNull SynchronousTransactionState state, @Nullable java.lang.Object transaction, @Nullable AbstractSynchronousStateTransactionManager.SuspendedResourcesHolder resourcesHolder) throws TransactionException
doResume
template method first, then resuming transaction synchronization.state
- The transaction statetransaction
- 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 transactiondoResume(java.lang.Object, java.lang.Object)
,
suspend(io.micronaut.transaction.support.SynchronousTransactionState, java.lang.Object)
public final void commit(@NonNull SynchronousTransactionState state, TransactionStatus<T> status) throws TransactionException
If the transaction wasn't a new one, omit the commit for proper participation in the surrounding transaction. If a previous transaction has been suspended to be able to create a new one, resume the previous transaction after committing the new one.
Note that when the commit call completes, no matter if normally or throwing an exception, the transaction must be fully completed and cleaned up. No rollback call should be expected in such a case.
If this method throws an exception other than a TransactionException, then some before-commit error caused the commit attempt to fail. For example, an O/R Mapping tool might have tried to flush changes to the database right before commit, with the resulting DataAccessException causing the transaction to fail. The original exception will be propagated to the caller of this commit method in such a case.
state
- the transaction statestatus
- 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.setRollbackOnly()
public final void rollback(@NonNull SynchronousTransactionState state, TransactionStatus<T> status) throws TransactionException
If the transaction wasn't a new one, just set it rollback-only for proper participation in the surrounding transaction. If a previous transaction has been suspended to be able to create a new one, resume the previous transaction after rolling back the new one.
Do not call rollback on a transaction if commit threw an exception. The transaction will already have been completed and cleaned up when commit returns, even in case of a commit exception. Consequently, a rollback call after commit failure will lead to an IllegalTransactionStateException.
state
- the transaction statestatus
- 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 rollbackprotected final void triggerBeforeCommit(@NonNull SynchronousTransactionState state, @NonNull DefaultTransactionStatus<T> status)
beforeCommit
callbacks.state
- The transaction statestatus
- object representing the transactionprotected final void triggerBeforeCompletion(@NonNull SynchronousTransactionState state, @NonNull DefaultTransactionStatus<T> status)
beforeCompletion
callbacks.state
- The transaction statestatus
- object representing the transactionprotected final void invokeAfterCompletion(@NonNull SynchronousTransactionState state, 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.
state
- The transaction statesynchronizations
- 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(@NonNull SynchronousTransactionState state) 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.
state
- The transaction stateCannotCreateTransactionException
- if transaction support is not availableTransactionException
- in case of lookup or system errorsdoBegin(java.lang.Object, io.micronaut.transaction.TransactionDefinition)
,
doCommit(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
,
doRollback(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
,
DefaultTransactionStatus.getTransaction()
protected boolean isExistingTransaction(@NonNull SynchronousTransactionState state, @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.
state
- The transaction statetransaction
- transaction object returned by doGetTransactionTransactionException
- in case of system errorsdoGetTransaction(io.micronaut.transaction.support.SynchronousTransactionState)
protected boolean useSavepointForNestedTransaction()
Default is true
, which causes delegation to DefaultTransactionStatus
for creating and holding a savepoint. If the transaction object does not implement
the SavepointManager interface, a NestedTransactionNotSupportedException will be
thrown. Else, the SavepointManager will be asked to create a new savepoint to
demarcate the start of the nested transaction.
Subclasses can override this to return false
, causing a further
call to doBegin
- within the context of an already existing transaction.
The doBegin
implementation needs to handle this accordingly in such
a scenario. This is appropriate for JTA, for example.
AbstractTransactionStatus.createAndHoldSavepoint()
,
AbstractTransactionStatus.rollbackToHeldSavepoint()
,
AbstractTransactionStatus.releaseHeldSavepoint()
,
doBegin(java.lang.Object, io.micronaut.transaction.TransactionDefinition)
protected abstract void doBegin(@NonNull java.lang.Object transaction, TransactionDefinition definition) throws TransactionException
This method gets called when the transaction manager has decided to actually start a new transaction. Either there wasn't any transaction before, or the previous transaction has been suspended.
A special scenario is a nested transaction without savepoint: If
useSavepointForNestedTransaction()
returns "false", this method
will be called to start a nested transaction when necessary. In such a context,
there will be an active transaction: The implementation of this method has
to detect this and start an appropriate nested transaction.
transaction
- transaction object returned by doGetTransaction
definition
- a TransactionDefinition instance, describing propagation
behavior, isolation level, read-only flag, timeout, and transaction nameTransactionException
- in case of creation or system errorsNestedTransactionNotSupportedException
- if the underlying transaction does not support nesting@Nullable protected java.lang.Object doSuspend(@NonNull java.lang.Object transaction) throws TransactionException
The 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 implementationTransactionException
- in case of system errorsdoResume(java.lang.Object, java.lang.Object)
protected void doResume(@Nullable java.lang.Object transaction, @NonNull java.lang.Object suspendedResources) throws TransactionException
The default implementation throws a TransactionSuspensionNotSupportedException, assuming that transaction suspension is generally not supported.
transaction
- transaction object returned by doGetTransaction
suspendedResources
- the object that holds suspended resources,
as returned by doSuspendTransactionSuspensionNotSupportedException
- if resuming is not supported by the transaction manager implementationTransactionException
- in case of system errorsdoSuspend(java.lang.Object)
protected boolean shouldCommitOnGlobalRollbackOnly()
doCommit
on a transaction that has been
marked as rollback-only in a global fashion.
Does not apply if an application locally sets the transaction to rollback-only via the TransactionStatus, but only to the transaction itself being marked as rollback-only by the transaction coordinator.
Default is "false": Local transaction strategies usually don't hold the rollback-only marker in the transaction itself, therefore they can't handle rollback-only transactions as part of transaction commit. Hence, AbstractPlatformTransactionManager will trigger a rollback in that case, throwing an UnexpectedRollbackException afterwards.
Override this to return "true" if the concrete transaction manager expects a
doCommit
call even for a rollback-only transaction, allowing for
special handling there. This will, for example, be the case for JTA, where
UserTransaction.commit
will check the read-only flag itself and
throw a corresponding RollbackException, which might include the specific reason
(such as a transaction timeout).
If this method returns "true" but the doCommit
implementation does not
throw an exception, this transaction manager will throw an UnexpectedRollbackException
itself. This should not be the typical case; it is mainly checked to cover misbehaving
JTA providers that silently roll back even when the rollback has not been requested
by the calling code.
doCommit
on a transaction that has been marked as rollback-only in a global fashion.doCommit(io.micronaut.transaction.support.DefaultTransactionStatus<T>)
,
DefaultTransactionStatus.isGlobalRollbackOnly()
,
AbstractTransactionStatus.isLocalRollbackOnly()
,
TransactionExecution.setRollbackOnly()
,
UnexpectedRollbackException
protected void prepareForCommit(DefaultTransactionStatus<T> status)
beforeCommit
synchronization callbacks occur.
Note that exceptions will get propagated to the commit caller and cause a rollback of the transaction.
status
- the status representation of the transactionjava.lang.RuntimeException
- in case of errors; will be propagated to the caller
(note: do not throw TransactionException subclasses here!)protected abstract void doCommit(DefaultTransactionStatus<T> 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 transactionTransactionException
- in case of commit or system errorsDefaultTransactionStatus.getTransaction()
protected abstract void doRollback(DefaultTransactionStatus<T> 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 transactionTransactionException
- in case of system errorsDefaultTransactionStatus.getTransaction()
protected void doSetRollbackOnly(DefaultTransactionStatus<T> status) throws TransactionException
The 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 transactionTransactionException
- in case of system errorsprotected void registerAfterCompletionWithExistingTransaction(@NonNull SynchronousTransactionState state, java.lang.Object transaction, java.util.List<TransactionSynchronization> synchronizations) throws TransactionException
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.
state
- The transaction statetransaction
- transaction object returned by doGetTransaction
synchronizations
- a List of TransactionSynchronization objectsTransactionException
- in case of system errorsinvokeAfterCompletion(SynchronousTransactionState, List, TransactionSynchronization.Status)
,
TransactionSynchronization.afterCompletion(TransactionSynchronization.Status)
,
TransactionSynchronization.Status.UNKNOWN
protected void doCleanupAfterCompletion(@NonNull SynchronousTransactionState state, 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.
state
- The statetransaction
- transaction object returned by doGetTransaction
protected void doDestroyState(@NonNull SynchronousTransactionState state)
state
- The state