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 class
Thrown ingetConnection()
orgetValidConnection()
when no free connection becomes available withintimeout
seconds. -
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 TypeMethodDescriptionvoid
dispose()
Closes all unused pooled connections.int
Returns the number of active (open) connections of this pool.Retrieves a connection from the connection pool.int
Returns 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
maxConnections
connections are already in use, the method waits until a connection becomes available ortimeout
seconds elapsed. When the application is finished using the connection, it must close it in order to return it to the pool.- Returns:
- a new
Connection
object. - Throws:
SQLException
MiniConnectionPoolManager.TimeoutException
- when no connection becomes available withintimeout
seconds.
-
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 withintimeout
seconds.
-
getActiveConnections
public int getActiveConnections()Returns the number of active (open) connections of this pool.This is the number of
Connection
objects 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.
-