public class DataSourcePool extends Object implements DataSource
Modifier and Type | Class and Description |
---|---|
static class |
DataSourcePool.Status |
Constructor and Description |
---|
DataSourcePool(DataSourceAlert notify,
String name,
DataSourceConfig params) |
Modifier and Type | Method and Description |
---|---|
void |
checkDataSource()
Check the dataSource is up.
|
void |
closeBusyConnections(long leakTimeMinutes)
Close any busy connections that have not been used for some time.
|
protected PooledConnection |
createConnectionForQueue(int connId)
Grow the pool by creating a new connection.
|
Connection |
createUnpooledConnection()
Create a Connection that will not be part of the connection pool.
|
void |
deregisterDriver()
Deregister the JDBC driver.
|
void |
dumpBusyConnectionInformation()
Dumps the busy connection information to the logs.
|
boolean |
getAutoCommit()
Return the default autoCommit setting Connections in this pool will use.
|
String |
getBusyConnectionInformation()
Returns information describing connections that are currently being used.
|
Connection |
getConnection()
Return a pooled connection.
|
Connection |
getConnection(String username,
String password)
Not implemented and shouldn't be used.
|
int |
getHeartbeatFreqSecs()
Return the heartbeat frequency in seconds.
|
Runnable |
getHeartbeatRunnable()
Returns the Runnable used to check the dataSource using a heartbeat query.
|
long |
getLeakTimeMinutes()
Return the number of minutes after which a busy connection could be
considered leaked from the connection pool.
|
int |
getLoginTimeout()
Not implemented and shouldn't be used.
|
PrintWriter |
getLogWriter()
Returns null.
|
long |
getMaxAgeMillis()
Return the maximum age a connection is allowed to be before it is trimmed
out of the pool.
|
int |
getMaxInactiveMillis()
Return the time after which inactive connections are trimmed.
|
int |
getMaxSize()
Return the max size this pool can grow to.
|
int |
getMaxStackTraceSize()
Return the max size of stack traces used when trying to find connection pool leaks.
|
int |
getMinSize()
Return the min size this pool should maintain.
|
String |
getName()
Return the dataSource name.
|
Logger |
getParentLogger() |
PooledConnection |
getPooledConnection()
Get a connection from the pool.
|
int |
getPstmtCacheSize()
Return the preparedStatement cache size.
|
DataSourcePoolStatistics |
getStatistics(boolean reset)
Return the aggregated load statistics collected on all the connections in the pool.
|
DataSourcePool.Status |
getStatus(boolean reset)
Return the current status of the connection pool.
|
int |
getTransactionIsolation()
Return the default transaction isolation level connections in this pool
should have.
|
int |
getWaitTimeoutMillis()
Return the time in millis that threads will wait when the pool has hit
the max size.
|
int |
getWarningSize()
Return the warning size.
|
boolean |
isCaptureStackTrace()
Return true if the connection pool is currently capturing the StackTrace
when connections are 'got' from the pool.
|
boolean |
isDataSourceUp()
Returns false when the dataSource is down.
|
boolean |
isWrapperFor(Class<?> arg0)
Returns false.
|
protected void |
notifyWarning(String msg)
Called when the pool hits the warning level.
|
protected void |
reportClosingConnection(PooledConnection pooledConnection)
Collect statistics of a connection that is fully closing
|
void |
reset()
Close all the connections in the pool.
|
protected void |
returnConnection(PooledConnection pooledConnection)
Called by the PooledConnection themselves, returning themselves to the
pool when they have been finished with.
|
protected void |
returnConnectionForceClose(PooledConnection pooledConnection)
This is a bad connection and must be removed from the pool's busy list and fully closed.
|
void |
setCaptureStackTrace(boolean captureStackTrace)
Set this to true means that the StackElements are captured every time a
connection is retrieved from the pool.
|
void |
setLeakTimeMinutes(long leakTimeMinutes)
For detecting and closing leaked connections.
|
void |
setLoginTimeout(int seconds)
Not implemented and shouldn't be used.
|
void |
setLogWriter(PrintWriter writer)
Not implemented.
|
void |
setMaxSize(int max)
Set a new maximum size.
|
void |
setMinSize(int min)
Set the min size this pool should maintain.
|
void |
setPstmtCacheSize(int pstmtCacheSize)
Set the preparedStatement cache size.
|
void |
setWarningSize(int warningSize)
Set a new maximum size.
|
void |
shutdown(boolean deregisterDriver)
This will close all the free connections, and then go into a wait loop,
waiting for the busy connections to be freed.
|
void |
testAlert()
Send a message to the DataSourceAlertListener to test it.
|
<T> T |
unwrap(Class<T> arg0)
Not Implemented.
|
protected boolean |
validateConnection(PooledConnection conn)
Make sure the connection is still ok to use.
|
public DataSourcePool(DataSourceAlert notify, String name, DataSourceConfig params)
public Logger getParentLogger() throws SQLFeatureNotSupportedException
getParentLogger
in interface CommonDataSource
SQLFeatureNotSupportedException
public boolean isWrapperFor(Class<?> arg0) throws SQLException
isWrapperFor
in interface Wrapper
SQLException
public <T> T unwrap(Class<T> arg0) throws SQLException
unwrap
in interface Wrapper
SQLException
public String getName()
public int getMaxStackTraceSize()
This is only used when isCaptureStackTrace()
is true.
public boolean isDataSourceUp()
protected void notifyWarning(String msg)
public int getHeartbeatFreqSecs()
This is the frequency that the heartbeat runnable should be run.
public Runnable getHeartbeatRunnable()
public void checkDataSource()
This is called by the HeartbeatRunnable which should be scheduled to run periodically (every heartbeatFreqSecs seconds actually).
public Connection createUnpooledConnection() throws SQLException
When this connection is closed it will not go back into the pool.
If withDefaults is true then the Connection will have the autoCommit and transaction isolation set to the defaults for the pool.
SQLException
public void setMaxSize(int max)
public int getMaxSize()
public void setMinSize(int min)
public int getMinSize()
public void setWarningSize(int warningSize)
public int getWarningSize()
public int getWaitTimeoutMillis()
public int getMaxInactiveMillis()
public long getMaxAgeMillis()
protected boolean validateConnection(PooledConnection conn)
protected void returnConnection(PooledConnection pooledConnection)
Note that connections may not be added back to the pool if returnToPool is false or if they where created before the recycleTime. In both of these cases the connection is fully closed and not pooled.
pooledConnection
- the returning connectionprotected void returnConnectionForceClose(PooledConnection pooledConnection)
protected void reportClosingConnection(PooledConnection pooledConnection)
public String getBusyConnectionInformation()
public void dumpBusyConnectionInformation()
This includes the stackTrace elements if they are being captured. This is useful when needing to look a potential connection pool leaks.
public void closeBusyConnections(long leakTimeMinutes)
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.
protected PooledConnection createConnectionForQueue(int connId) throws SQLException
This method is protected by synchronization in calling methods.
SQLException
public void reset()
public Connection getConnection() throws SQLException
getConnection
in interface DataSource
SQLException
public PooledConnection getPooledConnection() throws SQLException
This will grow the pool if all the current connections are busy. This will go into a wait if the pool has hit its maximum size.
SQLException
public void testAlert()
public void shutdown(boolean deregisterDriver)
The DataSources's should be shutdown AFTER thread pools. Leaked Connections are not waited on, as that would hang the server.
public boolean getAutoCommit()
public int getTransactionIsolation()
public boolean isCaptureStackTrace()
This is set to true to help diagnose connection pool leaks.
public void setCaptureStackTrace(boolean captureStackTrace)
public Connection getConnection(String username, String password) throws SQLException
getConnection
in interface DataSource
SQLException
public int getLoginTimeout() throws SQLException
getLoginTimeout
in interface CommonDataSource
SQLException
public void setLoginTimeout(int seconds) throws SQLException
setLoginTimeout
in interface CommonDataSource
SQLException
public PrintWriter getLogWriter()
getLogWriter
in interface CommonDataSource
public void setLogWriter(PrintWriter writer) throws SQLException
setLogWriter
in interface CommonDataSource
SQLException
public void setLeakTimeMinutes(long leakTimeMinutes)
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.
public long getLeakTimeMinutes()
public int getPstmtCacheSize()
public void setPstmtCacheSize(int pstmtCacheSize)
public DataSourcePool.Status getStatus(boolean reset)
If you pass reset = true then the counters such as hitCount, waitCount and highWaterMark are reset.
public DataSourcePoolStatistics getStatistics(boolean reset)
public void deregisterDriver()
Copyright © 2015. All Rights Reserved.