Class R2dbcTransactionManager
- All Implemented Interfaces:
Serializable,org.springframework.beans.factory.InitializingBean,org.springframework.transaction.ReactiveTransactionManager,org.springframework.transaction.TransactionManager
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 may come from a pool (the typical case), but the
ConnectionFactory must not return scoped Connections
or the like. This transaction manager will associate Connection
with context-bound transactions itself, 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 standard
R2DBC lookup pattern ConnectionFactory.create(), for example for code
that is not aware of Spring at all. 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.
This transaction manager triggers flush callbacks on registered transaction
synchronizations (if synchronization is generally active), assuming resources
operating on the underlying R2DBC Connection.
Spring's TransactionDefinition attributes are carried forward to R2DBC drivers
using extensible R2DBC TransactionDefinition. Subclasses may
override createTransactionDefinition(TransactionDefinition) to customize
transaction definitions for vendor-specific attributes.
- Since:
- 5.3
- Author:
- Mark Paluch, Juergen Hoeller
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.transaction.reactive.AbstractReactiveTransactionManager
org.springframework.transaction.reactive.AbstractReactiveTransactionManager.SuspendedResourcesHolder -
Field Summary
Fields inherited from class org.springframework.transaction.reactive.AbstractReactiveTransactionManager
logger -
Constructor Summary
ConstructorsConstructorDescriptionCreate a newR2dbcTransactionManagerinstance.R2dbcTransactionManager(ConnectionFactory connectionFactory) Create a newR2dbcTransactionManagerinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected TransactionDefinitioncreateTransactionDefinition(org.springframework.transaction.TransactionDefinition definition) Determine the transaction definition from ourTransactionDefinition.protected DurationdetermineTimeout(org.springframework.transaction.TransactionDefinition definition) Determine the actual timeout to use for the given definition.protected reactor.core.publisher.Mono<Void>doBegin(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction, org.springframework.transaction.TransactionDefinition definition) protected reactor.core.publisher.Mono<Void>doCleanupAfterCompletion(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction) protected reactor.core.publisher.Mono<Void>doCommit(org.springframework.transaction.reactive.TransactionSynchronizationManager TransactionSynchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) protected ObjectdoGetTransaction(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager) protected reactor.core.publisher.Mono<Void>doResume(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction, Object suspendedResources) protected reactor.core.publisher.Mono<Void>doRollback(org.springframework.transaction.reactive.TransactionSynchronizationManager TransactionSynchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) protected reactor.core.publisher.Mono<Void>doSetRollbackOnly(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) protected reactor.core.publisher.Mono<Object>doSuspend(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction) Return the R2DBCConnectionFactorythat this instance manages transactions for.booleanReturn whether to enforce the read-only nature of a transaction through an explicit statement on the transactional connection.protected booleanisExistingTransaction(Object transaction) protected ConnectionFactoryObtain theConnectionFactoryfor actual use.protected reactor.core.publisher.Mono<Void>prepareTransactionalConnection(Connection con, org.springframework.transaction.TransactionDefinition definition) Prepare the transactionalConnectionright after transaction begin.protected IsolationLevelresolveIsolationLevel(int isolationLevel) Resolve the isolation level constant to a R2DBCIsolationLevel.voidsetConnectionFactory(ConnectionFactory connectionFactory) Set the R2DBCConnectionFactorythat this instance should manage transactions for.voidsetEnforceReadOnly(boolean enforceReadOnly) Specify whether to enforce the read-only nature of a transaction (as indicated byTransactionDefinition.isReadOnly()) through an explicit statement on the transactional connection: "SET TRANSACTION READ ONLY" as understood by Oracle, MySQL and Postgres.protected RuntimeExceptiontranslateException(String task, R2dbcException ex) Translate the given R2DBC commit/rollback exception to a common Spring exception to propagate from theAbstractReactiveTransactionManager.commit(org.springframework.transaction.ReactiveTransaction)/AbstractReactiveTransactionManager.rollback(org.springframework.transaction.ReactiveTransaction)call.Methods inherited from class org.springframework.transaction.reactive.AbstractReactiveTransactionManager
commit, getReactiveTransaction, prepareForCommit, registerAfterCompletionWithExistingTransaction, rollback
-
Constructor Details
-
R2dbcTransactionManager
public R2dbcTransactionManager()Create a newR2dbcTransactionManagerinstance. A ConnectionFactory has to be set to be able to use it. -
R2dbcTransactionManager
Create a newR2dbcTransactionManagerinstance.- Parameters:
connectionFactory- the R2DBC ConnectionFactory to manage transactions for
-
-
Method Details
-
setConnectionFactory
Set the R2DBCConnectionFactorythat this instance should manage transactions for.This will typically be a locally defined
ConnectionFactory, for example a connection pool.The
ConnectionFactorypassed in here needs to return independentConnections. TheConnections may come from a pool (the typical case), but theConnectionFactorymust not return scopedConnections or the like.- See Also:
-
getConnectionFactory
Return the R2DBCConnectionFactorythat this instance manages transactions for. -
obtainConnectionFactory
Obtain theConnectionFactoryfor actual use.- Returns:
- the
ConnectionFactory(nevernull) - Throws:
IllegalStateException- in case of no ConnectionFactory set
-
setEnforceReadOnly
public void setEnforceReadOnly(boolean enforceReadOnly) Specify whether to enforce the read-only nature of a transaction (as indicated byTransactionDefinition.isReadOnly()) through an explicit statement on the transactional connection: "SET TRANSACTION READ ONLY" as understood by Oracle, MySQL and Postgres.The exact treatment, including any SQL statement executed on the connection, can be customized through
prepareTransactionalConnection(io.r2dbc.spi.Connection, org.springframework.transaction.TransactionDefinition). -
isEnforceReadOnly
public boolean isEnforceReadOnly()Return whether to enforce the read-only nature of a transaction through an explicit statement on the transactional connection.- See Also:
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
doGetTransaction
protected Object doGetTransaction(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager) throws org.springframework.transaction.TransactionException - Specified by:
doGetTransactionin classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager- Throws:
org.springframework.transaction.TransactionException
-
isExistingTransaction
- Overrides:
isExistingTransactionin classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
-
doBegin
protected reactor.core.publisher.Mono<Void> doBegin(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction, org.springframework.transaction.TransactionDefinition definition) throws org.springframework.transaction.TransactionException - Specified by:
doBeginin classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager- Throws:
org.springframework.transaction.TransactionException
-
createTransactionDefinition
protected TransactionDefinition createTransactionDefinition(org.springframework.transaction.TransactionDefinition definition) Determine the transaction definition from ourTransactionDefinition. Can be overridden to wrap the R2DBCTransactionDefinitionto adjust or enhance transaction attributes.- Parameters:
definition- the transaction definition- Returns:
- the actual transaction definition to use
- Since:
- 6.0
- See Also:
-
determineTimeout
protected Duration determineTimeout(org.springframework.transaction.TransactionDefinition definition) Determine the actual timeout to use for the given definition. Will fall back to this manager's default timeout if the transaction definition doesn't specify a non-default value.- Parameters:
definition- the transaction definition- Returns:
- the actual timeout to use
- See Also:
-
TransactionDefinition.getTimeout()
-
doSuspend
protected reactor.core.publisher.Mono<Object> doSuspend(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction) throws org.springframework.transaction.TransactionException - Overrides:
doSuspendin classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager- Throws:
org.springframework.transaction.TransactionException
-
doResume
protected reactor.core.publisher.Mono<Void> doResume(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, @Nullable Object transaction, Object suspendedResources) throws org.springframework.transaction.TransactionException - Overrides:
doResumein classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager- Throws:
org.springframework.transaction.TransactionException
-
doCommit
protected reactor.core.publisher.Mono<Void> doCommit(org.springframework.transaction.reactive.TransactionSynchronizationManager TransactionSynchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) throws org.springframework.transaction.TransactionException - Specified by:
doCommitin classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager- Throws:
org.springframework.transaction.TransactionException
-
doRollback
protected reactor.core.publisher.Mono<Void> doRollback(org.springframework.transaction.reactive.TransactionSynchronizationManager TransactionSynchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) throws org.springframework.transaction.TransactionException - Specified by:
doRollbackin classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager- Throws:
org.springframework.transaction.TransactionException
-
doSetRollbackOnly
protected reactor.core.publisher.Mono<Void> doSetRollbackOnly(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) throws org.springframework.transaction.TransactionException - Overrides:
doSetRollbackOnlyin classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager- Throws:
org.springframework.transaction.TransactionException
-
doCleanupAfterCompletion
protected reactor.core.publisher.Mono<Void> doCleanupAfterCompletion(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction) - Overrides:
doCleanupAfterCompletionin classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
-
prepareTransactionalConnection
protected reactor.core.publisher.Mono<Void> prepareTransactionalConnection(Connection con, org.springframework.transaction.TransactionDefinition definition) Prepare the transactionalConnectionright after transaction begin.The default implementation executes a "SET TRANSACTION READ ONLY" statement if the
"enforceReadOnly"flag is set totrueand the transaction definition indicates a read-only transaction.The "SET TRANSACTION READ ONLY" is understood by Oracle, MySQL and Postgres and may work with other databases as well. If you'd like to adapt this treatment, override this method accordingly.
- Parameters:
con- the transactional R2DBC Connectiondefinition- the current transaction definition- Since:
- 5.3.22
- See Also:
-
resolveIsolationLevel
Resolve the isolation level constant to a R2DBCIsolationLevel. If you'd like to extend isolation level translation for vendor-specificIsolationLevels, override this method accordingly.- Parameters:
isolationLevel- the isolation level to translate.- Returns:
- the resolved isolation level. Can be
nullif not resolvable or the isolation level should remaindefault. - See Also:
-
TransactionDefinition.getIsolationLevel()
-
translateException
Translate the given R2DBC commit/rollback exception to a common Spring exception to propagate from theAbstractReactiveTransactionManager.commit(org.springframework.transaction.ReactiveTransaction)/AbstractReactiveTransactionManager.rollback(org.springframework.transaction.ReactiveTransaction)call.- Parameters:
task- the task description (commit or rollback).ex- the SQLException thrown from commit/rollback.- Returns:
- the translated exception to emit
-