public class ConnectionPool
extends java.lang.Object
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.Modifier and Type | Class and Description |
---|---|
protected class |
ConnectionPool.ConnectionFuture
Tread 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 |
ConnectionPool.PoolCleaner |
Modifier and Type | Field and Description |
---|---|
protected ConnectionPool |
jmxPool
reference to the JMX mbean
|
static java.lang.String |
POOL_JMX_DOMAIN
Default domain for objects registering with an mbean server
|
static java.lang.String |
POOL_JMX_TYPE_PREFIX
Prefix type for JMX registration
|
Constructor and Description |
---|
ConnectionPool(PoolConfiguration prop)
Instantiate a connection pool.
|
Modifier and Type | Method and Description |
---|---|
protected void |
abandon(PooledConnection con)
thread safe way to abandon a connection
signals a connection to be abandoned.
|
protected PooledConnection |
borrowConnection(long now,
PooledConnection con,
java.lang.String username,
java.lang.String password)
Validates and configures a previously idle connection
|
void |
checkAbandoned()
Iterates through all the busy connections and checks for connections that have timed out
|
void |
checkIdle()
Iterates through the idle connections and resizes the idle pool based on parameters
PoolProperties.maxIdle , PoolProperties.minIdle , PoolProperties.minEvictableIdleTimeMillis |
void |
checkIdle(boolean ignoreMinSize) |
void |
checkPoolConfiguration(PoolConfiguration properties) |
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 |
protected PooledConnection |
create(boolean incrementCounter)
Create a new pooled connection object.
|
protected PooledConnection |
createConnection(long now,
PooledConnection notUsed,
java.lang.String username,
java.lang.String password)
Creates a JDBC connection and tries to connect to the database.
|
protected void |
createMBean()
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 |
finalize(PooledConnection con)
Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded
|
int |
getActive()
Returns the number of connections that are in use
|
long |
getBorrowedCount()
The total number of connections borrowed from this pool.
|
java.sql.Connection |
getConnection()
Borrows a connection from the pool.
|
java.sql.Connection |
getConnection(java.lang.String username,
java.lang.String password)
Borrows a connection from the pool.
|
java.util.concurrent.Future<java.sql.Connection> |
getConnectionAsync()
Retrieves a Connection future.
|
long |
getCreatedCount()
The total number of connections created by this pool.
|
int |
getIdle()
Returns the number of idle connections
|
ConnectionPool |
getJmxPool()
Return the object that is potentially registered in JMX for notifications
|
java.lang.String |
getName()
Returns the name of this pool
|
static java.util.Set<java.util.TimerTask> |
getPoolCleaners() |
PoolConfiguration |
getPoolProperties()
Returns the pool properties associated with this connection pool
|
static java.util.Timer |
getPoolTimer() |
long |
getPoolVersion() |
java.lang.reflect.Constructor<?> |
getProxyConstructor(boolean xa)
Creates and caches a
Constructor used to instantiate the proxy object. |
long |
getReconnectedCount()
The total number of connections reconnected by this pool.
|
long |
getReleasedCount()
The total number of connections released from this pool.
|
long |
getReleasedIdleCount()
The total number of connections released by eviction.
|
long |
getRemoveAbandonedCount()
The total number of connections released by remove abandoned.
|
long |
getReturnedCount()
The total number of connections returned to this pool.
|
int |
getSize()
Returns the total size of this pool, this includes both busy and idle connections
|
static java.lang.String |
getStackTrace(java.lang.Throwable x)
Convert an exception into a String
|
protected static java.lang.String |
getThreadDump()
Creates a stack trace representing the existing thread's current state.
|
int |
getWaitCount()
Return the number of threads waiting for a connection
|
protected void |
init(PoolConfiguration properties)
Initialize the connection pool - called from the constructor
|
void |
initializePoolCleaner(PoolConfiguration properties) |
boolean |
isClosed()
Returns true if
close has been called, and the connection pool is unusable |
void |
purge()
Purges all connections in the pool.
|
void |
purgeOnReturn()
Purges connections when they are returned from the pool.
|
protected void |
release(PooledConnection con)
thread safe way to release a connection
|
void |
resetStats()
reset the statistics of this pool.
|
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.
|
protected java.sql.Connection |
setupConnection(PooledConnection con)
configures a pooled connection as a proxy.
|
protected boolean |
shouldAbandon()
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) |
protected void |
suspect(PooledConnection con)
Thread safe way to suspect a connection.
|
void |
terminatePoolCleaner() |
protected boolean |
terminateTransaction(PooledConnection con)
Terminate the current transaction for the given connection.
|
void |
testAllIdle()
Forces a validation of all idle connections if
PoolProperties.testWhileIdle is set. |
public static final java.lang.String POOL_JMX_DOMAIN
public static final java.lang.String POOL_JMX_TYPE_PREFIX
protected ConnectionPool jmxPool
public ConnectionPool(PoolConfiguration prop) throws java.sql.SQLException
PoolProperties
should not be reused for another connection pool.prop
- PoolProperties - all the properties for this connection pooljava.sql.SQLException
public java.util.concurrent.Future<java.sql.Connection> getConnectionAsync() throws java.sql.SQLException
java.sql.SQLException
public java.sql.Connection getConnection() throws java.sql.SQLException
maxActive
connections a connection is returned immediately.
If no connection is available, the pool will attempt to fetch a connection for maxWait
milliseconds.java.sql.SQLException
- - if the wait times out or a failure occurs creating a connectionpublic java.sql.Connection getConnection(java.lang.String username, java.lang.String password) throws java.sql.SQLException
maxActive
connections a connection is returned immediately. If no
connection is available, the pool will attempt to fetch a connection for
maxWait
milliseconds.java.sql.SQLException
- - if the wait times out or a failure occurs creating a
connectionpublic java.lang.String getName()
public int getWaitCount()
public PoolConfiguration getPoolProperties()
public int getSize()
public int getActive()
public int getIdle()
public boolean isClosed()
close
has been called, and the connection pool is unusableprotected java.sql.Connection setupConnection(PooledConnection con) throws java.sql.SQLException
Connection
and PooledConnection
interfaces.
All calls on Connection
methods will be propagated down to the actual JDBC connection except for the
Connection.close()
method.con
- a PooledConnection
to wrap in a ProxyConnection
object wrapping a pooled connection.java.sql.SQLException
- if an interceptor can't be configured, if the proxy can't be instantiatedpublic java.lang.reflect.Constructor<?> getProxyConstructor(boolean xa) throws java.lang.NoSuchMethodException
Constructor
used to instantiate the proxy object.
We cache this, since the creation of a constructor is fairly slow.java.lang.NoSuchMethodException
protected void close(boolean force)
close
method is called
on the underlying connection instead of being returned to the poolforce
- - true to even close the active connectionsprotected void init(PoolConfiguration properties) throws java.sql.SQLException
properties
- PoolProperties - properties used to initialize the pool withjava.sql.SQLException
- if initialization failspublic void checkPoolConfiguration(PoolConfiguration properties)
public void initializePoolCleaner(PoolConfiguration properties)
public void terminatePoolCleaner()
protected void abandon(PooledConnection con)
con
- PooledConnectionprotected void suspect(PooledConnection con)
abandon(PooledConnection)
, but instead of actually abandoning
the connection, this will log a warning and set the suspect flag on the
PooledConnection
if logAbandoned=truecon
- PooledConnectionprotected void release(PooledConnection con)
con
- PooledConnectionprotected PooledConnection createConnection(long now, PooledConnection notUsed, java.lang.String username, java.lang.String password) throws java.sql.SQLException
now
- timestamp of when this was callednotUsed
- Argument not usedjava.sql.SQLException
protected PooledConnection borrowConnection(long now, PooledConnection con, java.lang.String username, java.lang.String password) throws java.sql.SQLException
now
- - timestampcon
- - the connection to validate and configurejava.sql.SQLException
- if a validation error happensprotected boolean terminateTransaction(PooledConnection con)
con
- true
if the connection TX termination succeeded
otherwise false
protected boolean shouldClose(PooledConnection con, int action)
con
- - the connectionaction
- - the validation action that should be performedprotected void returnConnection(PooledConnection con)
PoolProperties.testOnReturn
is set to true it will be validatedcon
- PooledConnection to be returned to the poolprotected boolean shouldAbandon()
PoolProperties.abandonWhenPercentageFull
setting.public void checkAbandoned()
public void checkIdle()
PoolProperties.maxIdle
, PoolProperties.minIdle
, PoolProperties.minEvictableIdleTimeMillis
public void checkIdle(boolean ignoreMinSize)
protected boolean shouldReleaseIdle(long now, PooledConnection con, long time)
public void testAllIdle()
PoolProperties.testWhileIdle
is set.protected static java.lang.String getThreadDump()
Thread.getStackTrace()
elementspublic static java.lang.String getStackTrace(java.lang.Throwable x)
x
- - the throwableprotected PooledConnection create(boolean incrementCounter)
public void purge()
purgeOnReturn()
public void purgeOnReturn()
purge()
protected void finalize(PooledConnection con)
con
- protected void disconnectEvent(PooledConnection con, boolean finalizing)
con
- public ConnectionPool getJmxPool()
ConnectionPoolMBean
interfaceprotected void createMBean()
public long getBorrowedCount()
public long getReturnedCount()
public long getCreatedCount()
public long getReleasedCount()
public long getReconnectedCount()
public long getRemoveAbandonedCount()
public long getReleasedIdleCount()
public void resetStats()
public static java.util.Set<java.util.TimerTask> getPoolCleaners()
public long getPoolVersion()
public static java.util.Timer getPoolTimer()
Copyright © 2000-2018 Apache Software Foundation. All Rights Reserved.