Class R2dbcTransactionManager

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

public class R2dbcTransactionManager extends AbstractReactiveTransactionManager implements InitializingBean
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 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.

Mark Paluch, Juergen Hoeller
See Also: