public class MiniConnectionPoolManager extends Object
The public methods of this class are thread-safe.
Home page: www.source-code.biz/miniconnectionpoolmanager
Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland
Multi-licensed: EPL / LGPL / MPL.
Modifier and Type | Class and Description |
---|---|
static class |
MiniConnectionPoolManager.TimeoutException
Thrown in
getConnection() or getValidConnection() when no free connection becomes
available within timeout seconds. |
Constructor and Description |
---|
MiniConnectionPoolManager(ConnectionPoolDataSource dataSource,
int maxConnections)
Constructs a MiniConnectionPoolManager object with a timeout of 60 seconds.
|
MiniConnectionPoolManager(ConnectionPoolDataSource dataSource,
int maxConnections,
int timeout)
Constructs a MiniConnectionPoolManager object.
|
Modifier and Type | Method and Description |
---|---|
void |
dispose()
Closes all unused pooled connections.
|
int |
getActiveConnections()
Returns the number of active (open) connections of this pool.
|
Connection |
getConnection()
Retrieves a connection from the connection pool.
|
int |
getInactiveConnections()
Returns the number of inactive (unused) connections in this pool.
|
Connection |
getValidConnection()
Retrieves a connection from the connection pool and ensures that it is valid
by calling
Connection.isValid(int) . |
public MiniConnectionPoolManager(ConnectionPoolDataSource dataSource, int maxConnections)
dataSource
- the data source for the connections.maxConnections
- the maximum number of connections.public MiniConnectionPoolManager(ConnectionPoolDataSource dataSource, int maxConnections, int timeout)
dataSource
- the data source for the connections.maxConnections
- the maximum number of connections.timeout
- the maximum time in seconds to wait for a free connection.public void dispose() throws SQLException
SQLException
public Connection getConnection() throws SQLException
If maxConnections
connections are already in use, the method
waits until a connection becomes available or timeout
seconds elapsed.
When the application is finished using the connection, it must close it
in order to return it to the pool.
Connection
object.MiniConnectionPoolManager.TimeoutException
- when no connection becomes available within timeout
seconds.SQLException
public Connection getValidConnection()
Connection.isValid(int)
.
If a connection is not valid, the method tries to get another connection until one is valid (or a timeout occurs).
Pooled connections may become invalid when e.g. the database server is restarted.
This method is slower than getConnection()
because the JDBC
driver has to send an extra command to the database server to test the connection.
This method requires Java 1.6 or newer.
MiniConnectionPoolManager.TimeoutException
- when no valid connection becomes available within timeout
seconds.public int getActiveConnections()
This is the number of Connection
objects that have been
issued by getConnection()
, for which Connection.close()
has not yet been called.
public int getInactiveConnections()
This is the number of internally kept recycled connections,
for which Connection.close()
has been called and which
have not yet been reused.