Class ConnectionFactoryUtils

java.lang.Object
org.springframework.r2dbc.connection.ConnectionFactoryUtils

public abstract class ConnectionFactoryUtils extends Object
Helper class that provides static methods for obtaining R2DBC Connections from a 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 Details

    • CONNECTION_SYNCHRONIZATION_ORDER

      public static final int CONNECTION_SYNCHRONIZATION_ORDER
      Order value for ReactiveTransactionSynchronization objects that clean up R2DBC Connections.
      See Also:
  • Constructor Details

    • ConnectionFactoryUtils

      public ConnectionFactoryUtils()
  • Method Details

    • getConnection

      public static reactor.core.publisher.Mono<Connection> getConnection(ConnectionFactory connectionFactory)
      Obtain a 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.

      Parameters:
      connectionFactory - the ConnectionFactory to obtain Connections from
      Returns:
      a R2DBC Connection from the given ConnectionFactory
      Throws:
      org.springframework.dao.DataAccessResourceFailureException - if the attempt to get a Connection failed
      See Also:
    • doGetConnection

      public static reactor.core.publisher.Mono<Connection> doGetConnection(ConnectionFactory connectionFactory)
      Actually obtain a R2DBC Connection from the given ConnectionFactory. Same as getConnection(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

      Parameters:
      connectionFactory - the ConnectionFactory to obtain Connections from
      Returns:
      a R2DBC Connection from the given ConnectionFactory.
    • releaseConnection

      public static reactor.core.publisher.Mono<Void> releaseConnection(Connection con, ConnectionFactory connectionFactory)
      Close the given Connection, obtained from the given ConnectionFactory, if it is not managed externally (that is, not bound to the subscription).
      Parameters:
      con - the Connection to close if necessary
      connectionFactory - the ConnectionFactory that the Connection was obtained from
      See Also:
    • doReleaseConnection

      public static reactor.core.publisher.Mono<Void> doReleaseConnection(Connection connection, ConnectionFactory connectionFactory)
      Actually close the given Connection, obtained from the given ConnectionFactory. Same as releaseConnection(Connection, ConnectionFactory), but preserving the original exception.
      Parameters:
      connection - the Connection to close if necessary
      connectionFactory - the ConnectionFactory that the Connection was obtained from
      See Also:
    • currentConnectionFactory

      public static reactor.core.publisher.Mono<ConnectionFactory> currentConnectionFactory(ConnectionFactory connectionFactory)
      Obtain the ConnectionFactory from the current TransactionSynchronizationManager.
      Parameters:
      connectionFactory - the ConnectionFactory that the Connection was obtained from
      See Also:
      • TransactionSynchronizationManager
    • convertR2dbcException

      public static org.springframework.dao.DataAccessException convertR2dbcException(String task, @Nullable String sql, R2dbcException ex)
      Translate the given 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.

      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
    • getTargetConnection

      public static Connection getTargetConnection(Connection con)
      Return the innermost target 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.
      Parameters:
      con - the Connection wrapper to unwrap
      Returns:
      the innermost target Connection, or the passed-in one if not wrapped
      See Also: