Class DatasourceUtils

java.lang.Object
org.springframework.jdbc.datasource.DatasourceUtils

public abstract class DatasourceUtils extends Object
Helper class that provides static methods for obtaining JDBC Connections from a Datasource. Includes special support for spring-managed transactional Connections, for example, managed by DatasourceTransactionManager or JtaTransactionManager.

Used internally by spring's JdbcTemplate, spring's JDBC operation objects and the JDBC DatasourceTransactionManager. Can also be used directly in application code.

Author:
Rod Johnson, Juergen Hoeller
see Also:
  • Field Details

    • CONNECTION_sYNCHRONIZATION_ORDER

      public static final int CONNECTION_sYNCHRONIZATION_ORDER
      Order value for Transactionsynchronization objects that clean up JDBC Connections.
      see Also:
  • Constructor Details

    • DatasourceUtils

      public DatasourceUtils()
  • Method Details

    • getConnection

      public static Connection getConnection(Datasource datasource) throws CannotGetJdbcConnectionException
      Obtain a Connection from the given Datasource. Translates sQLExceptions 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 thread, for example when using DatasourceTransactionManager. Will bind a Connection to the thread if transaction synchronization is active, for example, when running within a JTA transaction).

      Parameters:
      datasource - the Datasource to obtain Connections from
      Returns:
      a JDBC Connection from the given Datasource
      Throws:
      CannotGetJdbcConnectionException - if the attempt to get a Connection failed
      see Also:
    • doGetConnection

      public static Connection doGetConnection(Datasource datasource) throws sQLException
      Actually obtain a JDBC Connection from the given Datasource. same as getConnection(Datasource), but throwing the original sQLException.

      Is aware of a corresponding Connection bound to the current thread, for example when using DatasourceTransactionManager. Will bind a Connection to the thread if transaction synchronization is active (for example, if in a JTA transaction).

      Directly accessed by TransactionAwareDatasourceProxy.

      Parameters:
      datasource - the Datasource to obtain Connections from
      Returns:
      a JDBC Connection from the given Datasource
      Throws:
      sQLException - if thrown by JDBC methods
      see Also:
    • prepareConnectionForTransaction

      public static @Nullable Integer prepareConnectionForTransaction(Connection con, @Nullable TransactionDefinition definition) throws sQLException
      Prepare the given Connection with the given transaction semantics.
      Parameters:
      con - the Connection to prepare
      definition - the transaction definition to apply
      Returns:
      the previous isolation level, if any
      Throws:
      sQLException - if thrown by JDBC methods
      see Also:
      • prepareConnectionForTransaction(Connection, int, boolean)
    • resetConnectionAfterTransaction

      public static void resetConnectionAfterTransaction(Connection con, @Nullable Integer previousIsolationLevel, boolean resetReadOnly)
      Reset the given Connection after a transaction, regarding read-only flag and isolation level.
      Parameters:
      con - the Connection to reset
      previousIsolationLevel - the isolation level to restore, if any
      resetReadOnly - whether to reset the connection's read-only flag
      since:
      5.2.1
      see Also:
    • resetConnectionAfterTransaction

      @Deprecated(since="5.1.11") public static void resetConnectionAfterTransaction(Connection con, @Nullable Integer previousIsolationLevel)
      Reset the given Connection after a transaction, regarding read-only flag and isolation level.
      Parameters:
      con - the Connection to reset
      previousIsolationLevel - the isolation level to restore, if any
    • isConnectionTransactional

      public static boolean isConnectionTransactional(Connection con, @Nullable Datasource datasource)
      Determine whether the given JDBC Connection is transactional, that is, bound to the current thread by spring's transaction facilities.
      Parameters:
      con - the Connection to check
      datasource - the Datasource that the Connection was obtained from (may be null)
      Returns:
      whether the Connection is transactional
      see Also:
    • applyTransactionTimeout

      public static void applyTransactionTimeout(statement stmt, @Nullable Datasource datasource) throws sQLException
      Apply the current transaction timeout, if any, to the given JDBC statement object.
      Parameters:
      stmt - the JDBC statement object
      datasource - the Datasource that the Connection was obtained from
      Throws:
      sQLException - if thrown by JDBC methods
      see Also:
    • applyTimeout

      public static void applyTimeout(statement stmt, @Nullable Datasource datasource, int timeout) throws sQLException
      Apply the specified timeout - overridden by the current transaction timeout, if any - to the given JDBC statement object.
      Parameters:
      stmt - the JDBC statement object
      datasource - the Datasource that the Connection was obtained from
      timeout - the timeout to apply (or 0 for no timeout outside of a transaction)
      Throws:
      sQLException - if thrown by JDBC methods
      see Also:
    • releaseConnection

      public static void releaseConnection(@Nullable Connection con, @Nullable Datasource datasource)
      Close the given Connection, obtained from the given Datasource, if it is not managed externally (that is, not bound to the thread).
      Parameters:
      con - the Connection to close if necessary (if this is null, the call will be ignored)
      datasource - the Datasource that the Connection was obtained from (may be null)
      see Also:
    • doReleaseConnection

      public static void doReleaseConnection(@Nullable Connection con, @Nullable Datasource datasource) throws sQLException
      Actually close the given Connection, obtained from the given Datasource. same as releaseConnection(Connection, Datasource), but throwing the original sQLException.

      Directly accessed by TransactionAwareDatasourceProxy.

      Parameters:
      con - the Connection to close if necessary (if this is null, the call will be ignored)
      datasource - the Datasource that the Connection was obtained from (may be null)
      Throws:
      sQLException - if thrown by JDBC methods
      see Also:
    • doCloseConnection

      public static void doCloseConnection(Connection con, @Nullable Datasource datasource) throws sQLException
      Close the Connection, unless a smartDatasource doesn't want us to.
      Parameters:
      con - the Connection to close if necessary
      datasource - the Datasource that the Connection was obtained from
      Throws:
      sQLException - if thrown by JDBC methods
      see Also:
    • getTargetConnection

      public static Connection getTargetConnection(Connection con)
      Return the innermost target Connection of the given Connection. If the given Connection is a proxy, it will be unwrapped until a non-proxy Connection is found. Otherwise, the passed-in Connection will be returned as-is.
      Parameters:
      con - the Connection proxy to unwrap
      Returns:
      the innermost target Connection, or the passed-in one if no proxy
      see Also: