- All Implemented Interfaces:
DataSourcePool,Wrapper,CommonDataSource,DataSource
- Manages the number of connections closing connections that have been idle for some time.
- Notifies when the datasource goes down and comes back up.
- Provides PreparedStatement caching
- Knows the busy connections
- Traces connections that have been leaked
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcloseBusyConnections(long leakTimeMinutes) Close any busy connections that have not been used for some time.createUnpooledConnection(String username, String password) Create an un-pooled connection with the given username and password.voiddec()Decrement the current pool size.voidDumps the busy connection information to the logs.Returns information describing connections that are currently being used.Return a pooled connection.getConnection(String username, String password) Create an un-pooled connection with the given username and password.longReturn the number of minutes after which a busy connection could be considered leaked from the connection pool.intNot implemented and shouldn't be used.Returns null.longReturn the maximum age a connection is allowed to be before it is trimmed out of the pool.intReturn the time after which inactive connections are trimmed.intReturn the max size this pool can grow to.intReturn the min size this pool should maintain.getName()Return the dataSource name.intReturn the preparedStatement cache size.getStatus(boolean reset) Return the current status of the connection pool.intReturn the time in millis that threads will wait when the pool has hit the max size.intReturn the warning size.voidinc()Increment the current pool size.booleanReturn the default autoCommit setting for the pool.booleanReturn true if the connection pool is currently capturing the StackTrace when connections are 'got' from the pool.booleanReturns false when the dataSource is down.booleanisOnline()booleanisWrapperFor(Class<?> arg0) Returns false.voidoffline()voidonline()voidreset()Close all the connections in the pool.voidsetCaptureStackTrace(boolean captureStackTrace) Set this to true means that the StackElements are captured every time a connection is retrieved from the pool.voidsetLeakTimeMinutes(long leakTimeMinutes) For detecting and closing leaked connections.voidsetLoginTimeout(int seconds) Not implemented and shouldn't be used.voidsetLogWriter(PrintWriter writer) Not implemented.voidsetMaxSize(int max) Set a new maximum size.voidsetMinSize(int min) Set the min size this pool should maintain.voidsetPstmtCacheSize(int pstmtCacheSize) Set the preparedStatement cache size.voidsetWarningSize(int warningSize) Set a new maximum size.voidshutdown()This will close all the free connections, and then go into a wait loop, waiting for the busy connections to be freed.intsize()voidSend a message to the DataSourceAlertListener to test it.<T> TNot Implemented.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilderMethods inherited from interface javax.sql.DataSource
createConnectionBuilder
-
Constructor Details
-
ConnectionPool
-
-
Method Details
-
getParentLogger
- Specified by:
getParentLoggerin interfaceCommonDataSource- Throws:
SQLFeatureNotSupportedException
-
isWrapperFor
Returns false.- Specified by:
isWrapperForin interfaceWrapper
-
unwrap
Not Implemented.- Specified by:
unwrapin interfaceWrapper- Throws:
SQLException
-
getName
Return the dataSource name.- Specified by:
getNamein interfaceDataSourcePool
-
size
public int size()- Specified by:
sizein interfaceDataSourcePool
-
inc
public void inc()Increment the current pool size. -
dec
public void dec()Decrement the current pool size. -
isDataSourceUp
public boolean isDataSourceUp()Returns false when the dataSource is down.- Specified by:
isDataSourceUpin interfaceDataSourcePool
-
getDataSourceDownReason
- Specified by:
getDataSourceDownReasonin interfaceDataSourcePool
-
createUnpooledConnection
Create an un-pooled connection with the given username and password.- Throws:
SQLException
-
createUnpooledConnection
- Throws:
SQLException
-
setMaxSize
public void setMaxSize(int max) Set a new maximum size. The pool should respect this new maximum immediately and not require a restart. You may want to increase the maxConnections if the pool gets large and hits the warning level.- Specified by:
setMaxSizein interfaceDataSourcePool
-
getMaxSize
public int getMaxSize()Return the max size this pool can grow to. -
setMinSize
public void setMinSize(int min) Set the min size this pool should maintain. -
getMinSize
public int getMinSize()Return the min size this pool should maintain. -
setWarningSize
public void setWarningSize(int warningSize) Set a new maximum size. The pool should respect this new maximum immediately and not require a restart. You may want to increase the maxConnections if the pool gets large and hits the warning and or alert levels.- Specified by:
setWarningSizein interfaceDataSourcePool
-
getWarningSize
public int getWarningSize()Return the warning size. When the pool hits this size it can send a notify message to an administrator.- Specified by:
getWarningSizein interfaceDataSourcePool
-
getWaitTimeoutMillis
public int getWaitTimeoutMillis()Return the time in millis that threads will wait when the pool has hit the max size. These threads wait for connections to be returned by the busy connections. -
getMaxInactiveMillis
public int getMaxInactiveMillis()Return the time after which inactive connections are trimmed. -
getMaxAgeMillis
public long getMaxAgeMillis()Return the maximum age a connection is allowed to be before it is trimmed out of the pool. This value can be 0 which means there is no maximum age. -
getBusyConnectionInformation
Returns information describing connections that are currently being used. -
dumpBusyConnectionInformation
public void dumpBusyConnectionInformation()Dumps the busy connection information to the logs.This includes the stackTrace elements if they are being captured. This is useful when needing to look a potential connection pool leaks.
-
closeBusyConnections
public void closeBusyConnections(long leakTimeMinutes) Close any busy connections that have not been used for some time.These connections are considered to have leaked from the connection pool.
Connection leaks occur when code doesn't ensure that connections are closed() after they have been finished with. There should be an appropriate try catch finally block to ensure connections are always closed and put back into the pool.
-
reset
public void reset()Close all the connections in the pool.- Checks that the database is up.
- Resets the Alert level.
- Closes busy connections that have not been used for some time (aka leaks).
- This closes all the currently available connections.
- Busy connections are closed when they are returned to the pool.
-
getConnection
Return a pooled connection.- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
testAlert
public void testAlert()Send a message to the DataSourceAlertListener to test it. This is so that you can make sure the alerter is configured correctly etc. -
shutdown
public void shutdown()This will close all the free connections, and then go into a wait loop, waiting for the busy connections to be freed.The DataSources's should be shutdown AFTER thread pools. Leaked Connections are not waited on, as that would hang the server.
- Specified by:
shutdownin interfaceDataSourcePool
-
offline
public void offline()- Specified by:
offlinein interfaceDataSourcePool
-
online
- Specified by:
onlinein interfaceDataSourcePool- Throws:
SQLException
-
isOnline
public boolean isOnline()- Specified by:
isOnlinein interfaceDataSourcePool
-
isAutoCommit
public boolean isAutoCommit()Return the default autoCommit setting for the pool.- Specified by:
isAutoCommitin interfaceDataSourcePool
-
isCaptureStackTrace
public boolean isCaptureStackTrace()Return true if the connection pool is currently capturing the StackTrace when connections are 'got' from the pool.This is set to true to help diagnose connection pool leaks.
-
setCaptureStackTrace
public void setCaptureStackTrace(boolean captureStackTrace) Set this to true means that the StackElements are captured every time a connection is retrieved from the pool. This can be used to identify connection pool leaks. -
getConnection
Create an un-pooled connection with the given username and password.This uses the default isolation level and autocommit mode.
- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
getLoginTimeout
Not implemented and shouldn't be used.- Specified by:
getLoginTimeoutin interfaceCommonDataSource- Specified by:
getLoginTimeoutin interfaceDataSource- Throws:
SQLException
-
setLoginTimeout
Not implemented and shouldn't be used.- Specified by:
setLoginTimeoutin interfaceCommonDataSource- Specified by:
setLoginTimeoutin interfaceDataSource- Throws:
SQLException
-
getLogWriter
Returns null.- Specified by:
getLogWriterin interfaceCommonDataSource- Specified by:
getLogWriterin interfaceDataSource
-
setLogWriter
Not implemented.- Specified by:
setLogWriterin interfaceCommonDataSource- Specified by:
setLogWriterin interfaceDataSource- Throws:
SQLException
-
setLeakTimeMinutes
public void setLeakTimeMinutes(long leakTimeMinutes) For detecting and closing leaked connections. Connections that have been busy for more than leakTimeMinutes are considered leaks and will be closed on a reset().If you want to use a connection for that longer then you should consider creating an unpooled connection or setting longRunning to true on that connection.
-
getLeakTimeMinutes
public long getLeakTimeMinutes()Return the number of minutes after which a busy connection could be considered leaked from the connection pool. -
getPstmtCacheSize
public int getPstmtCacheSize()Return the preparedStatement cache size. -
setPstmtCacheSize
public void setPstmtCacheSize(int pstmtCacheSize) Set the preparedStatement cache size. -
getStatus
Return the current status of the connection pool.If you pass reset = true then the counters such as hitCount, waitCount and highWaterMark are reset.
- Specified by:
getStatusin interfaceDataSourcePool
-