Class MiniConnectionPoolManager

java.lang.Object
biz.source_code.miniConnectionPoolManager.MiniConnectionPoolManager

public class MiniConnectionPoolManager extends Object
A lightweight standalone JDBC connection pool manager.

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.

  • Constructor Details

    • MiniConnectionPoolManager

      public MiniConnectionPoolManager(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(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

      public void dispose() throws SQLException
      Closes all unused pooled connections.
      Throws:
      SQLException
    • getConnection

      public Connection getConnection() throws 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:
      SQLException
      MiniConnectionPoolManager.TimeoutException - when no connection becomes available within timeout seconds.
    • getValidConnection

      public Connection getValidConnection()
      Retrieves a connection from the connection pool and ensures that it is valid by calling 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.

      Returns:
      Throws:
      MiniConnectionPoolManager.TimeoutException - when no valid connection becomes available within timeout 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 by getConnection(), for which Connection.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.