ConnectionPool
Date: 2016-02-29

de.zib.scalaris

## Class ConnectionPool

- java.lang.Object
- - de.zib.scalaris.ConnectionPool

-     public class ConnectionPool
      extends Object

  Implements a simple (thread-safe) connection pool for Scalaris connections.
  Since:
    3.7
  Version:
    3.7
  Author:
    Nico Kruber, kruber@zib.de

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ConnectionPool(ConnectionFactory cFactory, int maxConnections)` Creates a new connection pool.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `closeAll()` Closes all available pooled connections.
    `void` | `closeAllBut(Collection<PeerNode> remainingNodes)` Closes all available pooled connections to any node not in the given collection.
    `Connection` | `getConnection()` Gets a connection from the pool.
    `Connection` | `getConnection(long timeout)` Tries to get a valid connection from the pool waiting at most `timeout` milliseconds.
    `ConnectionFactory` | `getConnectionFactory()` Gets the connection factory used by the pool.
    `void` | `releaseConnection(Connection conn)` Puts the given connection back into the pool.

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ConnectionPool

          public ConnectionPool(ConnectionFactory cFactory,
                        int maxConnections)

      Creates a new connection pool.
      Parameters:
        `cFactory` - the connection factory to use to create new connections
        `maxConnections` - the maximum number of connections (`0` for no limit)

  - ### Method Detail

    - #### getConnection

          public Connection getConnection()
                                   throws ConnectionException

      Gets a connection from the pool. Creates a new connection if necessary. Returns `null` if the maximum number of connections has already been hit.
      Returns:
        a connection to Scalaris or `null` if the maximum number of connections has been hit
      Throws:
        `ConnectionException` - if creating the connection fails

    - #### getConnection

          public Connection getConnection(long timeout)
                                   throws ConnectionException

      Tries to get a valid connection from the pool waiting at most `timeout` milliseconds. Creates a new connection if necessary and the maximum number of connections has not been hit yet. If the timeout is hit and no connection is available, `null` is returned.
      Parameters:
        `timeout` - number of milliseconds to wait at most for a valid connection to appear (`0` to wait forever)
      Returns:
        a connection to Scalaris or `null` if the timeout has been hit
      Throws:
        `ConnectionException` - if creating the connection fails

    - #### releaseConnection

          public void releaseConnection(Connection conn)

      Puts the given connection back into the pool.
      Parameters:
        `conn` - the connection to release

    - #### closeAll

          public void closeAll()

      Closes all available pooled connections. NOTE: This does not include any checked out connections!

    - #### closeAllBut

          public void closeAllBut(Collection<PeerNode> remainingNodes)

      Closes all available pooled connections to any node not in the given collection. NOTE: This does not include any checked out connections!
      Parameters:
        `remainingNodes` - a set of nodes to which connections should remain (fast access to `Collection.contains(Object)` is preferable, e.g. use `Set`)

    - #### getConnectionFactory

          public ConnectionFactory getConnectionFactory()

      Gets the connection factory used by the pool.
      Returns:
        the connection factory