com.jolbox.bonecp
Class BoneCP

java.lang.Object
  extended by com.jolbox.bonecp.BoneCP
All Implemented Interfaces:
Serializable

public class BoneCP
extends Object
implements Serializable

Connection pool (main class).

Author:
wwadge
See Also:
Serialized Form

Field Summary
protected  boolean cachedPoolStrategy
          Config setting.
protected  Properties clientInfo
          Config setting.
protected  boolean closeConnectionWatch
          If set to true, create a new thread that monitors a connection and displays warnings if application failed to close the connection.
protected  ConnectionStrategy connectionStrategy
          Currently active get connection strategy class to use.
protected  long connectionTimeoutInMs
          Time to wait before timing out the connection.
protected  boolean externalAuth
          Config setting.
static String MBEAN_BONECP
          JMX constant.
static String MBEAN_CONFIG
          JMX constant.
protected  boolean nullOnConnectionTimeout
          Config setting.
protected  int partitionCount
          Number of partitions passed in constructor.
protected  ConnectionPartition[] partitions
          Partitions handle.
protected  int poolAvailabilityThreshold
          Create more connections when we hit x% of our possible number of connections.
protected  boolean poolShuttingDown
          set to true if the connection pool has been flagged as shutting down.
protected  boolean resetConnectionOnClose
          Config setting.
protected  String shutdownStackTrace
          Placeholder to give more useful info in case of a double shutdown.
protected  Statistics statistics
          statistics handle.
protected  boolean statisticsEnabled
          if true, we care about statistics.
 
Constructor Summary
BoneCP(BoneCPConfig config)
          Constructor.
 
Method Summary
protected  String captureStackTrace(String message)
          Throw an exception to capture it so as to be able to print it out later on
 void close()
          Just a synonym to shutdown.
protected  void destroyConnection(ConnectionHandle conn)
           
 com.google.common.util.concurrent.ListenableFuture<Connection> getAsyncConnection()
          Obtain a connection asynchronously by queueing a request to obtain a connection in a separate thread.
 BoneCPConfig getConfig()
          Gets config object.
 Connection getConnection()
          Returns a free connection.
 AtomicBoolean getDbIsDown()
          Returns the dbIsDown field.
protected  com.google.common.base.FinalizableReferenceQueue getFinalizableRefQueue()
          Watch for connections that should have been safely closed but the application forgot.
protected  Map<Connection,Reference<ConnectionHandle>> getFinalizableRefs()
          Return the finalizable refs handle.
protected  ExecutorService getReleaseHelper()
           
protected  ExecutorService getStatementCloseHelperExecutor()
          Returns the statementCloseHelper field.
protected  LinkedTransferQueue<StatementHandle> getStatementsPendingRelease()
          Returns the statementsPendingRelease field.
 Statistics getStatistics()
          Returns a reference to the statistics class.
 int getTotalCreatedConnections()
          Return total number of connections created in all partitions.
 int getTotalFree()
          Return the number of free connections available to an application right away (excluding connections that can be created dynamically)
 int getTotalLeased()
          Return total number of connections currently in use by an application
protected  void initStmtReleaseHelper(String suffix)
          Starts off threads released to statement release helpers.
protected  void internalReleaseConnection(ConnectionHandle connectionHandle)
          Release a connection by placing the connection back in the pool.
 boolean isConnectionHandleAlive(ConnectionHandle connection)
          Sends a dummy statement to the server to keep the connection alive
protected  boolean isReleaseHelperThreadsConfigured()
          Returns the releaseHelperThreadsConfigured field.
protected  boolean isStatementReleaseHelperThreadsConfigured()
          Returns the statementReleaseHelperThreadsConfigured field.
protected  void maybeSignalForMoreConnections(ConnectionPartition connectionPartition)
          Tests if this partition has hit a threshold and signal to the pool watch thread to create new connections
protected  Connection obtainRawInternalConnection()
          Returns a database connection by using Driver.getConnection() or DataSource.getConnection()
protected  void poisonAndRepopulatePartitions()
          Add a poison connection handle so that waiting threads are terminated.
protected  void postDestroyConnection(ConnectionHandle handle)
          Update counters and call hooks.
protected  void putConnectionBackInPartition(ConnectionHandle connectionHandle)
          Places a connection back in the originating partition.
protected  void registerUnregisterJMX(boolean doRegister)
          Initialises JMX stuff.
protected  void releaseConnection(Connection connection)
          Releases the given connection back to the pool.
protected  void setReleaseHelper(ExecutorService releaseHelper)
           
protected  void setStatementCloseHelperExecutor(ExecutorService statementCloseHelper)
          Sets the statementCloseHelper field.
 void shutdown()
          Closes off this connection pool.
 void unregisterDriver()
          Drops a driver from the DriverManager's list.
protected  void watchConnection(ConnectionHandle connectionHandle)
          Starts off a new thread to monitor this connection attempt.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MBEAN_CONFIG

public static final String MBEAN_CONFIG
JMX constant.

See Also:
Constant Field Values

MBEAN_BONECP

public static final String MBEAN_BONECP
JMX constant.

See Also:
Constant Field Values

poolAvailabilityThreshold

protected final int poolAvailabilityThreshold
Create more connections when we hit x% of our possible number of connections.


partitionCount

protected int partitionCount
Number of partitions passed in constructor.


partitions

protected ConnectionPartition[] partitions
Partitions handle.


closeConnectionWatch

protected boolean closeConnectionWatch
If set to true, create a new thread that monitors a connection and displays warnings if application failed to close the connection.


poolShuttingDown

protected volatile boolean poolShuttingDown
set to true if the connection pool has been flagged as shutting down.


shutdownStackTrace

protected String shutdownStackTrace
Placeholder to give more useful info in case of a double shutdown.


connectionTimeoutInMs

protected long connectionTimeoutInMs
Time to wait before timing out the connection. Default in config is Long.MAX_VALUE milliseconds.


statisticsEnabled

protected boolean statisticsEnabled
if true, we care about statistics.


statistics

protected Statistics statistics
statistics handle.


externalAuth

protected boolean externalAuth
Config setting.


nullOnConnectionTimeout

protected boolean nullOnConnectionTimeout
Config setting.


resetConnectionOnClose

protected boolean resetConnectionOnClose
Config setting.


cachedPoolStrategy

protected volatile boolean cachedPoolStrategy
Config setting.


connectionStrategy

protected volatile ConnectionStrategy connectionStrategy
Currently active get connection strategy class to use.


clientInfo

protected Properties clientInfo
Config setting.

Constructor Detail

BoneCP

public BoneCP(BoneCPConfig config)
       throws SQLException
Constructor.

Parameters:
config - Configuration for pool
Throws:
SQLException - on error
Method Detail

shutdown

public void shutdown()
Closes off this connection pool.


unregisterDriver

public void unregisterDriver()
Drops a driver from the DriverManager's list.


close

public void close()
Just a synonym to shutdown.


poisonAndRepopulatePartitions

protected void poisonAndRepopulatePartitions()
Add a poison connection handle so that waiting threads are terminated.


destroyConnection

protected void destroyConnection(ConnectionHandle conn)
Parameters:
conn -

postDestroyConnection

protected void postDestroyConnection(ConnectionHandle handle)
Update counters and call hooks.

Parameters:
handle - connection handle.

obtainRawInternalConnection

protected Connection obtainRawInternalConnection()
                                          throws SQLException
Returns a database connection by using Driver.getConnection() or DataSource.getConnection()

Returns:
Connection handle
Throws:
SQLException - on error

initStmtReleaseHelper

protected void initStmtReleaseHelper(String suffix)
Starts off threads released to statement release helpers.

Parameters:
suffix - of pool

registerUnregisterJMX

protected void registerUnregisterJMX(boolean doRegister)
Initialises JMX stuff.

Parameters:
doRegister - if true, perform registration, if false unregister

getConnection

public Connection getConnection()
                         throws SQLException
Returns a free connection.

Returns:
Connection handle.
Throws:
SQLException

watchConnection

protected void watchConnection(ConnectionHandle connectionHandle)
Starts off a new thread to monitor this connection attempt.

Parameters:
connectionHandle - to monitor

captureStackTrace

protected String captureStackTrace(String message)
Throw an exception to capture it so as to be able to print it out later on

Parameters:
message - message to display
Returns:
Stack trace message

getAsyncConnection

public com.google.common.util.concurrent.ListenableFuture<Connection> getAsyncConnection()
Obtain a connection asynchronously by queueing a request to obtain a connection in a separate thread. Use as follows:

Future<Connection> result = pool.getAsyncConnection();

... do something else in your application here ...

Connection connection = result.get(); // get the connection

Returns:
A Future task returning a connection.

maybeSignalForMoreConnections

protected void maybeSignalForMoreConnections(ConnectionPartition connectionPartition)
Tests if this partition has hit a threshold and signal to the pool watch thread to create new connections

Parameters:
connectionPartition - to test for.

releaseConnection

protected void releaseConnection(Connection connection)
                          throws SQLException
Releases the given connection back to the pool. This method is not intended to be called by applications (hence set to protected). Call connection.close() instead which will return the connection back to the pool.

Parameters:
connection - to release
Throws:
SQLException

internalReleaseConnection

protected void internalReleaseConnection(ConnectionHandle connectionHandle)
                                  throws SQLException
Release a connection by placing the connection back in the pool.

Parameters:
connectionHandle - Connection being released.
Throws:
SQLException

putConnectionBackInPartition

protected void putConnectionBackInPartition(ConnectionHandle connectionHandle)
                                     throws SQLException
Places a connection back in the originating partition.

Parameters:
connectionHandle - to place back
Throws:
SQLException - on error

isConnectionHandleAlive

public boolean isConnectionHandleAlive(ConnectionHandle connection)
Sends a dummy statement to the server to keep the connection alive

Parameters:
connection - Connection handle to perform activity on
Returns:
true if test query worked, false otherwise

getTotalLeased

public int getTotalLeased()
Return total number of connections currently in use by an application

Returns:
no of leased connections

getTotalFree

public int getTotalFree()
Return the number of free connections available to an application right away (excluding connections that can be created dynamically)

Returns:
number of free connections

getTotalCreatedConnections

public int getTotalCreatedConnections()
Return total number of connections created in all partitions.

Returns:
number of created connections

getConfig

public BoneCPConfig getConfig()
Gets config object.

Returns:
config object

getReleaseHelper

protected ExecutorService getReleaseHelper()
Returns:
the releaseHelper

setReleaseHelper

protected void setReleaseHelper(ExecutorService releaseHelper)
Parameters:
releaseHelper - the releaseHelper to set

getFinalizableRefs

protected Map<Connection,Reference<ConnectionHandle>> getFinalizableRefs()
Return the finalizable refs handle.

Returns:
the finalizableRefs value.

getFinalizableRefQueue

protected com.google.common.base.FinalizableReferenceQueue getFinalizableRefQueue()
Watch for connections that should have been safely closed but the application forgot.

Returns:
the finalizableRefQueue

getStatementCloseHelperExecutor

protected ExecutorService getStatementCloseHelperExecutor()
Returns the statementCloseHelper field.

Returns:
statementCloseHelper

setStatementCloseHelperExecutor

protected void setStatementCloseHelperExecutor(ExecutorService statementCloseHelper)
Sets the statementCloseHelper field.

Parameters:
statementCloseHelper - the statementCloseHelper to set

isReleaseHelperThreadsConfigured

protected boolean isReleaseHelperThreadsConfigured()
Returns the releaseHelperThreadsConfigured field.

Returns:
releaseHelperThreadsConfigured

isStatementReleaseHelperThreadsConfigured

protected boolean isStatementReleaseHelperThreadsConfigured()
Returns the statementReleaseHelperThreadsConfigured field.

Returns:
statementReleaseHelperThreadsConfigured

getStatementsPendingRelease

protected LinkedTransferQueue<StatementHandle> getStatementsPendingRelease()
Returns the statementsPendingRelease field.

Returns:
statementsPendingRelease

getStatistics

public Statistics getStatistics()
Returns a reference to the statistics class.

Returns:
statistics

getDbIsDown

public AtomicBoolean getDbIsDown()
Returns the dbIsDown field.

Returns:
dbIsDown


Copyright © 2009-2012 JolBox. All Rights Reserved.