Class TransactionAwareConnectionFactoryProxy

java.lang.Object
org.springframework.r2dbc.connection.DelegatingConnectionFactory
org.springframework.r2dbc.connection.TransactionAwareConnectionFactoryProxy
All Implemented Interfaces:
ConnectionFactory, Wrapped<ConnectionFactory>

public class TransactionAwareConnectionFactoryProxy extends DelegatingConnectionFactory
Proxy for a target R2DBC ConnectionFactory, adding awareness of Spring-managed transactions.

Data access code that should remain unaware of Spring's data access support can work with this proxy to seamlessly participate in Spring-managed transactions. Note that the transaction manager, for example R2dbcTransactionManager, still needs to work with the underlying ConnectionFactory, not with this proxy.

Make sure that TransactionAwareConnectionFactoryProxy is the outermost ConnectionFactory of a chain of ConnectionFactory proxies/adapters. TransactionAwareConnectionFactoryProxy can delegate either directly to the target connection pool or to some intermediary proxy/adapter.

Delegates to ConnectionFactoryUtils for automatically participating in thread-bound transactions, for example managed by R2dbcTransactionManager. create() calls and close calls on returned Connection will behave properly within a transaction, i.e. always operate on the transactional Connection. If not within a transaction, normal ConnectionFactory behavior applies.

This proxy allows data access code to work with the plain R2DBC API. However, if possible, use Spring's ConnectionFactoryUtils or DatabaseClient to get transaction participation even without a proxy for the target ConnectionFactory, avoiding the need to define such a proxy in the first place.

NOTE: This ConnectionFactory proxy needs to return wrapped Connections in order to handle close calls properly. Use Wrapped.unwrap() to retrieve the native R2DBC Connection.

Since:
5.3
Author:
Mark Paluch, Christoph Strobl
See Also: