org.apache.commons.pool2

## Interface KeyedObjectPool<K,V>

- Type Parameters:
    `K` - The type of keys maintained by this pool.
    `V` - Type of element pooled in this pool.
  All Superinterfaces:
    AutoCloseable, Closeable
  All Known Implementing Classes:
    GenericKeyedObjectPool, ProxiedKeyedObjectPool

      public interface KeyedObjectPool<K,V>
      extends Closeable

  A "keyed" pooling interface.

  A keyed pool maintains a pool of instances for each key value.

  Example of use:

       Object obj = null;
       Object key = "Key";

       try {
           obj = pool.borrowObject(key);
           //...use the object...
       } catch(Exception e) {
           // invalidate the object
           pool.invalidateObject(key, obj);
           // do not return the object to the pool twice
           obj = null;
       } finally {
           // make sure the object is returned to the pool
           if(null != obj) {
               pool.returnObject(key, obj);
           }
       }

  `KeyedObjectPool` implementations may choose to store at most one instance per key value, or may choose to maintain a pool of instances for each key (essentially creating a `Map` of `pools`).

  See `GenericKeyedObjectPool` for an implementation.

  Since:
    2.0
  See Also:
    `KeyedPooledObjectFactory`, `ObjectPool`, `GenericKeyedObjectPool`

- - ### Method Summary

    All Methods Instance Methods Abstract Methods Default Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addObject(K key)` Create an object using the `factory` or other implementation dependent mechanism, passivate it, and then place it in the idle object pool.
    `default void` | `addObjects(Collection<K> keys, int count)` Calls `addObject(Object)` with each key in `keys` for `count` number of times.
    `default void` | `addObjects(K key, int count)` Calls `addObject(Object)` `key` `count` number of times.
    `V` | `borrowObject(K key)` Obtains an instance from this pool for the specified `key`.
    `void` | `clear()` Clears the pool, removing all pooled instances (optional operation).
    `void` | `clear(K key)` Clears the specified pool, removing all pooled instances corresponding to the given `key` (optional operation).
    `void` | `close()` Close this pool, and free any resources associated with it.
    `int` | `getNumActive()` Returns the total number of instances currently borrowed from this pool but not yet returned.
    `int` | `getNumActive(K key)` Returns the number of instances currently borrowed from but not yet returned to the pool corresponding to the given `key`.
    `int` | `getNumIdle()` Returns the total number of instances currently idle in this pool.
    `int` | `getNumIdle(K key)` Returns the number of instances corresponding to the given `key` currently idle in this pool.
    `void` | `invalidateObject(K key, V obj)` Invalidates an object from the pool.
    `default void` | `invalidateObject(K key, V obj, DestroyMode mode)` Invalidates an object from the pool, using the provided `DestroyMode`.
    `void` | `returnObject(K key, V obj)` Return an instance to the pool.

- - ### Method Detail

    - #### addObject

          void addObject(K key)
                  throws Exception,
                         IllegalStateException,
                         UnsupportedOperationException

      Create an object using the `factory` or other implementation dependent mechanism, passivate it, and then place it in the idle object pool. `addObject` is useful for "pre-loading" a pool with idle objects (Optional operation).
      Parameters:
        `key` - the key a new instance should be added to
      Throws:
        `Exception` - when `KeyedPooledObjectFactory.makeObject(K)` fails.
        `IllegalStateException` - after `close()` has been called on this pool.
        `UnsupportedOperationException` - when this pool cannot add new idle objects.

    - #### addObjects

          default void addObjects(Collection<K> keys,
                                  int count)
                           throws Exception,
                                  IllegalArgumentException

      Calls `addObject(Object)` with each key in `keys` for `count` number of times. This has the same effect as calling `addObjects(Object, int)` for each key in the `keys` collection.
      Parameters:
        `keys` - `Collection` of keys to add objects for.
        `count` - the number of idle objects to add for each `key`.
      Throws:
        `Exception` - when `addObject(Object)` fails.
        `IllegalArgumentException` - when `keyedPool`, `keys`, or any value in `keys` is `null`.
      See Also:
        `addObjects(Object, int)`

    - #### addObjects

          default void addObjects(K key,
                                  int count)
                           throws Exception,
                                  IllegalArgumentException

      Calls `addObject(Object)` `key` `count` number of times.
      Parameters:
        `key` - the key to add objects for.
        `count` - the number of idle objects to add for `key`.
      Throws:
        `Exception` - when `addObject(Object)` fails.
        `IllegalArgumentException` - when `key` is `null`.
      Since:
        2.8.0

    - #### borrowObject

          V borrowObject(K key)
                  throws Exception,
                         NoSuchElementException,
                         IllegalStateException

      Obtains an instance from this pool for the specified `key`.

      Instances returned from this method will have been either newly created with `makeObject` or will be a previously idle object and have been activated with `activateObject` and then (optionally) validated with `validateObject`.

      By contract, clients must return the borrowed object using `returnObject`, `invalidateObject`, or a related method as defined in an implementation or sub-interface, using a `key` that is `equivalent` to the one used to borrow the instance in the first place.

      The behavior of this method when the pool has been exhausted is not strictly specified (although it may be specified by implementations).

      Parameters:
        `key` - the key used to obtain the object
      Returns:
        an instance from this pool.
      Throws:
        `IllegalStateException` - after `close` has been called on this pool
        `Exception` - when `makeObject` throws an exception
        `NoSuchElementException` - when the pool is exhausted and cannot or will not return another instance

    - #### clear

          void clear()
              throws Exception,
                     UnsupportedOperationException

      Clears the pool, removing all pooled instances (optional operation).
      Throws:
        `UnsupportedOperationException` - when this implementation doesn't support the operation
        `Exception` - if the pool cannot be cleared

    - #### clear

          void clear(K key)
              throws Exception,
                     UnsupportedOperationException

      Clears the specified pool, removing all pooled instances corresponding to the given `key` (optional operation).
      Parameters:
        `key` - the key to clear
      Throws:
        `UnsupportedOperationException` - when this implementation doesn't support the operation
        `Exception` - if the key cannot be cleared

    - #### close

          void close()

      Close this pool, and free any resources associated with it.

      Calling `addObject` or `borrowObject` after invoking this method on a pool will cause them to throw an `IllegalStateException`.

      Implementations should silently fail if not all resources can be freed.

      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in interface `Closeable`

    - #### getNumActive

          int getNumActive()

      Returns the total number of instances currently borrowed from this pool but not yet returned. Returns a negative value if this information is not available.
      Returns:
        the total number of instances currently borrowed from this pool but not yet returned.

    - #### getNumActive

          int getNumActive(K key)

      Returns the number of instances currently borrowed from but not yet returned to the pool corresponding to the given `key`. Returns a negative value if this information is not available.
      Parameters:
        `key` - the key to query
      Returns:
        the number of instances currently borrowed from but not yet returned to the pool corresponding to the given `key`.

    - #### getNumIdle

          int getNumIdle()

      Returns the total number of instances currently idle in this pool. Returns a negative value if this information is not available.
      Returns:
        the total number of instances currently idle in this pool.

    - #### getNumIdle

          int getNumIdle(K key)

      Returns the number of instances corresponding to the given `key` currently idle in this pool. Returns a negative value if this information is not available.
      Parameters:
        `key` - the key to query
      Returns:
        the number of instances corresponding to the given `key` currently idle in this pool.

    - #### invalidateObject

          void invalidateObject(K key,
                                V obj)
                         throws Exception

      Invalidates an object from the pool.

      By contract, `obj` must have been obtained using `borrowObject` or a related method as defined in an implementation or sub-interface using a `key` that is equivalent to the one used to borrow the `Object` in the first place.

      This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.

      Parameters:
        `key` - the key used to obtain the object
        `obj` - a `borrowed` instance to be returned.
      Throws:
        `Exception` - if the instance cannot be invalidated

    - #### invalidateObject

          default void invalidateObject(K key,
                                        V obj,
                                        DestroyMode mode)
                                 throws Exception

      Invalidates an object from the pool, using the provided `DestroyMode`.

      By contract, `obj` must have been obtained using `borrowObject` or a related method as defined in an implementation or sub-interface using a `key` that is equivalent to the one used to borrow the `Object` in the first place.

      This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.

      Parameters:
        `key` - the key used to obtain the object
        `obj` - a `borrowed` instance to be returned.
        `mode` - destroy activation context provided to the factory
      Throws:
        `Exception` - if the instance cannot be invalidated
      Since:
        2.9.0

    - #### returnObject

          void returnObject(K key,
                            V obj)
                     throws Exception

      Return an instance to the pool. By contract, `obj` must have been obtained using `borrowObject` or a related method as defined in an implementation or sub-interface using a `key` that is equivalent to the one used to borrow the instance in the first place.
      Parameters:
        `key` - the key used to obtain the object
        `obj` - a `borrowed` instance to be returned.
      Throws:
        `IllegalStateException` - if an attempt is made to return an object to the pool that is in any state other than allocated (i.e. borrowed). Attempting to return an object more than once or attempting to return an object that was never borrowed from the pool will trigger this exception.
        `Exception` - if an instance cannot be returned to the pool

Copyright © 2001–2020 The Apache Software Foundation. All rights reserved.