Class SQLPool

java.lang.Object
com.blackrook.sql.SQLPool
All Implemented Interfaces:
AutoCloseable

public class SQLPool extends Object implements AutoCloseable
This is a database connection pool class for a bunch of shared, managed connections. Meant to be accessed by many threads in an enterprise setting. If a connection is requested that is not available, the requesting thread will wait until a connection is found or until it times out.
Author:
Matthew Tropiano
  • Constructor Details

    • SQLPool

      public SQLPool(SQLConnector connector, int connectionCount) throws SQLException
      Creates a new connection pool from a SQLConnector.
      Parameters:
      connector - the connector to use.
      connectionCount - the number of connections to pool.
      Throws:
      SQLException - if a connection cannot be established.
  • Method Details

    • getConnectionAnd

      public void getConnectionAnd(SQLConnectionConsumer handler) throws InterruptedException, SQLException
      Retrieves a connection from this pool, passes it to the provided SQLConnectionConsumer function, then returns it to the pool.
      Parameters:
      handler - the consumer function that accepts the retrieved connection.
      Throws:
      InterruptedException - if an interrupt is thrown by the current thread waiting for an available connection.
      SQLException - if a connection cannot be re-created or re-established.
    • getConnectionAnd

      public void getConnectionAnd(long waitMillis, SQLConnectionConsumer handler) throws InterruptedException, TimeoutException, SQLException
      Retrieves a connection from this pool, passes it to the provided SQLConnectionConsumer function, calls it, then returns it to the pool.
      Parameters:
      waitMillis - the amount of time (in milliseconds) to wait for a connection.
      handler - the consumer function that accepts the retrieved connection.
      Throws:
      InterruptedException - if an interrupt is thrown by the current thread waiting for an available connection.
      TimeoutException - if the wait lapses and there are no available connections.
      SQLException - if a connection cannot be re-created or re-established.
    • getConnectionAnd

      public <R> R getConnectionAnd(SQLConnectionFunction<R> handler) throws InterruptedException, SQLException
      Retrieves a connection from this pool, passes it to the provided SQLConnectionFunction, calls it, returns it to the pool, and returns the result.
      Type Parameters:
      R - the return type.
      Parameters:
      handler - the consumer function that accepts the retrieved connection and returns a value.
      Returns:
      the return value of the handler function.
      Throws:
      InterruptedException - if an interrupt is thrown by the current thread waiting for an available connection.
      SQLException - if a connection cannot be re-created or re-established.
    • getConnectionAnd

      public <R> R getConnectionAnd(long waitMillis, SQLConnectionFunction<R> handler) throws InterruptedException, TimeoutException, SQLException
      Retrieves a connection from this pool, passes it to the provided SQLConnectionFunction, calls it, returns it to the pool, and returns the result.
      Type Parameters:
      R - the return type.
      Parameters:
      waitMillis - the amount of time (in milliseconds) to wait for a connection.
      handler - the consumer function that accepts the retrieved connection and returns a value.
      Returns:
      the return value of the handler function.
      Throws:
      InterruptedException - if an interrupt is thrown by the current thread waiting for an available connection.
      TimeoutException - if the wait lapses and there are no available connections.
      SQLException - if a connection cannot be re-created or re-established.
    • getAvailableConnection

      public SQLConnection getAvailableConnection() throws InterruptedException, SQLException
      Retrieves an available connection from the pool.
      Returns:
      a connection to use.
      Throws:
      InterruptedException - if an interrupt is thrown by the current thread waiting for an available connection.
      SQLException - if a connection cannot be re-created or re-established.
    • getAvailableConnection

      public SQLConnection getAvailableConnection(long waitMillis) throws InterruptedException, TimeoutException, SQLException
      Retrieves an available connection from the pool.
      Parameters:
      waitMillis - the amount of time (in milliseconds) to wait for a connection.
      Returns:
      a connection to use.
      Throws:
      InterruptedException - if an interrupt is thrown by the current thread waiting for an available connection.
      TimeoutException - if the wait lapses and there are no available connections.
      SQLException - if a connection cannot be re-created or re-established.
    • getAvailableConnectionCount

      public int getAvailableConnectionCount()
      Gets the number of available connections.
      Returns:
      the amount of connections currently used.
    • getUsedConnectionCount

      public int getUsedConnectionCount()
      Gets the number of connections in use.
      Returns:
      the amount of connections currently used.
    • getTotalConnectionCount

      public int getTotalConnectionCount()
      Gets the number of total connections.
      Returns:
      the total amount of managed connections.
    • releaseConnection

      public void releaseConnection(SQLConnection connection)
      Releases a connection back to the pool. Also cancels a transaction that it may still be in, if any.
      Parameters:
      connection - the connection to release.
    • close

      public void close()
      Closes all open connections in the pool.
      Specified by:
      close in interface AutoCloseable