Class ConnectionPoolManager

java.lang.Object
net.simplace.core.database.ConnectionPoolManager

public class ConnectionPoolManager extends Object
Simple implementation of a JDBC connection pool manager (singleton). Pools are registered by name; each pool belongs to a system id (e.g. memorytable for in-memory databases) and is released with it.
  • Constructor Details

    • ConnectionPoolManager

      public ConnectionPoolManager()
  • Method Details

    • getInstance

      public static ConnectionPoolManager getInstance()
      Returns:
      the singleton connection pool manager (created on first call)
    • freeConnection

      public void freeConnection(String aName, Connection aConnection)
      Returns a connection to its pool (a warning is logged for unknown pools).
      Parameters:
      aName - name of the pool
      aConnection - the connection
    • freeConnection

      public void freeConnection(Connection aConnection)
      Returns a connection to the pool it belongs to.
      Parameters:
      aConnection - the connection, may be null
    • getConnection

      public Connection getConnection(String aName) throws SQLException
      Takes a free connection from a pool or opens a new one if the maximum is not reached.
      Parameters:
      aName - name of the pool
      Returns:
      the connection, or null if the pool is unknown or exhausted
      Throws:
      SQLException - if a new connection cannot be opened
    • getConnection

      public Connection getConnection(String aName, long aTime) throws SQLException
      Takes a connection from a pool, waiting up to the given time if none is available.
      Parameters:
      aName - name of the pool
      aTime - maximum waiting time in milliseconds
      Returns:
      the connection, or null if the pool is unknown or no connection became available
      Throws:
      SQLException - if a new connection cannot be opened
    • releaseAll

      public void releaseAll()
      Closes all connections of all pools (the pools stay registered) and deregisters the JDBC drivers.
    • release

      public void release(String aPoolKey)
      Closes all connections of a pool and removes it.
      Parameters:
      aPoolKey - name of the pool
    • releaseAll

      public void releaseAll(String aSystemID)
      Releases and removes all pools of a system.
      Parameters:
      aSystemID - the system id
    • createPool

      public void createPool(String aPoolName, String aJDBCDriver, String aJDBCURL, String aSystemID, String aUser, String aPassword, int aMaxConnections)
      Creates a pool, unless a pool with this name exists; the JDBC driver is loaded and registered once. Errors are logged.
      Parameters:
      aPoolName - name of the pool
      aJDBCDriver - class name of the JDBC driver
      aJDBCURL - JDBC URL of the database
      aSystemID - system the pool belongs to
      aUser - user name, or null to connect without credentials
      aPassword - password
      aMaxConnections - maximum number of connections in use; 0 for unlimited
    • renewPool

      public void renewPool(String aPoolName, String aJDBCDriver, String aJDBCURL, String aSystemID, String aUser, String aPassword, int aMaxConnections)
      Replaces a pool by a new one with the given settings; the free connections of the replaced pool are closed (without shutting down the database). If no pool with this name exists, pools named aPoolName_simsession... are released and replaced. Errors are logged.
      Parameters:
      aPoolName - name of the pool
      aJDBCDriver - class name of the JDBC driver
      aJDBCURL - JDBC URL of the database
      aSystemID - system the pool belongs to
      aUser - user name, or null to connect without credentials
      aPassword - password
      aMaxConnections - maximum number of connections in use; 0 for unlimited
    • createTempPool

      public static void createTempPool(String aPoolName)
      Creates an in-memory HSQL pool with 5 connections, see createHSQLTempPool(String, int).
      Parameters:
      aPoolName - name of the pool and the database
    • createH2TempPool

      public static void createH2TempPool(String aPoolName, int aPoolSize)
      Creates a pool for an in-memory H2 database in HSQLDB mode (system memorytable).
      Parameters:
      aPoolName - name of the pool and the database
      aPoolSize - maximum number of connections
    • createHSQLTempPool

      public static void createHSQLTempPool(String aPoolName, int aPoolSize)
      Creates a pool for an in-memory HSQL database (system memorytable).
      Parameters:
      aPoolName - name of the pool and the database
      aPoolSize - maximum number of connections
    • createPermanentPool

      public static void createPermanentPool(String aPoolName)
      Creates a pool for a file based HSQL database in the cache directory (system realtable) with 5 connections.
      Parameters:
      aPoolName - name of the pool and the database file
    • hasPool

      public boolean hasPool(String aName)
      Parameters:
      aName - name of the pool
      Returns:
      true if a pool with this name or a pool aName_simsession... exists