public abstract class ConnectionFactoryUtils extends Object
ConnectionFactory
.
Used internally by Spring's DatabaseClient
, Spring's R2DBC operation
objects. Can also be used directly in application code.
R2dbcTransactionManager
,
TransactionSynchronizationManager
Modifier and Type | Field and Description |
---|---|
static int |
CONNECTION_SYNCHRONIZATION_ORDER
Order value for ReactiveTransactionSynchronization objects that clean up R2DBC Connections.
|
Constructor and Description |
---|
ConnectionFactoryUtils() |
Modifier and Type | Method and Description |
---|---|
static DataAccessException |
convertR2dbcException(String task,
String sql,
io.r2dbc.spi.R2dbcException ex)
Translate the given
R2dbcException into a generic DataAccessException . |
static reactor.core.publisher.Mono<io.r2dbc.spi.ConnectionFactory> |
currentConnectionFactory(io.r2dbc.spi.ConnectionFactory connectionFactory)
Obtain the
ConnectionFactory from the current TransactionSynchronizationManager . |
static reactor.core.publisher.Mono<io.r2dbc.spi.Connection> |
doGetConnection(io.r2dbc.spi.ConnectionFactory connectionFactory)
Actually obtain a R2DBC Connection from the given
ConnectionFactory . |
static reactor.core.publisher.Mono<Void> |
doReleaseConnection(io.r2dbc.spi.Connection connection,
io.r2dbc.spi.ConnectionFactory connectionFactory)
Actually close the given
Connection , obtained from the given
ConnectionFactory . |
static reactor.core.publisher.Mono<io.r2dbc.spi.Connection> |
getConnection(io.r2dbc.spi.ConnectionFactory connectionFactory)
Obtain a
Connection from the given ConnectionFactory . |
static io.r2dbc.spi.Connection |
getTargetConnection(io.r2dbc.spi.Connection con)
Return the innermost target
Connection of the given Connection . |
static reactor.core.publisher.Mono<Void> |
releaseConnection(io.r2dbc.spi.Connection con,
io.r2dbc.spi.ConnectionFactory connectionFactory)
Close the given
Connection , obtained from the given ConnectionFactory , if
it is not managed externally (that is, not bound to the subscription). |
public static final int CONNECTION_SYNCHRONIZATION_ORDER
public static reactor.core.publisher.Mono<io.r2dbc.spi.Connection> getConnection(io.r2dbc.spi.ConnectionFactory connectionFactory)
Connection
from the given ConnectionFactory
.
Translates exceptions into the Spring hierarchy of unchecked generic
data access exceptions, simplifying calling code and making any
exception that is thrown more meaningful.
Is aware of a corresponding Connection bound to the current
TransactionSynchronizationManager
. Will bind a Connection to the
TransactionSynchronizationManager
if transaction synchronization is active.
connectionFactory
- the ConnectionFactory
to obtain
Connections
fromConnectionFactory
DataAccessResourceFailureException
- if the attempt to get a
Connection
failedreleaseConnection(io.r2dbc.spi.Connection, io.r2dbc.spi.ConnectionFactory)
public static reactor.core.publisher.Mono<io.r2dbc.spi.Connection> doGetConnection(io.r2dbc.spi.ConnectionFactory connectionFactory)
ConnectionFactory
.
Same as getConnection(io.r2dbc.spi.ConnectionFactory)
, but preserving the original exceptions.
Is aware of a corresponding Connection bound to the current
TransactionSynchronizationManager
. Will bind a Connection to the
TransactionSynchronizationManager
if transaction synchronization is active
connectionFactory
- the ConnectionFactory
to obtain Connections fromConnection
from the given ConnectionFactory
.public static reactor.core.publisher.Mono<Void> releaseConnection(io.r2dbc.spi.Connection con, io.r2dbc.spi.ConnectionFactory connectionFactory)
Connection
, obtained from the given ConnectionFactory
, if
it is not managed externally (that is, not bound to the subscription).con
- the Connection
to close if necessaryconnectionFactory
- the ConnectionFactory
that the Connection was obtained fromgetConnection(io.r2dbc.spi.ConnectionFactory)
public static reactor.core.publisher.Mono<Void> doReleaseConnection(io.r2dbc.spi.Connection connection, io.r2dbc.spi.ConnectionFactory connectionFactory)
Connection
, obtained from the given
ConnectionFactory
. Same as releaseConnection(io.r2dbc.spi.Connection, io.r2dbc.spi.ConnectionFactory)
,
but preserving the original exception.connection
- the Connection
to close if necessaryconnectionFactory
- the ConnectionFactory
that the Connection was obtained fromdoGetConnection(io.r2dbc.spi.ConnectionFactory)
public static reactor.core.publisher.Mono<io.r2dbc.spi.ConnectionFactory> currentConnectionFactory(io.r2dbc.spi.ConnectionFactory connectionFactory)
ConnectionFactory
from the current TransactionSynchronizationManager
.connectionFactory
- the ConnectionFactory
that the Connection was obtained fromTransactionSynchronizationManager
public static DataAccessException convertR2dbcException(String task, @Nullable String sql, io.r2dbc.spi.R2dbcException ex)
R2dbcException
into a generic DataAccessException
.
The returned DataAccessException is supposed to contain the original
R2dbcException
as root cause. However, client code may not generally
rely on this due to DataAccessExceptions possibly being caused by other resource
APIs as well. That said, a getRootCause() instanceof R2dbcException
check (and subsequent cast) is considered reliable when expecting R2DBC-based
access to have happened.
task
- readable text describing the task being attemptedsql
- the SQL query or update that caused the problem (if known)ex
- the offending R2dbcException
public static io.r2dbc.spi.Connection getTargetConnection(io.r2dbc.spi.Connection con)
Connection
of the given Connection
.
If the given Connection
is wrapped, it will be unwrapped until a
plain Connection
is found. Otherwise, the passed-in Connection
will be returned as-is.con
- the Connection
wrapper to unwrapWrapped.unwrap()