public class BoneCP
extends java.lang.Object
implements java.io.Serializable
限定符和类型 | 字段和说明 |
---|---|
private java.util.concurrent.ExecutorService |
asyncExecutor
Executor service for obtaining a connection in an asynchronous fashion.
|
private java.util.concurrent.ExecutorService |
closeConnectionExecutor
Threads monitoring for bad connection requests.
|
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.
|
private long |
closeConnectionWatchTimeoutInMs
No of ms to wait for thread.join() in connection watch thread.
|
private BoneCPConfig |
config
Configuration object used in constructor.
|
private java.util.concurrent.ExecutorService |
connectionsScheduler
Executor for threads watching each partition to dynamically create new threads/kill off excess ones.
|
private long |
connectionTimeoutInMs
Time to wait before timing out the connection.
|
private java.lang.Boolean |
defaultAutoCommit
Config setting.
|
private java.lang.String |
defaultCatalog
Config setting.
|
private java.lang.Boolean |
defaultReadOnly
Config setting.
|
private int |
defaultTransactionIsolationValue
Config setting.
|
private static java.lang.String |
ERROR_TEST_CONNECTION
Exception message.
|
protected boolean |
externalAuth
Config setting.
|
private FinalizableReferenceQueue |
finalizableRefQueue
Watch for connections that should have been safely closed but the application forgot.
|
private java.util.Map<java.sql.Connection,java.lang.ref.Reference<ConnectionHandle>> |
finalizableRefs
Reference of objects that are to be watched.
|
private static java.lang.String |
KEEPALIVEMETADATA
Constant for keep-alive test
|
private java.util.concurrent.ScheduledExecutorService |
keepAliveScheduler
Handle to factory that creates 1 thread per partition that periodically wakes up and performs some
activity on the connection.
|
private static Logger |
logger
Logger class.
|
private java.util.concurrent.ScheduledExecutorService |
maxAliveScheduler
Handle to factory that creates 1 thread per partition that periodically wakes up and performs some
activity on the connection.
|
static java.lang.String |
MBEAN_BONECP
JMX constant.
|
static java.lang.String |
MBEAN_CONFIG
JMX constant.
|
private javax.management.MBeanServer |
mbs
JMX support.
|
private static java.lang.String[] |
METADATATABLE
Constant for keep-alive test
|
private int |
partitionCount
Number of partitions passed in constructor.
|
private 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.
|
private java.util.concurrent.ExecutorService |
releaseHelper
pointer to the thread containing the release helper threads.
|
private boolean |
releaseHelperThreadsConfigured
If set to true, config has specified the use of helper threads.
|
private static long |
serialVersionUID
Serialization UID
|
private static java.lang.String |
SHUTDOWN_LOCATION_TRACE
Exception message.
|
private java.lang.String |
shutdownStackTrace
Placeholder to give more useful info in case of a double shutdown.
|
private java.util.concurrent.ExecutorService |
statementCloseHelperExecutor
pointer to the service containing the statement close helper threads.
|
private boolean |
statementReleaseHelperThreadsConfigured
If set to true, config has specified the use of statement release helper threads.
|
private LinkedTransferQueue<StatementHandle> |
statementsPendingRelease
Scratch queue of statments awaiting to be closed.
|
private Statistics |
statistics
statistics handle.
|
private boolean |
statisticsEnabled
if true, we care about statistics.
|
protected java.util.concurrent.locks.Lock |
terminationLock
Prevent repeated termination of all connections when the DB goes down.
|
private static java.lang.String |
THREAD_CLOSE_CONNECTION_WARNING
Warning message.
|
private static java.lang.String |
UNCLOSED_EXCEPTION_MESSAGE
Exception message.
|
构造器和说明 |
---|
BoneCP(BoneCPConfig config)
Constructor.
|
限定符和类型 | 方法和说明 |
---|---|
protected java.lang.String |
captureStackTrace(java.lang.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.
|
private boolean |
closeStatement(java.sql.Statement stmt,
boolean result) |
java.util.concurrent.Future<java.sql.Connection> |
getAsyncConnection()
Obtain a connection asynchronously by queueing a request to obtain a connection in a separate thread.
|
BoneCPConfig |
getConfig()
Gets config object.
|
java.sql.Connection |
getConnection()
Returns a free connection.
|
protected FinalizableReferenceQueue |
getFinalizableRefQueue()
Watch for connections that should have been safely closed but the application forgot.
|
protected java.util.Map<java.sql.Connection,java.lang.ref.Reference<ConnectionHandle>> |
getFinalizableRefs()
Return the finalizable refs handle.
|
protected java.util.concurrent.ExecutorService |
getReleaseHelper() |
protected java.util.concurrent.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 |
initJMX()
Initialises JMX stuff.
|
protected void |
initStmtReleaseHelper(java.lang.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.
|
private void |
maybeSignalForMoreConnections(ConnectionPartition connectionPartition)
Tests if this partition has hit a threshold and signal to the pool watch thread to create new connections
|
protected java.sql.Connection |
obtainRawInternalConnection()
Returns a database connection by using Driver.getConnection() or DataSource.getConnection()
|
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 |
releaseConnection(java.sql.Connection connection)
Releases the given connection back to the pool.
|
protected void |
setReleaseHelper(java.util.concurrent.ExecutorService releaseHelper) |
protected void |
setStatementCloseHelperExecutor(java.util.concurrent.ExecutorService statementCloseHelper)
Sets the statementCloseHelper field.
|
void |
shutdown()
Closes off this connection pool.
|
protected void |
terminateAllConnections()
Closes off all connections in all partitions.
|
private void |
watchConnection(ConnectionHandle connectionHandle)
Starts off a new thread to monitor this connection attempt.
|
private static final java.lang.String THREAD_CLOSE_CONNECTION_WARNING
private static final long serialVersionUID
private static final java.lang.String ERROR_TEST_CONNECTION
private static final java.lang.String SHUTDOWN_LOCATION_TRACE
private static final java.lang.String UNCLOSED_EXCEPTION_MESSAGE
public static final java.lang.String MBEAN_CONFIG
public static final java.lang.String MBEAN_BONECP
private static final java.lang.String[] METADATATABLE
private static final java.lang.String KEEPALIVEMETADATA
protected final int poolAvailabilityThreshold
private int partitionCount
private ConnectionPartition[] partitions
private java.util.concurrent.ScheduledExecutorService keepAliveScheduler
private java.util.concurrent.ScheduledExecutorService maxAliveScheduler
private java.util.concurrent.ExecutorService connectionsScheduler
private BoneCPConfig config
private boolean releaseHelperThreadsConfigured
private java.util.concurrent.ExecutorService releaseHelper
private java.util.concurrent.ExecutorService statementCloseHelperExecutor
private java.util.concurrent.ExecutorService asyncExecutor
private static Logger logger
private javax.management.MBeanServer mbs
protected java.util.concurrent.locks.Lock terminationLock
protected boolean closeConnectionWatch
private java.util.concurrent.ExecutorService closeConnectionExecutor
protected volatile boolean poolShuttingDown
private java.lang.String shutdownStackTrace
private final java.util.Map<java.sql.Connection,java.lang.ref.Reference<ConnectionHandle>> finalizableRefs
private FinalizableReferenceQueue finalizableRefQueue
private long connectionTimeoutInMs
private long closeConnectionWatchTimeoutInMs
private boolean statementReleaseHelperThreadsConfigured
private LinkedTransferQueue<StatementHandle> statementsPendingRelease
private boolean statisticsEnabled
private Statistics statistics
private java.lang.Boolean defaultReadOnly
private java.lang.String defaultCatalog
private int defaultTransactionIsolationValue
private java.lang.Boolean defaultAutoCommit
protected boolean externalAuth
public BoneCP(BoneCPConfig config) throws java.sql.SQLException
config
- Configuration for pooljava.sql.SQLException
- on errorpublic void shutdown()
public void close()
protected void terminateAllConnections()
protected void postDestroyConnection(ConnectionHandle handle)
handle
- connection handle.protected java.sql.Connection obtainRawInternalConnection() throws java.sql.SQLException
java.sql.SQLException
- on errorprotected void initStmtReleaseHelper(java.lang.String suffix)
suffix
- of poolprotected void initJMX()
public java.sql.Connection getConnection() throws java.sql.SQLException
java.sql.SQLException
private void watchConnection(ConnectionHandle connectionHandle)
connectionHandle
- to monitorprotected java.lang.String captureStackTrace(java.lang.String message)
message
- message to displaypublic java.util.concurrent.Future<java.sql.Connection> getAsyncConnection()
Future<Connection> result = pool.getAsyncConnection();
... do something else in your application here ...
Connection connection = result.get(); // get the connection
private void maybeSignalForMoreConnections(ConnectionPartition connectionPartition)
connectionPartition
- to test for.protected void releaseConnection(java.sql.Connection connection) throws java.sql.SQLException
connection
- to releasejava.sql.SQLException
protected void internalReleaseConnection(ConnectionHandle connectionHandle) throws java.sql.SQLException
connectionHandle
- Connection being released.java.sql.SQLException
protected void putConnectionBackInPartition(ConnectionHandle connectionHandle) throws java.sql.SQLException
connectionHandle
- to place backjava.sql.SQLException
- on errorpublic boolean isConnectionHandleAlive(ConnectionHandle connection)
connection
- Connection handle to perform activity onprivate boolean closeStatement(java.sql.Statement stmt, boolean result)
stmt
- result
- public int getTotalLeased()
public int getTotalFree()
public int getTotalCreatedConnections()
public BoneCPConfig getConfig()
protected java.util.concurrent.ExecutorService getReleaseHelper()
protected void setReleaseHelper(java.util.concurrent.ExecutorService releaseHelper)
releaseHelper
- the releaseHelper to setprotected java.util.Map<java.sql.Connection,java.lang.ref.Reference<ConnectionHandle>> getFinalizableRefs()
protected FinalizableReferenceQueue getFinalizableRefQueue()
protected java.util.concurrent.ExecutorService getStatementCloseHelperExecutor()
protected void setStatementCloseHelperExecutor(java.util.concurrent.ExecutorService statementCloseHelper)
statementCloseHelper
- the statementCloseHelper to setprotected boolean isReleaseHelperThreadsConfigured()
protected boolean isStatementReleaseHelperThreadsConfigured()
protected LinkedTransferQueue<StatementHandle> getStatementsPendingRelease()
public Statistics getStatistics()