Class ConnectionFactoryUtils
ConnectionFactory.
 Used internally by Spring's DatabaseClient, Spring's R2DBC operation
 objects. Can also be used directly in application code.
- Since:
- 5.3
- Author:
- Mark Paluch, Christoph Strobl
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intOrder value for ReactiveTransactionSynchronization objects that clean up R2DBC Connections.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic DataAccessExceptionconvertR2dbcException(String task, @Nullable String sql, R2dbcException ex) Translate the givenR2dbcExceptioninto a genericDataAccessException.static reactor.core.publisher.Mono<ConnectionFactory>currentConnectionFactory(ConnectionFactory connectionFactory) Obtain theConnectionFactoryfrom the currentTransactionSynchronizationManager.static reactor.core.publisher.Mono<Connection>doGetConnection(ConnectionFactory connectionFactory) Actually obtain a R2DBC Connection from the givenConnectionFactory.static reactor.core.publisher.Mono<Void>doReleaseConnection(Connection connection, ConnectionFactory connectionFactory) Actually close the givenConnection, obtained from the givenConnectionFactory.static reactor.core.publisher.Mono<Connection>getConnection(ConnectionFactory connectionFactory) Obtain aConnectionfrom the givenConnectionFactory.static ConnectionReturn the innermost targetConnectionof the givenConnection.static reactor.core.publisher.Mono<Void>releaseConnection(Connection con, ConnectionFactory connectionFactory) Close the givenConnection, obtained from the givenConnectionFactory, if it is not managed externally (that is, not bound to the subscription).
- 
Field Details- 
CONNECTION_SYNCHRONIZATION_ORDERpublic static final int CONNECTION_SYNCHRONIZATION_ORDEROrder value for ReactiveTransactionSynchronization objects that clean up R2DBC Connections.- See Also:
 
 
- 
- 
Constructor Details- 
ConnectionFactoryUtilspublic ConnectionFactoryUtils()
 
- 
- 
Method Details- 
getConnectionpublic static reactor.core.publisher.Mono<Connection> getConnection(ConnectionFactory connectionFactory) Obtain aConnectionfrom the givenConnectionFactory. 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 theTransactionSynchronizationManagerif transaction synchronization is active.- Parameters:
- connectionFactory- the- ConnectionFactoryto obtain- Connectionsfrom
- Returns:
- a R2DBC Connection from the given ConnectionFactory
- Throws:
- DataAccessResourceFailureException- if the attempt to get a- Connectionfailed
- See Also:
 
- 
doGetConnectionpublic static reactor.core.publisher.Mono<Connection> doGetConnection(ConnectionFactory connectionFactory) Actually obtain a R2DBC Connection from the givenConnectionFactory. Same asgetConnection(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 theTransactionSynchronizationManagerif transaction synchronization is active- Parameters:
- connectionFactory- the- ConnectionFactoryto obtain Connections from
- Returns:
- a R2DBC Connectionfrom the givenConnectionFactory.
 
- 
releaseConnectionpublic static reactor.core.publisher.Mono<Void> releaseConnection(Connection con, ConnectionFactory connectionFactory) Close the givenConnection, obtained from the givenConnectionFactory, if it is not managed externally (that is, not bound to the subscription).- Parameters:
- con- the- Connectionto close if necessary
- connectionFactory- the- ConnectionFactorythat the Connection was obtained from
- See Also:
 
- 
doReleaseConnectionpublic static reactor.core.publisher.Mono<Void> doReleaseConnection(Connection connection, ConnectionFactory connectionFactory) Actually close the givenConnection, obtained from the givenConnectionFactory. Same asreleaseConnection(io.r2dbc.spi.Connection, io.r2dbc.spi.ConnectionFactory), but preserving the original exception.- Parameters:
- connection- the- Connectionto close if necessary
- connectionFactory- the- ConnectionFactorythat the Connection was obtained from
- See Also:
 
- 
currentConnectionFactorypublic static reactor.core.publisher.Mono<ConnectionFactory> currentConnectionFactory(ConnectionFactory connectionFactory) Obtain theConnectionFactoryfrom the currentTransactionSynchronizationManager.- Parameters:
- connectionFactory- the- ConnectionFactorythat the Connection was obtained from
- See Also:
 
- 
convertR2dbcExceptionpublic static DataAccessException convertR2dbcException(String task, @Nullable String sql, R2dbcException ex) Translate the givenR2dbcExceptioninto a genericDataAccessException.The returned DataAccessException is supposed to contain the original R2dbcExceptionas 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, agetRootCause() instanceof R2dbcExceptioncheck (and subsequent cast) is considered reliable when expecting R2DBC-based access to have happened.- Parameters:
- task- readable text describing the task being attempted
- sql- the SQL query or update that caused the problem (if known)
- ex- the offending- R2dbcException
- Returns:
- the corresponding DataAccessException instance
 
- 
getTargetConnectionReturn the innermost targetConnectionof the givenConnection. If the givenConnectionis wrapped, it will be unwrapped until a plainConnectionis found. Otherwise, the passed-in Connection will be returned as-is.- Parameters:
- con- the- Connectionwrapper to unwrap
- Returns:
- the innermost target Connection, or the passed-in one if not wrapped
- See Also:
 
 
-