public class ConnectionPool extends java.lang.Object implements ConnectionSupplier
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.
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
| Modifier and Type | Class and Description |
|---|---|
static class |
ConnectionPool.Initializer
Initializes and creates connection pools.
|
static class |
ConnectionPool.Stats
Pool statistics.
|
| Modifier and Type | Method and Description |
|---|---|
void |
closeUnmanagedConnection(java.sql.Connection conn)
Closes an unumanged connection.
|
int |
getActiveConnections()
Gets the number of connections that are currently active.
|
int |
getActiveSegments()
Gets the current number of active segments.
|
<any> |
getAKA()
Gets an unmodifiable set of alias names for this pool.
|
int |
getAvailableConnections()
Gets the number of connections that are currently available.
|
java.sql.Connection |
getConnection()
Gets a connection.
|
<any> |
getFutureConnection(ListeningExecutorService executor)
Gets a connection future.
|
MetricSet |
getMetrics()
Gets the metrics for this pool.
|
int |
getMinActiveSegments()
Gets the minimum number of active segments.
|
java.lang.String |
getName()
Gets the name of the pool.
|
ConnectionPool.Stats |
getStats()
Gets a snapshot of statistics for this pool.
|
int |
getTotalSegments()
Gets the total number of segments (active + reserve).
|
java.sql.Connection |
getUnmanagedConnection()
Gets a new connection that is not managed by the pool.
|
boolean |
isIdle()
Is the pool currently idle?
|
static ConnectionPool.Initializer |
newInitializer()
Creates an empty initializer.
|
MetricRegistry |
registerMetrics(MetricRegistry registry)
Registers all metrics for this cache with the prefix 'org.attribyte.sql.pool.ConnectionPool.[pool name]'.
|
void |
shutdown()
Shutdown the pool.
|
public static ConnectionPool.Initializer newInitializer()
public final java.sql.Connection getUnmanagedConnection()
throws java.sql.SQLException
The connection is created directly by the driver/datasource as configured for the first segment. Caller must call closeUnmanagedConnection for this connection when complete.
java.sql.SQLException - If no connection is available.public final void closeUnmanagedConnection(java.sql.Connection conn)
throws java.sql.SQLException
conn - The conneciton.java.sql.SQLException - If close fails.public final java.sql.Connection getConnection()
throws java.sql.SQLException
getConnection in interface ConnectionSupplierjava.sql.SQLException - If no connection is available.public final <any> getFutureConnection(ListeningExecutorService executor)
executor - The executor service used to complete the future.public final void shutdown()
public java.lang.String getName()
public <any> getAKA()
public int getActiveSegments()
public int getTotalSegments()
public int getMinActiveSegments()
public int getActiveConnections()
public int getAvailableConnections()
public boolean isIdle()
public ConnectionPool.Stats getStats()
public MetricSet getMetrics()
public MetricRegistry registerMetrics(MetricRegistry registry)
registry - The registry to which metrics are added.