Class MiniConnectionPoolManager
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThrown ingetConnection()orgetValidConnection()when no free connection becomes available withintimeoutseconds. -
Constructor Summary
ConstructorsConstructorDescriptionMiniConnectionPoolManager(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. -
Method Summary
Modifier and TypeMethodDescriptionvoiddispose()Closes all unused pooled connections.intReturns the number of active (open) connections of this pool.Retrieves a connection from the connection pool.intReturns the number of inactive (unused) connections in this pool.Retrieves a connection from the connection pool and ensures that it is valid by callingConnection.isValid(int).
-
Constructor Details
-
MiniConnectionPoolManager
Constructs a MiniConnectionPoolManager object with a timeout of 60 seconds.- Parameters:
dataSource- the data source for the connections.maxConnections- the maximum number of connections.
-
MiniConnectionPoolManager
public MiniConnectionPoolManager(ConnectionPoolDataSource dataSource, int maxConnections, int timeout) Constructs a MiniConnectionPoolManager object.- Parameters:
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.
-
-
Method Details
-
dispose
Closes all unused pooled connections.- Throws:
SQLException
-
getConnection
Retrieves a connection from the connection pool.If
maxConnectionsconnections are already in use, the method waits until a connection becomes available ortimeoutseconds elapsed. When the application is finished using the connection, it must close it in order to return it to the pool.- Returns:
- a new
Connectionobject. - Throws:
SQLExceptionMiniConnectionPoolManager.TimeoutException- when no connection becomes available withintimeoutseconds.
-
getValidConnection
Retrieves a connection from the connection pool and ensures that it is valid by callingConnection.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.
- Returns:
- Throws:
MiniConnectionPoolManager.TimeoutException- when no valid connection becomes available withintimeoutseconds.
-
getActiveConnections
public int getActiveConnections()Returns the number of active (open) connections of this pool.This is the number of
Connectionobjects that have been issued bygetConnection(), for whichConnection.close()has not yet been called.- Returns:
- the number of active connections.
-
getInactiveConnections
public int getInactiveConnections()Returns the number of inactive (unused) connections in this pool.This is the number of internally kept recycled connections, for which
Connection.close()has been called and which have not yet been reused.- Returns:
- the number of inactive connections.
-