|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sadun.util.pool.connection.ConnectionPool
A pool of SQL connections. The connections must not be closed before being returned
to the pool, unless #isTestBeforeAcquiring()
is
true.
If that is the case, a test is performed on the connection before releasing it
to the client code (when it invokes getConnection()
).
Note that this can impair performance if connections are requested often.
The pool can also be associated to a watchdog thread (by using setWatchDogEnabled(()
and setWatchDogCheckPeriodTime()
,
which periodically checks the state of the pool by picking up a connection and testing it.
On termination, the pool should be destroyed by invoking destroy()
which terminates the watchdog thread and closes the connections (see also
org.sadun.util.pool.ObjectPool.destroy()
).
Constructor Summary | |
ConnectionPool(int n,
javax.sql.DataSource dataSource)
Create a pool of n connections to the given DataSource. |
|
ConnectionPool(int n,
java.lang.String jdbcUrl)
Create a pool of n connections to the given JDBC URL. |
|
ConnectionPool(int n,
java.lang.String jdbcUrl,
java.util.Properties connectionProperties)
Create a pool of n connections to the given JDBC URL using the given connection properties. |
Method Summary | |
void |
addListener(WatchDog.Listener l)
Register a listener for the watchdog thread, if enabled . |
void |
destroy(boolean waitForReleasedObject)
Destroy any resource associated to the pool, closing all the pooled connections. |
java.sql.Connection |
getConnection()
Retrieves a connection from the pool. |
int |
getFreeCount()
Return the number of available connections in the pool |
java.lang.String |
getJdbcUrl()
Return the JDBC url to which this pool connects. |
int |
getMaxGetRetry()
Return the maximum number of retry attempts before getConnection() stops and returns null (applies only if isTestBeforeAcquiring() is true). |
int |
getSize()
Return the size of the pool |
int |
getUsedCount()
Return the number of used objects in the pool |
long |
getWatchDogCheckPeriodTime()
Return the amount of time the watchdog waits between each check (if the watchdog is enabled). |
boolean |
isTestBeforeAcquiring()
Return true if getConnection() tests
the connection before returning it, else false. |
boolean |
isWatchDogEnabled()
|
void |
releaseConnection(java.sql.Connection conn)
Return the given connection to the pool. |
void |
removeListener(WatchDog.Listener l)
Deregister a listener for the watchdog thread, if enabled . |
void |
setMaxGetRetry(int maxGetRetry)
Set the maximum number of retry attempts before getConnection() stops and returns null (applies only if isTestBeforeAcquiring() is true). |
void |
setTestBeforeAcquiring(boolean testBeforeAcquiring)
Set wether or not getConnection() tests
the connection before returning it. |
void |
setWatchDogCheckPeriodTime(long watchDogCheckPeriodTime)
Set the amount of time the watchdog waits between each check (if the watchdog is enabled). |
void |
setWatchDogEnabled(boolean v)
Enable or disable the watchdog thread associated to this
connection pool, which periodically checks the state of the pool. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public ConnectionPool(int n, java.lang.String jdbcUrl, java.util.Properties connectionProperties)
n
- the size of the pooljdbcUrl
- the JDBC URL for the connectionconnectionProperties
- the connection properties to use
{@link
- ObjectPool.ObjectPoolException ObjectPoolException} if the connections cannot be createdpublic ConnectionPool(int n, java.lang.String jdbcUrl)
n
- the size of the pooljdbcUrl
- the JDBC URL for the connection
{@link
- ObjectPool.ObjectPoolException ObjectPoolException} if the connections cannot be createdpublic ConnectionPool(int n, javax.sql.DataSource dataSource)
Note that DataSource implementations often already pool connections.
n
- the size of the pooldataSource
- the data source to use
{@link
- ObjectPool.ObjectPoolException ObjectPoolException} if the connections cannot be createdMethod Detail |
public java.sql.Connection getConnection() throws ObjectPool.ObjectPoolException
If isTestBeforeAcquiring()
is true
doTest()
is invoked before actually returning the
connection object.
ObjectPool.ObjectPoolException
public void releaseConnection(java.sql.Connection conn)
conn
- the connection to return.public boolean isTestBeforeAcquiring()
getConnection()
tests
the connection before returning it, else false.
getConnection()
tests
the connection before returning it, else false.public void setTestBeforeAcquiring(boolean testBeforeAcquiring)
getConnection()
tests
the connection before returning it.
testBeforeAcquiring
- true if the connection has to be tested.public int getMaxGetRetry()
getConnection()
stops and returns null (applies only if isTestBeforeAcquiring()
is true).
getConnection()
stops and returns null (applies only if isTestBeforeAcquiring()
is true).public void setMaxGetRetry(int maxGetRetry)
getConnection()
stops and returns null (applies only if isTestBeforeAcquiring()
is true).
maxGetRetry
- the maximum number of retry attempts before getConnection()
stops and returns null (applies only if isTestBeforeAcquiring()
is true).public void addListener(WatchDog.Listener l)
enabled
.
l
- the WatchDog.Listener
to register.public void removeListener(WatchDog.Listener l)
enabled
.
l
- the WatchDog.Listener
to deregister.public void setWatchDogEnabled(boolean v)
watchdog
thread associated to this
connection pool, which periodically checks the state of the pool.
v
- if true enables and starts the watchdog
,
else, disables it.public boolean isWatchDogEnabled()
public long getWatchDogCheckPeriodTime()
public void setWatchDogCheckPeriodTime(long watchDogCheckPeriodTime)
watchDogCheckPeriodTime
- the amount of time the watchdog waits between each check (if the watchdog is enabled).public java.lang.String getJdbcUrl()
public void destroy(boolean waitForReleasedObject)
waitForReleasedObject
- if true the pool will wait until
all connections are released before closing them.public int getFreeCount()
public int getUsedCount()
public int getSize()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |