public class TransactionAwareConnectionFactoryProxy extends DelegatingConnectionFactory
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
Connection
s in order to handle close calls properly.
Use Wrapped.unwrap()
to retrieve the native R2DBC Connection.
ConnectionFactory.create()
,
Connection.close()
,
ConnectionFactoryUtils.doGetConnection(io.r2dbc.spi.ConnectionFactory)
,
ConnectionFactoryUtils.doReleaseConnection(io.r2dbc.spi.Connection, io.r2dbc.spi.ConnectionFactory)
Constructor and Description |
---|
TransactionAwareConnectionFactoryProxy(ConnectionFactory targetConnectionFactory)
Create a new
TransactionAwareConnectionFactoryProxy . |
Modifier and Type | Method and Description |
---|---|
reactor.core.publisher.Mono<Connection> |
create()
Delegates to
ConnectionFactoryUtils for automatically participating
in Spring-managed transactions. |
protected reactor.core.publisher.Mono<Connection> |
getTransactionAwareConnectionProxy(ConnectionFactory targetConnectionFactory)
Wraps the given
Connection with a proxy that delegates every method call
to it but delegates close() calls to ConnectionFactoryUtils . |
getMetadata, getTargetConnectionFactory, unwrap
public TransactionAwareConnectionFactoryProxy(ConnectionFactory targetConnectionFactory)
TransactionAwareConnectionFactoryProxy
.targetConnectionFactory
- the target ConnectionFactory
public reactor.core.publisher.Mono<Connection> create()
ConnectionFactoryUtils
for automatically participating
in Spring-managed transactions.create
in interface ConnectionFactory
create
in class DelegatingConnectionFactory
Connection
if any, a new one else.ConnectionFactoryUtils.doGetConnection(io.r2dbc.spi.ConnectionFactory)
protected reactor.core.publisher.Mono<Connection> getTransactionAwareConnectionProxy(ConnectionFactory targetConnectionFactory)
Connection
with a proxy that delegates every method call
to it but delegates close()
calls to ConnectionFactoryUtils
.targetConnectionFactory
- the ConnectionFactory
that the Connection
came fromConnection
.Connection.close()
,
ConnectionFactoryUtils.doReleaseConnection(io.r2dbc.spi.Connection, io.r2dbc.spi.ConnectionFactory)