Top Description Inners Fields Constructors Methods
org.springframework.r2dbc.connection

public Class R2dbcTransactionManager

extends AbstractReactiveTransactionManager
implements InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean
Annotations
@SuppressWarnings:serial
Imports
java.time.Duration, io.r2dbc.spi.Connection, .ConnectionFactory, .IsolationLevel, .Option, .R2dbcException, .Result, reactor.core.publisher.Mono, org.springframework.beans.factory.InitializingBean, org.springframework.lang.Nullable, org.springframework.transaction.CannotCreateTransactionException, .TransactionDefinition, org.springframework.transaction.reactive.AbstractReactiveTransactionManager, .GenericReactiveTransaction, .TransactionSynchronizationManager, org.springframework.util.Assert

org.springframework.transaction.ReactiveTransactionManager implementation for a single R2DBC ConnectionFactory. This class is capable of working in any environment with any R2DBC driver, as long as the setup uses a ConnectionFactory as its Connection factory mechanism. Binds a R2DBC Connection from the specified ConnectionFactory to the current subscriber context, potentially allowing for one context-bound Connection per ConnectionFactory.

Note: The ConnectionFactory that this transaction manager operates on needs to return independent Connections. The Connections typically come from a connection pool but the ConnectionFactory must not return specifically scoped or constrained Connections. This transaction manager will associate Connection with context-bound transactions, according to the specified propagation behavior. It assumes that a separate, independent Connection can be obtained even during an ongoing transaction.

Application code is required to retrieve the R2DBC Connection via ConnectionFactoryUtils#getConnection(ConnectionFactory) instead of a standard R2DBC-style ConnectionFactory#create() call. Spring classes such as DatabaseClient use this strategy implicitly. If not used in combination with this transaction manager, the ConnectionFactoryUtils lookup strategy behaves exactly like the native ConnectionFactory lookup; it can thus be used in a portable fashion.

Alternatively, you can allow application code to work with the lookup pattern ConnectionFactory#create(), for example for code not aware of Spring. In that case, define a TransactionAwareConnectionFactoryProxy for your target ConnectionFactory, and pass that proxy ConnectionFactory to your DAOs which will automatically participate in Spring-managed transactions when accessing it.

Spring's TransactionDefinition attributes are carried forward to R2DBC drivers using extensible R2DBC io.r2dbc.spi.TransactionDefinition. Subclasses may override createTransactionDefinition(TransactionDefinition) to customize transaction definitions for vendor-specific attributes. As of 6.0.10, this transaction manager supports nested transactions via R2DBC savepoints as well.

Authors
Mark Paluch, Juergen Hoeller
Since
5.3
See Also
ConnectionFactoryUtils#getConnection(ConnectionFactory), ConnectionFactoryUtils#releaseConnection, TransactionAwareConnectionFactoryProxy

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
R2dbcTransactionManager.ConnectionFactoryTransactionObject

ConnectionFactory transaction object, representing a ConnectionHolder.

private static record
R2dbcTransactionManager.ExtendedTransactionDefinition

Extended R2DBC transaction definition object providing transaction attributes to R2DBC drivers when starting a transaction.

Field Summary

Modifier and TypeField and Description
private ConnectionFactory
private boolean
Inherited from org.springframework.transaction.reactive.AbstractReactiveTransactionManager:
logger

Constructor Summary

AccessConstructor and Description
public
R2dbcTransactionManager()

Create a new R2dbcTransactionManager instance.

public
R2dbcTransactionManager(ConnectionFactory
the R2DBC ConnectionFactory to manage transactions for
connectionFactory
)

Create a new R2dbcTransactionManager instance.

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

protected TransactionDefinition

Returns:

the actual transaction definition to use
createTransactionDefinition
(TransactionDefinition
the transaction definition
definition
)

Determine the transaction definition from our TransactionDefinition.

protected Duration

Returns:

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

Determine the actual timeout to use for the given definition.

protected Mono<Void>
doBegin(TransactionSynchronizationManager
the synchronization manager bound to the new transaction
synchronizationManager
,
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
)

Implements abstract org.springframework.transaction.reactive.AbstractReactiveTransactionManager.doBegin.

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

private Mono<Void>
protected Mono<Void>
doCleanupAfterCompletion(TransactionSynchronizationManager
the synchronization manager bound to the current transaction
synchronizationManager
,
Object
the transaction object returned by doGetTransaction
transaction
)

Overrides org.springframework.transaction.reactive.AbstractReactiveTransactionManager.doCleanupAfterCompletion.

Cleanup resources after transaction completion.

protected Mono<Void>
doCommit(TransactionSynchronizationManager
the synchronization manager bound to the current transaction
TransactionSynchronizationManager
,
GenericReactiveTransaction
the status representation of the transaction
status
)

Implements abstract org.springframework.transaction.reactive.AbstractReactiveTransactionManager.doCommit.

Perform an actual commit of the given transaction.

protected Object
doGetTransaction(TransactionSynchronizationManager
the synchronization manager bound to the current transaction
synchronizationManager
)

Implements abstract org.springframework.transaction.reactive.AbstractReactiveTransactionManager.doGetTransaction.

Return a transaction object for the current transaction state.

protected Mono<Void>
doResume(TransactionSynchronizationManager
the synchronization manager bound to the current transaction
synchronizationManager
,
Object
the transaction object returned by doGetTransaction
transaction
,
Object
the object that holds suspended resources, as returned by doSuspend
suspendedResources
)

Overrides org.springframework.transaction.reactive.AbstractReactiveTransactionManager.doResume.

Resume the resources of the current transaction.

protected Mono<Void>
doRollback(TransactionSynchronizationManager
the synchronization manager bound to the current transaction
TransactionSynchronizationManager
,
GenericReactiveTransaction
the status representation of the transaction
status
)

Implements abstract org.springframework.transaction.reactive.AbstractReactiveTransactionManager.doRollback.

Perform an actual rollback of the given transaction.

protected Mono<Void>
doSetRollbackOnly(TransactionSynchronizationManager
the synchronization manager bound to the current transaction
synchronizationManager
,
GenericReactiveTransaction
the status representation of the transaction
status
)

Overrides org.springframework.transaction.reactive.AbstractReactiveTransactionManager.doSetRollbackOnly.

Set the given transaction rollback-only.

protected Mono<Object>
doSuspend(TransactionSynchronizationManager
the synchronization manager bound to the current transaction
synchronizationManager
,
Object
the transaction object returned by doGetTransaction
transaction
)

Overrides org.springframework.transaction.reactive.AbstractReactiveTransactionManager.doSuspend.

Suspend the resources of the current transaction.

public ConnectionFactory
getConnectionFactory()

Return the R2DBC ConnectionFactory that this instance manages transactions for.

public boolean
isEnforceReadOnly()

Return whether to enforce the read-only nature of a transaction through an explicit statement on the transactional connection.

protected boolean
isExistingTransaction(Object
the transaction object returned by doGetTransaction
transaction
)

Overrides org.springframework.transaction.reactive.AbstractReactiveTransactionManager.isExistingTransaction.

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

protected ConnectionFactory

Returns:

the ConnectionFactory (never null)
obtainConnectionFactory
()

Obtain the ConnectionFactory for actual use.

protected Mono<Void>
prepareTransactionalConnection(Connection
the transactional R2DBC Connection
con
,
TransactionDefinition
the current transaction definition
definition
)

Prepare the transactional Connection right after transaction begin.

protected IsolationLevel

Returns:

the resolved isolation level. Can be null if not resolvable or the isolation level should remain default.
resolveIsolationLevel
(int
the isolation level to translate.
isolationLevel
)

Resolve the isolation level constant to a R2DBC IsolationLevel.

public void
setConnectionFactory(ConnectionFactory connectionFactory)

Set the R2DBC ConnectionFactory that this instance should manage transactions for.

public void
setEnforceReadOnly(boolean enforceReadOnly)

Specify whether to enforce the read-only nature of a transaction (as indicated by TransactionDefinition#isReadOnly()) through an explicit statement on the transactional connection: "SET TRANSACTION READ ONLY" as understood by Oracle, MySQL and Postgres.

protected RuntimeException

Returns:

the translated exception to emit
translateException
(String
the task description (commit or rollback).
task
,
R2dbcException
the SQLException thrown from commit/rollback.
ex
)

Translate the given R2DBC commit/rollback exception to a common Spring exception to propagate from the commit/rollback call.

Inherited from org.springframework.transaction.reactive.AbstractReactiveTransactionManager:
commitgetReactiveTransactiongetTransactionExecutionListenersprepareForCommitregisterAfterCompletionWithExistingTransactionrollbacksetTransactionExecutionListeners