Class ConnectionPool

java.lang.Object
org.attribyte.sql.pool.ConnectionPool
All Implemented Interfaces:
org.attribyte.sql.ConnectionSupplier

public class ConnectionPool extends Object implements org.attribyte.sql.ConnectionSupplier
Provides logical database connections to an application from a pool of physical connections that are maintained by the pool. Connection pools are composed of segments that are used, activated and deactivated in sequence in response to connection demand. When active, a segment provides logical connections from a fixed-size pool of physical connections. Connection pools may be created using XML or property-based configuration, as well as programmatically using an Initializer.

Configuration Parameters

name
The pool name. Required.
minActiveSegments
The minimum number of active segments. Default 1.
saturatedAcquireTimeout
The maximum amount of time to block waiting for an available connection. Default 0ms.
idleCheckInterval
The time between idle segment checks. Default 60s.
segments
The number of segments. Default 1.
activeSegments
The number of segments active on start. Default 1.
connection.user
The database user.
connection.password
The database password.
connection.url
The database connection string.
connection.testSQL
SQL used for connection tests.
connection.debug
Is connection debug mode turned on?
connection.testInterval
The interval between connection tests. Default 60s.
connection.maxWait
The maximum amount of time to wait for a database connection before giving up. Default 0s.
segment.size
The number of connections in each segment. Required.
segment.closeConcurrency
The number of background threads processing connection close. If 0, close blocks in the application thread. Default 0.
segment.reconnectConcurrency
The maximum number of concurrent database reconnects. Default 1.
segment.testOnLogicalOpen
Should connections be tested when they are acquired? Default false.
segment.testOnLogicalClose
Should connections be tested when they are released? Default false.
segment.acquireTimeout
The maximum amount of time to wait for a segment connection to become available. Default 0ms.
segment.activeTimeout
The maximum amount of time a logical connection may be open before it is forcibly closed. Default 5m.
segment.connectionLifetime
The maximum amount of time a physical connection may be open. Default 1h.
segment.maxReconnectWait
The maximum amount of time to wait between physical connection attempts on failure. Default 30s.

Sample Properties Configuration File

 driver.mysql.class=com.mysql.jdbc.Driver

 logger.class=org.attribyte.api.ConsoleLogger

 property.std.useUnicode=true
 property.std.characterEncoding=utf8

 connection.local.user=apprw
 connection.local.password=secret
 connection.local.connectionString=jdbc:mysql://127.0.0.1/attribyte
 connection.local.testSQL=SELECT 1 FROM test
 connection.local.testInterval=30s
 connection.local.createTimeout=60s
 connection.local.debug=true
 connection.local.properties=std

 connection.remote.user=apprw
 connection.remote.password=secret
 connection.remote.connectionString=jdbc:mysql://127.0.0.1/attribyte
 connection.remote.testSQL=SELECT 1 FROM test
 connection.remote.testInterval=30s
 connection.remote.createTimeout=60s
 connection.remote.debug=true
 connection.remote.properties=std

 pool.localPool.minActiveSegments=1
 pool.localPool.idleCheckInterval=30s
 pool.localPool.saturatedAcquireTimeout=1s

 pool.localPool.segment0.size=5
 pool.localPool.segment0.closeConcurrency=2
 pool.localPool.segment0.testOnLogicalOpen=true
 pool.localPool.segment0.testOnLogicalClose=true
 pool.localPool.segment0.connectionName=local
 pool.localPool.segment0.acquireTimeout=10ms
 pool.localPool.segment0.activeTimeout=60s
 pool.localPool.segment0.connectionLifetime=15m
 pool.localPool.segment0.idleTimeBeforeShutdown=30s
 pool.localPool.segment0.minActiveTime=30s
 pool.localPool.segment0.reconnectConcurrency=2
 pool.localPool.segment0.reconnectMaxWaitTime=1m
 pool.localPool.segment0.activeTimeoutMonitorFrequency=30s

 pool.localPool.segment1.clone=segment0
 pool.localPool.segment1.acquireTimeout=10ms
 pool.localPool.segment1.size=10

 pool.localPool.segment2.clone=segment0
 pool.localPool.segment2.acquireTimeout=50ms

 pool.remotePool.minActiveSegments=1
 pool.remotePool.idleCheckInterval=30s
 pool.remotePool.saturatedAcquireTimeout=1s

 pool.remotePool.segment0.size=5
 pool.remotePool.segment0.closeConcurrency=2
 pool.remotePool.segment0.testOnLogicalOpen=true
 pool.remotePool.segment0.testOnLogicalClose=true
 pool.remotePool.segment0.connectionName=local
 pool.remotePool.segment0.acquireTimeout=10ms
 pool.remotePool.segment0.activeTimeout=60s
 pool.remotePool.segment0.connectionLifetime=15m
 pool.remotePool.segment0.idleTimeBeforeShutdown=30s
 pool.remotePool.segment0.minActiveTime=30s
 pool.remotePool.segment0.reconnectConcurrency=2
 pool.remotePool.segment0.reconnectMaxWaitTime=1m
 pool.remotePool.segment0.activeTimeoutMonitorFrequency=30s

 pool.remotePool.segment1.clone=segment0
 pool.remotePool.segment1.acquireTimeout=10ms
 pool.remotePool.segment1.size=10
 
Author:
Matt Hamer - Attribyte Labs, LLC
  • Method Details

    • newInitializer

      public static ConnectionPool.Initializer newInitializer()
      Creates an empty initializer.
      Returns:
      The initializer.
    • getUnmanagedConnection

      public final Connection getUnmanagedConnection() throws SQLException
      Gets a new connection that is not managed by the pool.

      The connection is created directly by the driver/datasource as configured for the first segment. Caller must call closeUnmanagedConnection for this connection when complete.

      Returns:
      The unmanaged connection.
      Throws:
      SQLException - If no connection is available.
    • closeUnmanagedConnection

      public final void closeUnmanagedConnection(Connection conn) throws SQLException
      Closes an unmanaged connection.
      Parameters:
      conn - The connection.
      Throws:
      SQLException - If close fails.
    • getConnection

      public final Connection getConnection() throws SQLException
      Gets a connection.
      Specified by:
      getConnection in interface org.attribyte.sql.ConnectionSupplier
      Returns:
      The connection
      Throws:
      SQLException - If no connection is available.
    • getFutureConnection

      public final com.google.common.util.concurrent.ListenableFuture<Connection> getFutureConnection(com.google.common.util.concurrent.ListeningExecutorService executor)
      Gets a connection future. See Listenable Future - Explained
      Parameters:
      executor - The executor service used to complete the future.
      Returns:
      The (listenable) connection future.
    • shutdown

      public final void shutdown()
      Shutdown the pool.
    • shutdownNow

      public final void shutdownNow()
      Shutdown the pool without waiting for any in-progress operations to complete.
    • getName

      public String getName()
      Gets the name of the pool.
      Returns:
      The pool name.
    • getAKA

      public com.google.common.collect.ImmutableSet<String> getAKA()
      Gets an unmodifiable set of alias names for this pool.
      Returns:
      The set of names.
    • getActiveSegments

      public int getActiveSegments()
      Gets the current number of active segments.
      Returns:
      The number of active segments.
    • getTotalSegments

      public int getTotalSegments()
      Gets the total number of segments (active + reserve).
      Returns:
      The total number of segments.
    • getMinActiveSegments

      public int getMinActiveSegments()
      Gets the minimum number of active segments.
      Returns:
      The minimum number of active segments.
    • getActiveConnections

      public int getActiveConnections()
      Gets the number of connections that are currently active.
      Returns:
      The number of active connections.
    • getAvailableConnections

      public int getAvailableConnections()
      Gets the number of connections that are currently available.
      Returns:
      The number of available.
    • isIdle

      public boolean isIdle()
      Is the pool currently idle?
      Returns:
      Is the pool idle.
    • getStats

      public ConnectionPool.Stats getStats()
      Gets a snapshot of statistics for this pool.
      Returns:
      The statistics.
    • getMetrics

      public com.codahale.metrics.MetricSet getMetrics()
      Gets the metrics for this pool.
      Returns:
      The metrics.
    • registerMetrics

      public com.codahale.metrics.MetricRegistry registerMetrics(com.codahale.metrics.MetricRegistry registry)
      Registers all metrics for this cache with the prefix 'org.attribyte.sql.pool.ConnectionPool.[pool name]'.
      Parameters:
      registry - The registry to which metrics are added.
      Returns:
      The input registry.