Class PoolableConnection

All Implemented Interfaces:
AutoCloseable, Connection, Wrapper, PoolableConnectionMXBean, org.apache.commons.pool2.TrackedUse
Direct Known Subclasses:
PoolableManagedConnection

public class PoolableConnection extends DelegatingConnection<Connection> implements PoolableConnectionMXBean
A delegating connection that, rather than closing the underlying connection, returns itself to an ObjectPool when closed.
Since:
2.0
  • Field Details

    • MBEAN_SERVER

      private static MBeanServer MBEAN_SERVER
    • pool

      private final org.apache.commons.pool2.ObjectPool<PoolableConnection> pool
      The pool to which I should return.
    • jmxObjectName

      private final ObjectNameWrapper jmxObjectName
    • validationPreparedStatement

      private PreparedStatement validationPreparedStatement
    • lastValidationSql

      private String lastValidationSql
    • fatalSqlExceptionThrown

      private boolean fatalSqlExceptionThrown
      Indicate that unrecoverable SQLException was thrown when using this connection. Such a connection should be considered broken and not pass validation in the future.
    • disconnectionSqlCodes

      private final Collection<String> disconnectionSqlCodes
      SQL_STATE codes considered to signal fatal conditions. Overrides the defaults in Utils.getDisconnectionSqlCodes() (plus anything starting with Utils.DISCONNECTION_SQL_CODE_PREFIX).
    • fastFailValidation

      private final boolean fastFailValidation
      Whether or not to fast fail validation after fatal connection errors
    • lock

      private final Lock lock
  • Constructor Details

    • PoolableConnection

      public PoolableConnection(Connection conn, org.apache.commons.pool2.ObjectPool<PoolableConnection> pool, ObjectName jmxName)
      Parameters:
      conn - my underlying connection
      pool - the pool to which I should return when closed
      jmxName - JMX name
    • PoolableConnection

      public PoolableConnection(Connection conn, org.apache.commons.pool2.ObjectPool<PoolableConnection> pool, ObjectName jmxObjectName, Collection<String> disconnectSqlCodes, boolean fastFailValidation)
      Parameters:
      conn - my underlying connection
      pool - the pool to which I should return when closed
      jmxObjectName - JMX name
      disconnectSqlCodes - SQL_STATE codes considered fatal disconnection errors
      fastFailValidation - true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to run query or isValid)
  • Method Details

    • abort

      public void abort(Executor executor) throws SQLException
      Abort my underlying Connection.
      Specified by:
      abort in interface Connection
      Overrides:
      abort in class DelegatingConnection<Connection>
      Throws:
      SQLException
      Since:
      2.9.0
    • close

      public void close() throws SQLException
      Returns me to my pool.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Connection
      Specified by:
      close in interface PoolableConnectionMXBean
      Overrides:
      close in class DelegatingConnection<Connection>
      Throws:
      SQLException - Ignored here, for subclasses.
    • getDisconnectionSqlCodes

      public Collection<String> getDisconnectionSqlCodes()
      Returns:
      The disconnection SQL codes.
      Since:
      2.6.0
    • getToString

      public String getToString()
      Expose the DelegatingConnection.toString() method via a bean getter, so it can be read as a property via JMX.
      Specified by:
      getToString in interface PoolableConnectionMXBean
    • handleException

      protected void handleException(SQLException e) throws SQLException
      Description copied from class: DelegatingConnection
      Handles the given exception by throwing it.
      Overrides:
      handleException in class DelegatingConnection<Connection>
      Parameters:
      e - the exception to throw.
      Throws:
      SQLException - the exception to throw.
    • isClosed

      public boolean isClosed() throws SQLException

      This method should not be used by a client to determine whether or not a connection should be return to the connection pool (by calling close()). Clients should always attempt to return a connection to the pool once it is no longer required.

      Specified by:
      isClosed in interface Connection
      Specified by:
      isClosed in interface PoolableConnectionMXBean
      Overrides:
      isClosed in class DelegatingConnection<Connection>
      Throws:
      SQLException
    • isDisconnectionSqlException

      boolean isDisconnectionSqlException(SQLException e)
      Checks the SQLState of the input exception.

      If disconnectionSqlCodes has been set, sql states are compared to those in the configured list of fatal exception codes. If this property is not set, codes are compared against the default codes in Utils.getDisconnectionSqlCodes() and in this case anything starting with #{link Utils.DISCONNECTION_SQL_CODE_PREFIX} is considered a disconnection.

      Parameters:
      e - SQLException to be examined
      Returns:
      true if the exception signals a disconnection
    • isFastFailValidation

      public boolean isFastFailValidation()
      Returns:
      Whether to fail-fast.
      Since:
      2.6.0
    • isFatalException

      boolean isFatalException(SQLException e)
      Checks the SQLState of the input exception and any nested SQLExceptions it wraps.

      If disconnectionSqlCodes has been set, sql states are compared to those in the configured list of fatal exception codes. If this property is not set, codes are compared against the default codes in Utils.getDisconnectionSqlCodes() and in this case anything starting with #{link Utils.DISCONNECTION_SQL_CODE_PREFIX} is considered a disconnection.

      Parameters:
      e - SQLException to be examined
      Returns:
      true if the exception signals a disconnection
    • passivate

      protected void passivate() throws SQLException
      Overrides:
      passivate in class DelegatingConnection<Connection>
      Throws:
      SQLException
    • reallyClose

      public void reallyClose() throws SQLException
      Actually close my underlying Connection.
      Specified by:
      reallyClose in interface PoolableConnectionMXBean
      Throws:
      SQLException
    • setLastUsed

      public void setLastUsed()
      Description copied from class: AbandonedTrace
      Sets the time this object was last used to the current time in milliseconds.
      Overrides:
      setLastUsed in class AbandonedTrace
    • validate

      public void validate(String sql, Duration timeoutDuration) throws SQLException
      Validates the connection, using the following algorithm:
      1. If fastFailValidation (constructor argument) is true and this connection has previously thrown a fatal disconnection exception, a SQLException is thrown.
      2. If sql is null, the driver's #isValid(timeout) is called. If it returns false, SQLException is thrown; otherwise, this method returns successfully.
      3. If sql is not null, it is executed as a query and if the resulting ResultSet contains at least one row, this method returns successfully. If not, SQLException is thrown.
      Parameters:
      sql - The validation SQL query.
      timeoutDuration - The validation timeout in seconds.
      Throws:
      SQLException - Thrown when validation fails or an SQLException occurs during validation
      Since:
      2.10.0
    • validate

      @Deprecated public void validate(String sql, int timeoutSeconds) throws SQLException
      Deprecated.
      Validates the connection, using the following algorithm:
      1. If fastFailValidation (constructor argument) is true and this connection has previously thrown a fatal disconnection exception, a SQLException is thrown.
      2. If sql is null, the driver's #isValid(timeout) is called. If it returns false, SQLException is thrown; otherwise, this method returns successfully.
      3. If sql is not null, it is executed as a query and if the resulting ResultSet contains at least one row, this method returns successfully. If not, SQLException is thrown.
      Parameters:
      sql - The validation SQL query.
      timeoutSeconds - The validation timeout in seconds.
      Throws:
      SQLException - Thrown when validation fails or an SQLException occurs during validation