Class ConnectionPool

java.lang.Object
org.apache.tomcat.jdbc.pool.ConnectionPool

public class ConnectionPool extends Object
Implementation of simple connection pool. The ConnectionPool uses a PoolProperties object for storing all the meta information about the connection pool. As the underlying implementation, the connection pool uses BlockingQueue to store active and idle connections. A custom implementation of a fair FairBlockingQueue blocking queue is provided with the connection pool itself.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected class 
    Thread safe wrapper around a future for the regular queue This one retrieves the pooled connection object and performs the initialization according to interceptors and validation rules.
    protected static class 
    A scheduled task that periodically checks for abandoned connections, validates idle connections, and resizes the idle pool according to the pool's configuration.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected ConnectionPool
    reference to the JMX mbean. close() gives it up without holding any pool lock while other threads still notify through it, so the field is volatile and every user reads it at most once, into a local.
    static final String
    Default domain for objects registering with an mbean server
    static final String
    Prefix type for JMX registration
  • Constructor Summary

    Constructors
    Constructor
    Description
    Instantiate a connection pool.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    thread safe way to abandon a connection signals a connection to be abandoned. this will disconnect the connection, and log the stack trace if logAbandoned=true
    void
    Validates a running pool's configuration and brings the pool cleaner in line with the result as one step under this pool's monitor.
    borrowConnection(PooledConnection con, String username, String password)
    Validates and configures a previously idle connection
    void
    Iterates through all the busy connections and checks for connections that have timed out
    void
    Iterates through the idle connections and resizes the idle pool based on parameters PoolProperties.maxIdle, PoolProperties.minIdle, PoolProperties.minEvictableIdleTimeMillis
    void
    checkIdle(boolean ignoreMinSize)
    Iterates through the idle connections and resizes the idle pool based on pool configuration parameters.
    void
    Validates and corrects pool configuration values.
    protected void
    close(boolean force)
    Closes the pool and all disconnects all idle connections Active connections will be closed upon the close method is called on the underlying connection instead of being returned to the pool
    create(boolean incrementCounter)
    Create a new pooled connection object.
    createConnection(PooledConnection notUsed, String username, String password)
    Creates a JDBC connection and tries to connect to the database.
    protected void
    Create MBean object that can be registered.
    protected void
    disconnectEvent(PooledConnection con, boolean finalizing)
    Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded
    protected void
    Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded
    int
    Returns the number of connections that are in use
    long
    The total number of connections borrowed from this pool.
    Borrows a connection from the pool.
    getConnection(String username, String password)
    Borrows a connection from the pool.
    Retrieves a Connection future.
    long
    The total number of connections created by this pool.
    int
    Returns the number of idle connections
    Return the object that is potentially registered in JMX for notifications
    Returns the name of this pool
    static Set<TimerTask>
    Returns an unmodifiable set of all registered pool cleaner tasks.
    Returns the pool properties associated with this connection pool
    static Timer
    Returns the shared timer used for scheduling pool cleaner tasks.
    long
    Returns the current pool version counter.
    getProxyConstructor(boolean xa)
    Creates and caches a Constructor used to instantiate the proxy object of the requested kind (with or without the XAConnection interface).
    long
    The total number of connections reconnected by this pool.
    Returns the name this pool was registered under when it started.
    long
    The total number of connections released from this pool.
    long
    The total number of connections released by eviction.
    long
    The total number of connections released by remove abandoned.
    long
    The total number of connections returned to this pool.
    int
    Returns the total size of this pool, this includes both busy and idle connections
    static String
    Convert an exception into a String
    protected static String
    Creates a stack trace representing the existing thread's current state.
    protected static StackTraceElement[]
    Captures the existing thread's current state without formatting it.
    int
    Return the number of threads waiting for a connection
    protected void
    init(PoolConfiguration properties)
    Initialize the connection pool - called from the constructor
    void
    Starts the pool cleaner (evicter) thread if pool sweeper is enabled.
    boolean
    Returns true if close has been called, and the connection pool is unusable
    void
    Purges all connections in the pool.
    void
    Purges connections when they are returned from the pool.
    void
    Brings the pool cleaner in line with a configuration: what is running now is compared against what the configuration asks for, including the interval the running task was built with, and only a disagreement is acted on.
    protected boolean
    Checks whether this connection has expired and tries to reconnect if it has.
    void
    Publishes the MBeans of all connections this pool currently holds, below the pool's own MBean.
    protected void
    thread safe way to release a connection
    void
    reset the statistics of this pool.
    protected void
    Returns a connection to the pool If the pool is closed, the connection will be released If the connection is not part of the busy queue, it will be released.
    protected Connection
    configures a pooled connection as a proxy.
    protected boolean
    Determines if a connection should be abandoned based on PoolProperties.abandonWhenPercentageFull setting.
    protected boolean
    shouldClose(PooledConnection con, int action)
    Determines if a connection should be closed upon return to the pool.
    protected boolean
    shouldReleaseIdle(long now, PooledConnection con, long time)
    Determines whether an idle connection should be released from the pool.
    protected void
    Thread safe way to suspect a connection.
    void
    Stops the pool cleaner (evicter) thread if it is currently running and forgets its reference.
    protected boolean
    Terminate the current transaction for the given connection.
    void
    Forces a validation of all idle connections if PoolProperties.testWhileIdle is set.
    void
    testAllIdle(boolean checkMaxAgeOnly)
    Forces a validation of all idle connections if PoolProperties.testWhileIdle is set.
    void
    Releases the MBeans published for the connections this pool currently holds and for the interceptors of their chains, when the pool's own MBean is released while the pool stays live.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • POOL_JMX_DOMAIN

      public static final String POOL_JMX_DOMAIN
      Default domain for objects registering with an mbean server
      See Also:
    • POOL_JMX_TYPE_PREFIX

      public static final String POOL_JMX_TYPE_PREFIX
      Prefix type for JMX registration
      See Also:
    • jmxPool

      protected volatile ConnectionPool jmxPool
      reference to the JMX mbean. close() gives it up without holding any pool lock while other threads still notify through it, so the field is volatile and every user reads it at most once, into a local.
  • Constructor Details

    • ConnectionPool

      public ConnectionPool(PoolConfiguration prop) throws SQLException
      Instantiate a connection pool. This will create connections if initialSize is larger than 0. The PoolProperties should not be reused for another connection pool.
      Parameters:
      prop - PoolProperties - all the properties for this connection pool
      Throws:
      SQLException - Pool initialization error
  • Method Details

    • getConnectionAsync

      public Future<Connection> getConnectionAsync() throws SQLException
      Retrieves a Connection future. If a connection is not available, one can block using future.get() until a connection has become available. If a connection is not retrieved, the Future must be cancelled in order for the connection to be returned to the pool. Closing the pool cancels a future that has not received a connection yet, so a thread blocked in Future.get() observes a CancellationException instead of waiting for a connection that can no longer be handed out.
      Returns:
      a Future containing a reference to the connection or the future connection
      Throws:
      SQLException - Cannot use asynchronous connect
    • getConnection

      public Connection getConnection() throws SQLException
      Borrows a connection from the pool. If a connection is available (in the idle queue) or the pool has not reached maxActive connections a connection is returned immediately. If no connection is available, the pool will attempt to fetch a connection for maxWait milliseconds.
      Returns:
      Connection - a java.sql.Connection/javax.sql.PooledConnection reflection proxy, wrapping the underlying object.
      Throws:
      SQLException - - if the wait times out or a failure occurs creating a connection
    • getConnection

      public Connection getConnection(String username, String password) throws SQLException
      Borrows a connection from the pool. If a connection is available (in the idle queue) or the pool has not reached maxActive connections a connection is returned immediately. If no connection is available, the pool will attempt to fetch a connection for maxWait milliseconds.
      Parameters:
      username - The user name to use for the connection
      password - The password for the connection
      Returns:
      Connection - a java.sql.Connection/javax.sql.PooledConnection reflection proxy, wrapping the underlying object.
      Throws:
      SQLException - - if the wait times out or a failure occurs creating a connection
    • getName

      public String getName()
      Returns the name of this pool
      Returns:
      String - the name of the pool
    • getRegistrationName

      public String getRegistrationName()
      Returns the name this pool was registered under when it started. The registrations made for a pool - its query statistics and the monitoring MBean the reporting interceptors publish under the name - are keyed by this name, not by the live name property, which getName() returns and which management can rewrite on a running pool: a rename then changes what the pool calls itself and moves nothing that was registered under the old name.
      Returns:
      the name recorded at pool start, or null while the pool has not gotten far enough into its start to record one, or if it carried no name when it started
    • getWaitCount

      public int getWaitCount()
      Return the number of threads waiting for a connection
      Returns:
      number of threads waiting for a connection
    • getPoolProperties

      public PoolConfiguration getPoolProperties()
      Returns the pool properties associated with this connection pool
      Returns:
      PoolProperties
    • getSize

      public int getSize()
      Returns the total size of this pool, this includes both busy and idle connections
      Returns:
      int - number of established connections to the database
    • getActive

      public int getActive()
      Returns the number of connections that are in use
      Returns:
      int - number of established connections that are being used by the application
    • getIdle

      public int getIdle()
      Returns the number of idle connections
      Returns:
      int - number of established connections not being used
    • isClosed

      public boolean isClosed()
      Returns true if close has been called, and the connection pool is unusable
      Returns:
      boolean
    • setupConnection

      protected Connection setupConnection(PooledConnection con) throws SQLException
      configures a pooled connection as a proxy. This Proxy implements Connection and PooledConnection interfaces. All calls on Connection methods will be propagated down to the actual JDBC connection except for the Connection.close() method.
      Parameters:
      con - a PooledConnection to wrap in a Proxy
      Returns:
      a Connection object wrapping a pooled connection.
      Throws:
      SQLException - if an interceptor can't be configured, if the proxy can't be instantiated
    • getProxyConstructor

      public Constructor<?> getProxyConstructor(boolean xa) throws NoSuchMethodException
      Creates and caches a Constructor used to instantiate the proxy object of the requested kind (with or without the XAConnection interface). We cache these, since the creation of a constructor is fairly slow.
      Parameters:
      xa - Use a XA connection
      Returns:
      constructor used to instantiate the wrapper object
      Throws:
      NoSuchMethodException - Failed to get a constructor
    • close

      protected void close(boolean force)
      Closes the pool and all disconnects all idle connections Active connections will be closed upon the close method is called on the underlying connection instead of being returned to the pool
      Parameters:
      force - - true to even close the active connections
    • init

      protected void init(PoolConfiguration properties) throws SQLException
      Initialize the connection pool - called from the constructor
      Parameters:
      properties - PoolProperties - properties used to initialize the pool with
      Throws:
      SQLException - if initialization fails
    • checkPoolConfiguration

      public void checkPoolConfiguration(PoolConfiguration properties)
      Validates and corrects pool configuration values. Ensures that maxActive, initialSize, minIdle, maxIdle, and related values are consistent.

      A running pool's writers must not call this alone: the correction can change the eviction interval the cleaner runs on, and a change made outside this object's monitor can interleave with another surface's write between the correction and the cleaner reconciliation. See applyConfigurationChange(PoolConfiguration).

      Parameters:
      properties - the pool configuration to validate and correct
    • applyConfigurationChange

      public void applyConfigurationChange(PoolConfiguration properties)
      Validates a running pool's configuration and brings the pool cleaner in line with the result as one step under this pool's monitor.

      The two halves belong together: the validation corrects values the caller did not write - the eviction interval among them - and the cleaner's period is fixed when its task is built, so validating without then looking the cleaner over leaves a task on the interval the correction replaced. Running them under one monitor is what makes the pair atomic against the other management surface: the data source holds its own monitor while it writes the shared configuration, and the pool's MBean holds none, so this object's monitor is the only thing both writers are certain to serialise on.

      Parameters:
      properties - the pool configuration to validate and reconcile the cleaner with
    • initializePoolCleaner

      public void initializePoolCleaner(PoolConfiguration properties)
      Starts the pool cleaner (evicter) thread if pool sweeper is enabled. A cleaner that is still running from a previous start is terminated first, so this call never leaves a replaced task registered on the shared timer without a reference to stop it. A pool that has been closed never gets a cleaner at all.

      close(boolean) sets the closed flag before it terminates the cleaner, and that termination and the registration below both run under this object's monitor, so whichever of the two calls happens last wins: a close that arrives first is seen here and no task is created, a close that arrives afterwards terminates the task that was created. Without the check, a thread writing a sweep attribute concurrently with the close - either of the configuration setters that re-arm the sweeper, or the JMX resource attributes - could register a task for a pool that had already released its connections. Nothing else ever unregisters such a task: it keeps the shared cleaner timer, and the thread it owns, alive for the lifetime of the class loader that loaded the pool. The task itself does no work, as its run method checks the closed flag, but it is never removed from the timer.

      Parameters:
      properties - the pool configuration containing sweeper settings
    • terminatePoolCleaner

      public void terminatePoolCleaner()
      Stops the pool cleaner (evicter) thread if it is currently running and forgets its reference.
    • reconcilePoolCleaner

      public void reconcilePoolCleaner(PoolConfiguration properties)
      Brings the pool cleaner in line with a configuration: what is running now is compared against what the configuration asks for, including the interval the running task was built with, and only a disagreement is acted on. Nothing the caller noticed before it wrote is taken into account, because a change to a pool is something an operator can make from either of two surfaces at the same moment, and what an operator saw on the way in is not what the other operator left behind.

      Both writers of the sweeper's attributes hand their change here rather than deciding it themselves - the data source and this pool's own MBean - because this object's monitor is the only thing the two are certain to serialise on: the data source holds its own monitor, the MBean holds nothing, and they write the same configuration through whichever of them is reached for. Whether the sweeper is wanted, and what interval it wants, are therefore both read here, under that monitor, together with the task that is registered; whoever decides last sees every write that any setter has made, and no arm can slip between one writer's reading and another's stopping of the task that arm registered. A writer of an attribute the configuration check covers ends here as well, even when that attribute is not one the sweeper computes from: the check can move the interval the sweeper runs on, and a task's period is fixed when it is built, so checking without then looking the cleaner over is what leaves a task running on an interval the configuration has already replaced.

      The pool's creation uses this too, after it has validated the configuration and published the pool. Its own arming happened while the pool was still under construction, so a change that landed between that arming and the publication is something the arming never saw and that no setter could report to the pool; see DataSourceProxy for that window. A change that moved the interval is expected here rather than surprising: the validation can shorten it to the max age, which leaves the task the constructor armed running on the longer one.

      Parameters:
      properties - the pool configuration containing sweeper settings
    • abandon

      protected void abandon(PooledConnection con)
      thread safe way to abandon a connection signals a connection to be abandoned. this will disconnect the connection, and log the stack trace if logAbandoned=true
      Parameters:
      con - PooledConnection
    • suspect

      protected void suspect(PooledConnection con)
      Thread safe way to suspect a connection. Similar to abandon(PooledConnection), but instead of actually abandoning the connection, this marks the suspect flag on the PooledConnection and reports it through the JMX suspect notification - in every configuration, whatever later reporting keys off that flag must see it. A warning log on top of that is only produced when logAbandoned is set.
      Parameters:
      con - PooledConnection
    • release

      protected void release(PooledConnection con)
      thread safe way to release a connection
      Parameters:
      con - PooledConnection
    • createConnection

      protected PooledConnection createConnection(PooledConnection notUsed, String username, String password) throws SQLException
      Creates a JDBC connection and tries to connect to the database.
      Parameters:
      notUsed - Not used; retained for API compatibility
      username - The user name to use for the connection
      password - The password for the connection
      Returns:
      a PooledConnection that has been connected
      Throws:
      SQLException - Failed to get a connection
    • borrowConnection

      protected PooledConnection borrowConnection(PooledConnection con, String username, String password) throws SQLException
      Validates and configures a previously idle connection
      Parameters:
      con - - the connection to validate and configure
      username - The user name to use for the connection
      password - The password for the connection
      Returns:
      a connection
      Throws:
      SQLException - if a validation error happens
    • terminateTransaction

      protected boolean terminateTransaction(PooledConnection con)
      Terminate the current transaction for the given connection.
      Parameters:
      con - The connection
      Returns:
      true if the connection TX termination succeeded otherwise false
    • shouldClose

      protected boolean shouldClose(PooledConnection con, int action)
      Determines if a connection should be closed upon return to the pool.
      Parameters:
      con - - the connection
      action - - the validation action that should be performed
      Returns:
      true if the connection should be closed
    • reconnectIfExpired

      protected boolean reconnectIfExpired(PooledConnection con)
      Checks whether this connection has expired and tries to reconnect if it has.
      Parameters:
      con - PooledConnection
      Returns:
      true if the connection was either not expired or expired but reconnecting succeeded, false if reconnecting failed (either because a new connection could not be established or validating the newly created connection failed)
      See Also:
    • returnConnection

      protected void returnConnection(PooledConnection con)
      Returns a connection to the pool If the pool is closed, the connection will be released If the connection is not part of the busy queue, it will be released. If PoolProperties.testOnReturn is set to true it will be validated
      Parameters:
      con - PooledConnection to be returned to the pool
    • shouldAbandon

      protected boolean shouldAbandon()
      Determines if a connection should be abandoned based on PoolProperties.abandonWhenPercentageFull setting.
      Returns:
      true if the connection should be abandoned
    • checkAbandoned

      public void checkAbandoned()
      Iterates through all the busy connections and checks for connections that have timed out
    • checkIdle

      public void checkIdle()
      Iterates through the idle connections and resizes the idle pool based on parameters PoolProperties.maxIdle, PoolProperties.minIdle, PoolProperties.minEvictableIdleTimeMillis
    • checkIdle

      public void checkIdle(boolean ignoreMinSize)
      Iterates through the idle connections and resizes the idle pool based on pool configuration parameters. If ignoreMinSize is true, idle connections may be removed even if the pool is below minIdle.
      Parameters:
      ignoreMinSize - if true, ignore the minIdle setting when evicting
    • shouldReleaseIdle

      protected boolean shouldReleaseIdle(long now, PooledConnection con, long time)
      Determines whether an idle connection should be released from the pool.
      Parameters:
      now - the current time in milliseconds
      con - the pooled connection to evaluate
      time - the timestamp when the connection last became idle
      Returns:
      true if the connection should be released
    • testAllIdle

      public void testAllIdle()
      Forces a validation of all idle connections if PoolProperties.testWhileIdle is set.

      Note: when this method is invoked externally while the per-connection lock is disabled (sweeper switched off and PoolProperties.getUseLock() returning false), it still serialises with borrowers through the queues: every connection is claimed out of the idle queue before it is validated or reconnected, and is put back or released afterwards, so a borrower is never handed a connection this method is working on. That claim is what makes an invocation concurrent with traffic safe, as PooledConnection.lock() alone protects nothing in this configuration. A pool closed while a connection is claimed does not get it offered back into its idle queue - the shutdown drain has passed it by, so the released connection is disconnected directly; the small window that accepts between that check and the queue hand-over is the same one the return path accepts.

    • testAllIdle

      public void testAllIdle(boolean checkMaxAgeOnly)
      Forces a validation of all idle connections if PoolProperties.testWhileIdle is set.
      Parameters:
      checkMaxAgeOnly - whether to only check PooledConnection.isMaxAgeExpired() but not invoke PooledConnection.validate(int)
    • getThreadDump

      protected static String getThreadDump()
      Creates a stack trace representing the existing thread's current state.
      Returns:
      a string object representing the current state.
    • getThreadDumpElements

      protected static StackTraceElement[] getThreadDumpElements()
      Captures the existing thread's current state without formatting it. An abandoned connection reports the borrow site only rarely, so the formatting is left to PooledConnection.getStackTrace() and a borrow that is never reported never pays for a thread dump.
      Returns:
      the stack trace elements representing the current state
    • getStackTrace

      public static String getStackTrace(Throwable x)
      Convert an exception into a String
      Parameters:
      x - - the throwable
      Returns:
      a string representing the stack trace
    • create

      protected PooledConnection create(boolean incrementCounter)
      Create a new pooled connection object. Not connected nor validated.
      Parameters:
      incrementCounter - true to increment the connection count
      Returns:
      a pooled connection object
    • purge

      public void purge()
      Purges all connections in the pool. For connections currently in use, these connections will be purged when returned on the pool. This call also purges connections that are idle and in the pool To only purge used/active connections see purgeOnReturn()
    • purgeOnReturn

      public void purgeOnReturn()
      Purges connections when they are returned from the pool. This call does not purge idle connections until they are used. To purge idle connections see purge()
    • finalize

      protected void finalize(PooledConnection con)
      Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded
      Parameters:
      con - The connection
    • disconnectEvent

      protected void disconnectEvent(PooledConnection con, boolean finalizing)
      Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded
      Parameters:
      con - The connection
      finalizing - true if finalizing the connection
    • getJmxPool

      public ConnectionPool getJmxPool()
      Return the object that is potentially registered in JMX for notifications
      Returns:
      the object implementing the ConnectionPoolMBean interface
    • createMBean

      protected void createMBean()
      Create MBean object that can be registered.
    • registerConnectionMBeans

      public void registerConnectionMBeans()
      Publishes the MBeans of all connections this pool currently holds, below the pool's own MBean. A connection registers itself when its physical connection is established, but that is only possible once the pool's own MBean has been published - and a container publishes it only after the pool, already connected to its initial size, has been created. Without this sweep the connections made before that publication, and in particular the ones the pool pre-created for its initial size, would be counted in the pool's size while staying invisible to JMX for as long as they are never borrowed.
    • unregisterConnectionMBeans

      public void unregisterConnectionMBeans()
      Releases the MBeans published for the connections this pool currently holds and for the interceptors of their chains, when the pool's own MBean is released while the pool stays live. The connections and their sessions are untouched; a later borrow or reconnect can publish them again.
    • getBorrowedCount

      public long getBorrowedCount()
      The total number of connections borrowed from this pool.
      Returns:
      the borrowed connection count
    • getReturnedCount

      public long getReturnedCount()
      The total number of connections returned to this pool.
      Returns:
      the returned connection count
    • getCreatedCount

      public long getCreatedCount()
      The total number of connections created by this pool.
      Returns:
      the created connection count
    • getReleasedCount

      public long getReleasedCount()
      The total number of connections released from this pool.
      Returns:
      the released connection count
    • getReconnectedCount

      public long getReconnectedCount()
      The total number of connections reconnected by this pool.
      Returns:
      the reconnected connection count
    • getRemoveAbandonedCount

      public long getRemoveAbandonedCount()
      The total number of connections released by remove abandoned.
      Returns:
      the PoolCleaner removed abandoned connection count
    • getReleasedIdleCount

      public long getReleasedIdleCount()
      The total number of connections released by eviction.
      Returns:
      the PoolCleaner evicted idle connection count
    • resetStats

      public void resetStats()
      reset the statistics of this pool.
    • getPoolCleaners

      public static Set<TimerTask> getPoolCleaners()
      Returns an unmodifiable set of all registered pool cleaner tasks.
      Returns:
      the set of pool cleaner timer tasks
    • getPoolVersion

      public long getPoolVersion()
      Returns the current pool version counter. This counter is incremented when connections are purged, allowing pooled connections to detect that they should be discarded.
      Returns:
      the current pool version
    • getPoolTimer

      public static Timer getPoolTimer()
      Returns the shared timer used for scheduling pool cleaner tasks.
      Returns:
      the pool cleaner timer, or null if no cleaners are registered