Package org.springframework.transaction.reactive

# Class AbstractReactiveTransactionManager

java.lang.Object
org.springframework.transaction.reactive.AbstractReactiveTransactionManager

All Implemented Interfaces:
  `Serializable`, `ConfigurableTransactionManager`, `ReactiveTransactionManager`, `TransactionManager`

public abstract class AbstractReactiveTransactionManager extends Object implements ReactiveTransactionManager, ConfigurableTransactionManager, Serializable
Abstract base class that implements Spring's standard reactive transaction workflow, serving as basis for concrete platform transaction managers.

This base class provides the following workflow handling:

- determines if there is an existing transaction;
- applies the appropriate propagation behavior;
- suspends and resumes transactions if necessary;
- checks the rollback-only flag on commit;
- applies the appropriate modification on rollback (actual rollback or setting rollback-only);
- triggers registered synchronization callbacks.

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 R2DBC, MongoDB, etc. 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.

Since:
  5.2
Author:
  Mark Paluch, Juergen Hoeller
See Also:
  - `TransactionSynchronizationManager`
  - Serialized Form

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`protected static final class`
`AbstractReactiveTransactionManager.SuspendedResourcesHolder`
Holder for suspended resources.

## Field Summary

Fields
Modifier and Type
Field
Description
`protected org.apache.commons.logging.Log`
`logger`

## Constructor Summary

Constructors
Constructor
Description
`AbstractReactiveTransactionManager()`

## Method Summary

Modifier and Type
Method
Description
`final reactor.core.publisher.Mono<Void>`
`commit(ReactiveTransaction transaction)`
This implementation of commit handles participating in existing transactions and programmatic rollback requests.
`protected abstract reactor.core.publisher.Mono<Void>`
`doBegin(TransactionSynchronizationManager synchronizationManager, Object transaction, TransactionDefinition definition)`
Begin a new transaction with semantics according to the given transaction definition.
`protected reactor.core.publisher.Mono<Void>`
`doCleanupAfterCompletion(TransactionSynchronizationManager synchronizationManager, Object transaction)`
Cleanup resources after transaction completion.
`protected abstract reactor.core.publisher.Mono<Void>`
`doCommit(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status)`
Perform an actual commit of the given transaction.
`protected abstract Object`
`doGetTransaction(TransactionSynchronizationManager synchronizationManager)`
Return a transaction object for the current transaction state.
`protected reactor.core.publisher.Mono<Void>`
`doResume(TransactionSynchronizationManager synchronizationManager, Object transaction, Object suspendedResources)`
Resume the resources of the current transaction.
`protected abstract reactor.core.publisher.Mono<Void>`
`doRollback(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status)`
Perform an actual rollback of the given transaction.
`protected reactor.core.publisher.Mono<Void>`
`doSetRollbackOnly(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status)`
Set the given transaction rollback-only.
`protected reactor.core.publisher.Mono<Object>`
`doSuspend(TransactionSynchronizationManager synchronizationManager, Object transaction)`
Suspend the resources of the current transaction.
`final reactor.core.publisher.Mono<ReactiveTransaction>`
`getReactiveTransaction(TransactionDefinition definition)`
This implementation handles propagation behavior.
`final Collection<TransactionExecutionListener>`
`getTransactionExecutionListeners()`
Return the registered transaction execution listeners for this transaction manager.
`protected boolean`
`isExistingTransaction(Object transaction)`
Check if the given transaction object indicates an existing transaction (that is, a transaction which has already started).
`protected reactor.core.publisher.Mono<Void>`
`prepareForCommit(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status)`
Make preparations for commit, to be performed before the `beforeCommit` synchronization callbacks occur.
`protected reactor.core.publisher.Mono<Void>`
`registerAfterCompletionWithExistingTransaction(TransactionSynchronizationManager synchronizationManager, Object transaction, List<TransactionSynchronization> synchronizations)`
Register the given list of transaction synchronizations with the existing transaction.
`final reactor.core.publisher.Mono<Void>`
`rollback(ReactiveTransaction transaction)`
This implementation of rollback handles participating in existing transactions.
`final void`
`setTransactionExecutionListeners(Collection<TransactionExecutionListener> listeners)`
Set the transaction execution listeners for begin/commit/rollback callbacks from this transaction manager.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.springframework.transaction.ConfigurableTransactionManager

`addListener`

## Field Details

### logger

protected transient org.apache.commons.logging.Log logger

## Constructor Details

### AbstractReactiveTransactionManager

public AbstractReactiveTransactionManager()

## Method Details

### setTransactionExecutionListeners

public final void setTransactionExecutionListeners(Collection<TransactionExecutionListener> listeners)
Description copied from interface: `ConfigurableTransactionManager`
Set the transaction execution listeners for begin/commit/rollback callbacks from this transaction manager.

Specified by:
  `setTransactionExecutionListeners` in interface `ConfigurableTransactionManager`
See Also:
  - `ConfigurableTransactionManager.addListener(org.springframework.transaction.TransactionExecutionListener)`

### getTransactionExecutionListeners

public final Collection<TransactionExecutionListener> getTransactionExecutionListeners()
Description copied from interface: `ConfigurableTransactionManager`
Return the registered transaction execution listeners for this transaction manager.

Specified by:
  `getTransactionExecutionListeners` in interface `ConfigurableTransactionManager`
See Also:
  - `ConfigurableTransactionManager.setTransactionExecutionListeners(java.util.Collection<org.springframework.transaction.TransactionExecutionListener>)`

### getReactiveTransaction

public final reactor.core.publisher.Mono<ReactiveTransaction> getReactiveTransaction(@Nullable TransactionDefinition definition)
This implementation handles propagation behavior. Delegates to `doGetTransaction`, `isExistingTransaction` and `doBegin`.

Specified by:
  `getReactiveTransaction` in interface `ReactiveTransactionManager`
Parameters:
  `definition` - the TransactionDefinition instance, describing propagation behavior, isolation level, timeout etc.
Returns:
  transaction status object representing the new or current transaction
See Also:
  - `doGetTransaction(org.springframework.transaction.reactive.TransactionSynchronizationManager)`
  - `isExistingTransaction(java.lang.Object)`
  - `doBegin(org.springframework.transaction.reactive.TransactionSynchronizationManager, java.lang.Object, org.springframework.transaction.TransactionDefinition)`

### commit

public final reactor.core.publisher.Mono<Void> commit(ReactiveTransaction transaction)
This implementation of commit handles participating in existing transactions and programmatic rollback requests. Delegates to `isRollbackOnly`, `doCommit` and `rollback`.

Specified by:
  `commit` in interface `ReactiveTransactionManager`
Parameters:
  `transaction` - object returned by the `getTransaction` method
See Also:
  - `TransactionExecution.isRollbackOnly()`
  - `doCommit(org.springframework.transaction.reactive.TransactionSynchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction)`
  - `rollback(org.springframework.transaction.ReactiveTransaction)`

### rollback

public final reactor.core.publisher.Mono<Void> rollback(ReactiveTransaction transaction)
This implementation of rollback handles participating in existing transactions. Delegates to `doRollback` and `doSetRollbackOnly`.

Specified by:
  `rollback` in interface `ReactiveTransactionManager`
Parameters:
  `transaction` - object returned by the `getTransaction` method
See Also:
  - `doRollback(org.springframework.transaction.reactive.TransactionSynchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction)`
  - `doSetRollbackOnly(org.springframework.transaction.reactive.TransactionSynchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction)`

### doGetTransaction

protected abstract Object doGetTransaction(TransactionSynchronizationManager synchronizationManager)
Return a transaction object for the current transaction state.

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 (for example, doBegin and doCommit), either directly or as part of a DefaultReactiveTransactionStatus 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.

Parameters:
  `synchronizationManager` - the synchronization manager bound to the current transaction
Returns:
  the current transaction object
Throws:
  `CannotCreateTransactionException` - if transaction support is not available
See Also:
  - `doBegin(org.springframework.transaction.reactive.TransactionSynchronizationManager, java.lang.Object, org.springframework.transaction.TransactionDefinition)`
  - `doCommit(org.springframework.transaction.reactive.TransactionSynchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction)`
  - `doRollback(org.springframework.transaction.reactive.TransactionSynchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction)`
  - `GenericReactiveTransaction.getTransaction()`

### isExistingTransaction

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

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.

Parameters:
  `transaction` - the transaction object returned by doGetTransaction
Returns:
  if there is an existing transaction
See Also:
  - `doGetTransaction(org.springframework.transaction.reactive.TransactionSynchronizationManager)`

### doBegin

protected abstract reactor.core.publisher.Mono<Void> doBegin(TransactionSynchronizationManager synchronizationManager, Object transaction, TransactionDefinition definition)
Begin a new transaction with semantics according to the given transaction definition. Does not have to care about applying the propagation behavior, as this has already been handled by this abstract manager.

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: 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.

Parameters:
  `synchronizationManager` - the synchronization manager bound to the new transaction
  `transaction` - the transaction object returned by `doGetTransaction`
  `definition` - a TransactionDefinition instance, describing propagation behavior, isolation level, read-only flag, timeout, and transaction name
Throws:
  `NestedTransactionNotSupportedException` - if the underlying transaction does not support nesting (for example, through savepoints)

### doSuspend

protected reactor.core.publisher.Mono<Object> doSuspend(TransactionSynchronizationManager synchronizationManager, Object transaction)
Suspend the resources of the current transaction. Transaction synchronization will already have been suspended.

The default implementation throws a TransactionSuspensionNotSupportedException, assuming that transaction suspension is generally not supported.

Parameters:
  `synchronizationManager` - the synchronization manager bound to the current transaction
  `transaction` - the transaction object returned by `doGetTransaction`
Returns:
  an object that holds suspended resources (will be kept unexamined for passing it into doResume)
Throws:
  `TransactionSuspensionNotSupportedException` - if suspending is not supported by the transaction manager implementation
See Also:
  - `doResume(org.springframework.transaction.reactive.TransactionSynchronizationManager, java.lang.Object, java.lang.Object)`

### doResume

protected reactor.core.publisher.Mono<Void> doResume(TransactionSynchronizationManager synchronizationManager, @Nullable Object transaction, Object suspendedResources)
Resume the resources of the current transaction. Transaction synchronization will be resumed afterwards.

The default implementation throws a TransactionSuspensionNotSupportedException, assuming that transaction suspension is generally not supported.

Parameters:
  `synchronizationManager` - the synchronization manager bound to the current transaction
  `transaction` - the transaction object returned by `doGetTransaction`
  `suspendedResources` - the object that holds suspended resources, as returned by doSuspend
Throws:
  `TransactionSuspensionNotSupportedException` - if suspending is not supported by the transaction manager implementation
See Also:
  - `doSuspend(org.springframework.transaction.reactive.TransactionSynchronizationManager, java.lang.Object)`

### prepareForCommit

protected reactor.core.publisher.Mono<Void> prepareForCommit(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status)
Make preparations for commit, to be performed before the `beforeCommit` synchronization callbacks occur.

Note that exceptions will get propagated to the commit caller and cause a rollback of the transaction.

Parameters:
  `synchronizationManager` - the synchronization manager bound to the current transaction
  `status` - the status representation of the transaction
Throws:
  `RuntimeException` - in case of errors; will be propagated to the caller (note: do not throw TransactionException subclasses here!)

### doCommit

protected abstract reactor.core.publisher.Mono<Void> doCommit(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status)
Perform an actual commit of the given transaction.

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.

Parameters:
  `synchronizationManager` - the synchronization manager bound to the current transaction
  `status` - the status representation of the transaction
See Also:
  - `GenericReactiveTransaction.getTransaction()`

### doRollback

protected abstract reactor.core.publisher.Mono<Void> doRollback(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status)
Perform an actual rollback of the given transaction.

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.

Parameters:
  `synchronizationManager` - the synchronization manager bound to the current transaction
  `status` - the status representation of the transaction
See Also:
  - `GenericReactiveTransaction.getTransaction()`

### doSetRollbackOnly

protected reactor.core.publisher.Mono<Void> doSetRollbackOnly(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status)
Set the given transaction rollback-only. Only called on rollback if the current transaction participates in an existing one.

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.

Parameters:
  `synchronizationManager` - the synchronization manager bound to the current transaction
  `status` - the status representation of the transaction

### registerAfterCompletionWithExistingTransaction

protected reactor.core.publisher.Mono<Void> registerAfterCompletionWithExistingTransaction(TransactionSynchronizationManager synchronizationManager, Object transaction, List<TransactionSynchronization> synchronizations)
Register the given list of transaction synchronizations with the existing transaction.

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.

Parameters:
  `synchronizationManager` - the synchronization manager bound to the current transaction
  `transaction` - the transaction object returned by `doGetTransaction`
  `synchronizations` - a List of TransactionSynchronization objects
See Also:
  - `invokeAfterCompletion(TransactionSynchronizationManager, List, int)`
  - `TransactionSynchronization.afterCompletion(int)`
  - `TransactionSynchronization.STATUS_UNKNOWN`

### doCleanupAfterCompletion

protected reactor.core.publisher.Mono<Void> doCleanupAfterCompletion(TransactionSynchronizationManager synchronizationManager, Object transaction)
Cleanup resources after transaction completion.

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.

Parameters:
  `synchronizationManager` - the synchronization manager bound to the current transaction
  `transaction` - the transaction object returned by `doGetTransaction`