Top Description Inners Fields Constructors Methods
org.apache.tomcat.jdbc.pool

public Class ConnectionPool

extends Object
Class Inheritance
Imports
java.lang.ref.WeakReference, java.lang.reflect.Constructor, .InvocationHandler, .Proxy, java.security.AccessController, .PrivilegedAction, java.sql.Connection, .SQLException, java.util.Collections, .ConcurrentModificationException, .HashSet, .Iterator, .Set, .Timer, .TimerTask, java.util.concurrent.BlockingQueue, .CountDownLatch, .ExecutionException, .Future, .LinkedBlockingQueue, .ThreadPoolExecutor, .TimeUnit, .TimeoutException, java.util.concurrent.atomic.AtomicBoolean, .AtomicInteger, .AtomicLong, javax.sql.XAConnection, org.apache.juli.logging.Log, .LogFactory

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 java.util.concurrent.BlockingQueue to store active and idle connections. A custom implementation of a fair FairBlockingQueue blocking queue is provided with the connection pool itself.

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
ConnectionPool.ConnectionFuture

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
private static class

Field Summary

Modifier and TypeField and Description
private final AtomicLong
borrowedCount

The counters for statistics of the pool.

private BlockingQueue<PooledConnection>
busy

Contains all the connections that are in use TODO - this shouldn't be a blocking queue, simply a list to hold our objects

private ThreadPoolExecutor
cancellator

Executor service used to cancel Futures

private static Set<ConnectionPool.PoolCleaner>
private volatile boolean
closed

Pool closed flag

private final AtomicLong
private BlockingQueue<PooledConnection>
idle

Contains all the idle connections

protected ConnectionPool
jmxPool

reference to the JMX mbean

private static final Log
log

Logger

public static final String
POOL_JMX_DOMAIN

Default domain for objects registering with an mbean server

public static final String
POOL_JMX_TYPE_PREFIX

Prefix type for JMX registration

private volatile ConnectionPool.PoolCleaner
poolCleaner

The thread that is responsible for checking abandoned and idle threads

private static volatile Timer
private PoolConfiguration
poolProperties

All the information about the connection pool These are the properties the pool got instantiated with

private AtomicLong
private Constructor<?>
proxyClassConstructor

Since newProxyInstance performs the same operation, over and over again, it is much more optimized if we simply store the constructor ourselves.

private final AtomicLong
private final AtomicLong
private final AtomicLong
private final AtomicLong
private final AtomicLong
private AtomicInteger
size

Carries the size of the pool, instead of relying on a queue implementation that usually iterates over to get an exact count

private AtomicInteger
waitcount

counter to track how many threads are waiting for a connection

Constructor Summary

AccessConstructor and Description
public
ConnectionPool(PoolConfiguration
PoolProperties - all the properties for this connection pool
prop
)

Instantiate a connection pool.

Method Summary

Modifier and TypeMethod and Description
protected void
abandon(PooledConnection
PooledConnection
con
)

thread safe way to abandon a connection signals a connection to be abandoned.

private PooledConnection

Returns:

a connection
borrowConnection
(int
- time to wait, overrides the maxWait from the properties, set to -1 if you wish to use maxWait, 0 if you wish no wait time.
wait
,
String
The user name to use for the connection
username
,
String
The password for the connection
password
)

Thread safe way to retrieve a connection from the pool

protected PooledConnection

Returns:

a connection
borrowConnection
(long
- timestamp
now
,
PooledConnection
- the connection to validate and configure
con
,
String
The user name to use for the connection
username
,
String
The password for the connection
password
)

Validates and configures a previously idle connection

public void
checkAbandoned()

Iterates through all the busy connections and checks for connections that have timed out

public void
checkIdle()

Iterates through the idle connections and resizes the idle pool based on parameters PoolProperties#maxIdle, PoolProperties#minIdle, PoolProperties#minEvictableIdleTimeMillis

public void
checkIdle(boolean ignoreMinSize)

public void
protected void
close(boolean
- true to even close the active connections
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

Returns:

a pooled connection object
create
(boolean
true to increment the connection count
incrementCounter
)

Create a new pooled connection object.

protected PooledConnection

Returns:

a PooledConnection that has been connected
createConnection
(long
timestamp of when this was called
now
,
PooledConnection
Argument not used
notUsed
,
String
The user name to use for the connection
username
,
String
The password for the connection
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
The connection
con
,
boolean
true if finalizing the connection
finalizing
)

Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded

protected void
finalize(PooledConnection
The connection
con
)

Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded

public int

Returns:

int - number of established connections that are being used by the application
getActive
()

Returns the number of connections that are in use

public long

Returns:

the borrowed connection count
getBorrowedCount
()

The total number of connections borrowed from this pool.

public Connection

Returns:

Connection - a java.sql.Connection/javax.sql.PooledConnection reflection proxy, wrapping the underlying object.
getConnection
()

Borrows a connection from the pool.

public Connection

Returns:

Connection - a java.sql.Connection/javax.sql.PooledConnection reflection proxy, wrapping the underlying object.
getConnection
(String
The user name to use for the connection
username
,
String
The password for the connection
password
)

Borrows a connection from the pool.

public Future<Connection>

Returns:

a Future containing a reference to the connection or the future connection
getConnectionAsync
()

Retrieves a Connection future.

public long

Returns:

the created connection count
getCreatedCount
()

The total number of connections created by this pool.

public int

Returns:

int - number of established connections not being used
getIdle
()

Returns the number of idle connections

public ConnectionPool

Returns:

the object implementing the org.apache.tomcat.jdbc.pool.jmx.ConnectionPoolMBean interface
getJmxPool
()

Return the object that is potentially registered in JMX for notifications

public String

Returns:

String - the name of the pool
getName
()

Returns the name of this pool

public static synchronized Set<TimerTask>
public PoolConfiguration

Returns:

PoolProperties
getPoolProperties
()

Returns the pool properties associated with this connection pool

public static synchronized Timer
public long
public Constructor<?>

Returns:

constructor used to instantiate the wrapper object
getProxyConstructor
(boolean
Use a XA connection
xa
)

Creates and caches a java.lang.reflect.Constructor used to instantiate the proxy object.

public long

Returns:

the reconnected connection count
getReconnectedCount
()

The total number of connections reconnected by this pool.

public long

Returns:

the released connection count
getReleasedCount
()

The total number of connections released from this pool.

public long

Returns:

the PoolCleaner evicted idle connection count
getReleasedIdleCount
()

The total number of connections released by eviction.

public long

Returns:

the PoolCleaner removed abandoned connection count
getRemoveAbandonedCount
()

The total number of connections released by remove abandoned.

public long

Returns:

the returned connection count
getReturnedCount
()

The total number of connections returned to this pool.

public int

Returns:

int - number of established connections to the database
getSize
()

Returns the total size of this pool, this includes both busy and idle connections

public static String

Returns:

a string representing the stack trace
getStackTrace
(Throwable
- the throwable
x
)

Convert an exception into a String

protected static String

Returns:

a string object representing the current state. TODO investigate if we simply should store java.lang.Thread#getStackTrace() elements
getThreadDump
()

Creates a stack trace representing the existing thread's current state.

public int

Returns:

number of threads waiting for a connection
getWaitCount
()

Return the number of threads waiting for a connection

protected void
init(PoolConfiguration
PoolProperties - properties used to initialize the pool with
properties
)

Initialize the connection pool - called from the constructor

public void
public boolean

Returns:

boolean
isClosed
()

Returns true if close has been called, and the connection pool is unusable

private boolean

Returns:

true if pool is either configured to test connections on connect or a non-NULL init SQL has been configured
isInitNewConnections
()

Returns whether new connections should be initialized by invoking PooledConnection#validate(int) with PooledConnection#VALIDATE_INIT.

public void
purge()

Purges all connections in the pool.

public void
purgeOnReturn()

Purges connections when they are returned from the pool.

protected boolean

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)
reconnectIfExpired
(PooledConnection
PooledConnection
con
)

Checks whether this connection has expired and tries to reconnect if it has.

private static synchronized void
protected void
release(PooledConnection
PooledConnection
con
)

thread safe way to release a connection

public void
resetStats()

reset the statistics of this pool.

protected void
returnConnection(PooledConnection
PooledConnection to be returned to the pool
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 Connection

Returns:

a java.sql.Connection object wrapping a pooled connection.
setupConnection
(PooledConnection
a PooledConnection to wrap in a Proxy
con
)

configures a pooled connection as a proxy.

protected boolean

Returns:

true if the connection should be abandoned
shouldAbandon
()

Determines if a connection should be abandoned based on PoolProperties#abandonWhenPercentageFull setting.

protected boolean

Returns:

true if the connection should be closed
shouldClose
(PooledConnection
- the connection
con
,
int
- the validation action that should be performed
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
PooledConnection
con
)

Thread safe way to suspect a connection.

public void
protected boolean

Returns:

true if the connection TX termination succeeded otherwise false
terminateTransaction
(PooledConnection
The connection
con
)

Terminate the current transaction for the given connection.

public void
testAllIdle()

Forces a validation of all idle connections if PoolProperties#testWhileIdle is set.

public void
testAllIdle(boolean checkMaxAgeOnly)

Forces a validation of all idle connections if PoolProperties#testWhileIdle is set.

private static synchronized void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait