com.armatiek.infofuze.pool
Class MiniConnectionPoolManager

java.lang.Object
  extended by com.armatiek.infofuze.pool.MiniConnectionPoolManager

public class MiniConnectionPoolManager
extends java.lang.Object

A simple standalone JDBC connection pool manager.

The public methods of this class are thread-safe.

Home page: www.source-code.biz
Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland
Multi-licensed: EPL/LGPL/MPL.

2007-06-21: Constructor with a timeout parameter added.
2008-05-03: Additional licenses added (EPL/MPL).
2009-06-26: Variable recycledConnections changed from Stack to Queue, so that the unused connections are reused in a circular manner. Thanks to Daniel Jurado for the tip.
2009-08-21: ArrayDeque (which was introduced with change 2009-06-26) replaced by LinkedList, because ArrayDeque is only available since Java 1.6 and we want to keep MiniConnectionPoolManager compatible with Java 1.5.


Nested Class Summary
static class MiniConnectionPoolManager.TimeoutException
          Thrown in getConnection() when no free connection becomes available within timeout seconds.
 
Constructor Summary
MiniConnectionPoolManager(javax.sql.ConnectionPoolDataSource dataSource, int maxConnections)
          Constructs a MiniConnectionPoolManager object with a timeout of 60 seconds.
MiniConnectionPoolManager(javax.sql.ConnectionPoolDataSource dataSource, int maxConnections, int timeout)
          Constructs a MiniConnectionPoolManager object.
 
Method Summary
 void dispose()
          Closes all unused pooled connections.
 int getActiveConnections()
          Returns the number of active (open) connections of this pool.
 java.sql.Connection getConnection()
          Retrieves a connection from the connection pool.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MiniConnectionPoolManager

public MiniConnectionPoolManager(javax.sql.ConnectionPoolDataSource dataSource,
                                 int maxConnections)
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(javax.sql.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 Detail

dispose

public void dispose()
             throws java.sql.SQLException
Closes all unused pooled connections.

Throws:
java.sql.SQLException

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Retrieves a connection from the connection pool. 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.

Returns:
a new Connection object.
Throws:
MiniConnectionPoolManager.TimeoutException - when no connection becomes available within timeout seconds.
java.sql.SQLException

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 by getConnection() for which Connection.close() has not yet been called.

Returns:
the number of active connections.