StackObjectPool (Apache Tomcat 7.0.108 API Documentation)

org.apache.tomcat.dbcp.pool.impl

## Class StackObjectPool<T>

- java.lang.Object
- - org.apache.tomcat.dbcp.pool.BaseObjectPool<T>
  - - org.apache.tomcat.dbcp.pool.impl.StackObjectPool<T>

- Type Parameters:
    `T` - the type of objects held in this pool
  All Implemented Interfaces:
    ObjectPool<T>

      public class StackObjectPool<T>
      extends BaseObjectPool<T>

  A simple, `Stack`-based `ObjectPool` implementation.

  Given a `PoolableObjectFactory`, this class will maintain a simple pool of instances. A finite number of "sleeping" or idle instances is enforced, but when the pool is empty, new instances are created to support the new load. Hence this class places no limit on the number of "active" instances created by the pool, but is quite useful for re-using `Object`s without introducing artificial limits.

  Since:
    Pool 1.0
  Author:
    Rodney Waldhoff, Dirk Verbeeck, Sandy McArthur

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected PoolableObjectFactory<T>` | `_factory` Deprecated. to be made private in pool 2.0 - use `getFactory()`
    `protected int` | `_maxSleeping` Deprecated. to be made private in pool 2.0 - use `getMaxSleeping()`
    `protected int` | `_numActive` Deprecated. to be made private in pool 2.0 - use `getNumActive()`
    `protected Stack<T>` | `_pool` Deprecated. to be made private in pool 2.0
    `protected static int` | `DEFAULT_INIT_SLEEPING_CAPACITY` The default initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)
    `protected static int` | `DEFAULT_MAX_SLEEPING` The cap on the number of "sleeping" instances in the pool.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `StackObjectPool()` Deprecated. to be removed in pool 2.0 - use `StackObjectPool(PoolableObjectFactory)`
    `StackObjectPool(int maxIdle)` Deprecated. to be removed in pool 2.0 - use `StackObjectPool(PoolableObjectFactory, int)`
    `StackObjectPool(int maxIdle, int initIdleCapacity)` Deprecated. to be removed in pool 2.0 - use `StackObjectPool(PoolableObjectFactory, int, int)`
    `StackObjectPool(PoolableObjectFactory<T> factory)` Create a new `StackObjectPool` using the specified factory to create new instances.
    `StackObjectPool(PoolableObjectFactory<T> factory, int maxIdle)` Create a new `SimpleObjectPool` using the specified factory to create new instances, capping the number of "sleeping" instances to maxIdle.
    `StackObjectPool(PoolableObjectFactory<T> factory, int maxIdle, int initIdleCapacity)` Create a new `StackObjectPool` using the specified `factory` to create new instances, capping the number of "sleeping" instances to `maxIdle`, and initially allocating a container capable of containing at least `initIdleCapacity` instances.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addObject()` Create an object, and place it on top of the stack.
    `T` | `borrowObject()` Borrows an object from the pool.
    `void` | `clear()` Clears any objects sitting idle in the pool.
    `void` | `close()` Close this pool, and free any resources associated with it.
    `PoolableObjectFactory<T>` | `getFactory()` Returns the `PoolableObjectFactory` used by this pool to create and manage object instances.
    `int` | `getMaxSleeping()` Returns the maximum number of idle instances in the pool.
    `int` | `getNumActive()` Return the number of instances currently borrowed from this pool.
    `int` | `getNumIdle()` Return the number of instances currently idle in this pool.
    `void` | `invalidateObject(T obj)` Invalidates an object from the pool.
    `void` | `returnObject(T obj)` Returns an instance to the pool, pushing it on top of the idle instance stack after successful validation and passivation.
    `void` | `setFactory(PoolableObjectFactory<T> factory)` Deprecated. to be removed in pool 2.0

    - ### Methods inherited from class org.apache.tomcat.dbcp.pool.BaseObjectPool

      `assertOpen, isClosed`

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

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

- - ### Field Detail

    - #### DEFAULT_MAX_SLEEPING

          protected static final int DEFAULT_MAX_SLEEPING

      The cap on the number of "sleeping" instances in the pool.
      See Also:
        Constant Field Values

    - #### DEFAULT_INIT_SLEEPING_CAPACITY

          protected static final int DEFAULT_INIT_SLEEPING_CAPACITY

      The default initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)
      See Also:
        Constant Field Values

    - #### _pool

          @Deprecated
          protected Stack<T> _pool

      Deprecated. to be made private in pool 2.0
      My pool.

    - #### _factory

          @Deprecated
          protected PoolableObjectFactory<T> _factory

      Deprecated. to be made private in pool 2.0 - use `getFactory()`
      My `PoolableObjectFactory`.

    - #### _maxSleeping

          @Deprecated
          protected int _maxSleeping

      Deprecated. to be made private in pool 2.0 - use `getMaxSleeping()`
      The cap on the number of "sleeping" instances in the pool.

    - #### _numActive

          @Deprecated
          protected int _numActive

      Deprecated. to be made private in pool 2.0 - use `getNumActive()`
      Number of objects borrowed but not yet returned to the pool.

  - ### Constructor Detail

    - #### StackObjectPool

          @Deprecated
          public StackObjectPool()

      Deprecated. to be removed in pool 2.0 - use `StackObjectPool(PoolableObjectFactory)`
      Create a new pool using no factory. Clients must first `set the factory` or else this pool will not behave correctly. Clients may first populate the pool using `returnObject(java.lang.Object)` before they can be `borrowed` but this usage is discouraged.
      See Also:
        `StackObjectPool(PoolableObjectFactory)`

    - #### StackObjectPool

          @Deprecated
          public StackObjectPool(int maxIdle)

      Deprecated. to be removed in pool 2.0 - use `StackObjectPool(PoolableObjectFactory, int)`
      Create a new pool using no factory. Clients must first `set the factory` or else this pool will not behave correctly. Clients may first populate the pool using `returnObject(java.lang.Object)` before they can be `borrowed` but this usage is discouraged.
      Parameters:
        `maxIdle` - cap on the number of "sleeping" instances in the pool
      See Also:
        `StackObjectPool(PoolableObjectFactory, int)`

    - #### StackObjectPool

          @Deprecated
          public StackObjectPool(int maxIdle,
                                    int initIdleCapacity)

      Deprecated. to be removed in pool 2.0 - use `StackObjectPool(PoolableObjectFactory, int, int)`
      Create a new pool using no factory. Clients must first `set the factory` or else this pool will not behave correctly. Clients may first populate the pool using `returnObject(java.lang.Object)` before they can be `borrowed` but this usage is discouraged.
      Parameters:
        `maxIdle` - cap on the number of "sleeping" instances in the pool
        `initIdleCapacity` - initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)
      See Also:
        `StackObjectPool(PoolableObjectFactory, int, int)`

    - #### StackObjectPool

          public StackObjectPool(PoolableObjectFactory<T> factory)

      Create a new `StackObjectPool` using the specified factory to create new instances.
      Parameters:
        `factory` - the `PoolableObjectFactory` used to populate the pool

    - #### StackObjectPool

          public StackObjectPool(PoolableObjectFactory<T> factory,
                         int maxIdle)

      Create a new `SimpleObjectPool` using the specified factory to create new instances, capping the number of "sleeping" instances to maxIdle.
      Parameters:
        `factory` - the `PoolableObjectFactory` used to populate the pool
        `maxIdle` - cap on the number of "sleeping" instances in the pool

    - #### StackObjectPool

          public StackObjectPool(PoolableObjectFactory<T> factory,
                         int maxIdle,
                         int initIdleCapacity)

      Create a new `StackObjectPool` using the specified `factory` to create new instances, capping the number of "sleeping" instances to `maxIdle`, and initially allocating a container capable of containing at least `initIdleCapacity` instances. The pool is not pre-populated. The `initIdleCapacity` parameter just determines the initial size of the underlying container, which can increase beyond this value if `maxIdle > initIdleCapacity.`

      Negative values of `maxIdle` are ignored (i.e., the pool is created using `DEFAULT_MAX_SLEEPING`) as are non-positive values for `initIdleCapacity.`

      Parameters:
        `factory` - the `PoolableObjectFactory` used to populate the pool
        `maxIdle` - cap on the number of "sleeping" instances in the pool
        `initIdleCapacity` - initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)

  - ### Method Detail

    - #### borrowObject

          public T borrowObject()
                         throws Exception

      Borrows an object from the pool. If there are idle instances available on the stack, the top element of the stack is popped to activate, validate and return to the client. If there are no idle instances available, the `makeObject` method of the pool's `PoolableObjectFactory` is invoked to create a new instance.

      All instances are `activated` and `validated` before being returned to the client. If validation fails or an exception occurs activating or validating an instance popped from the idle instance stack, the failing instance is `destroyed` and the next instance on the stack is popped, validated and activated. This process continues until either the stack is empty or an instance passes validation. If the stack is empty on activation or it does not contain any valid instances, the factory's `makeObject` method is used to create a new instance. If a null instance is returned by the factory or the created instance either raises an exception on activation or fails validation, `NoSuchElementException` is thrown. Exceptions thrown by `MakeObject` are propagated to the caller; but other than `ThreadDeath` or `VirtualMachineError`, exceptions generated by activation, validation or destroy methods are swallowed silently.

      Specified by:
        `borrowObject` in interface `ObjectPool<T>`
      Specified by:
        `borrowObject` in class `BaseObjectPool<T>`
      Returns:
        an instance from the pool
      Throws:
        `Exception` - if an instance cannot be obtained from the pool

    - #### returnObject

          public void returnObject(T obj)
                            throws Exception

      Returns an instance to the pool, pushing it on top of the idle instance stack after successful validation and passivation. The returning instance is destroyed if any of the following are true:

      - the pool is closed
      - `validation` fails
      - `passivation` throws an exception
      If adding a validated, passivated returning instance to the stack would cause `maxSleeping` to be exceeded, the oldest (bottom) instance on the stack is destroyed to make room for the returning instance, which is pushed on top of the stack.

      Exceptions passivating or destroying instances are silently swallowed. Exceptions validating instances are propagated to the client.

      Specified by:
        `returnObject` in interface `ObjectPool<T>`
      Specified by:
        `returnObject` in class `BaseObjectPool<T>`
      Parameters:
        `obj` - instance to return to the pool
      Throws:
        `Exception`

    - #### invalidateObject

          public void invalidateObject(T obj)
                                throws Exception

      Invalidates an object from the pool.

      By contract, `obj` must have been obtained using `borrowObject`.

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

      Specified by:
        `invalidateObject` in interface `ObjectPool<T>`
      Specified by:
        `invalidateObject` in class `BaseObjectPool<T>`
      Parameters:
        `obj` - a `borrowed` instance to be disposed.
      Throws:
        `Exception`

    - #### getNumIdle

          public int getNumIdle()

      Return the number of instances currently idle in this pool.
      Specified by:
        `getNumIdle` in interface `ObjectPool<T>`
      Overrides:
        `getNumIdle` in class `BaseObjectPool<T>`
      Returns:
        the number of instances currently idle in this pool

    - #### getNumActive

          public int getNumActive()

      Return the number of instances currently borrowed from this pool.
      Specified by:
        `getNumActive` in interface `ObjectPool<T>`
      Overrides:
        `getNumActive` in class `BaseObjectPool<T>`
      Returns:
        the number of instances currently borrowed from this pool

    - #### clear

          public void clear()

      Clears any objects sitting idle in the pool. Silently swallows any exceptions thrown by `PoolableObjectFactory.destroyObject(Object)`.
      Specified by:
        `clear` in interface `ObjectPool<T>`
      Overrides:
        `clear` in class `BaseObjectPool<T>`

    - #### close

          public void close()
                     throws Exception

      Close this pool, and free any resources associated with it. Invokes `clear()` to destroy and remove instances in the pool.

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

      Specified by:
        `close` in interface `ObjectPool<T>`
      Overrides:
        `close` in class `BaseObjectPool<T>`
      Throws:
        `Exception` - never - exceptions clearing the pool are swallowed

    - #### addObject

          public void addObject()
                         throws Exception

      Create an object, and place it on top of the stack. This method is useful for "pre-loading" a pool with idle objects.

      Before being added to the pool, the newly created instance is `validated` and `passivated`. If validation fails, the new instance is `destroyed`. Exceptions generated by the factory `makeObject` or `passivate` are propagated to the caller. Exceptions destroying instances are silently swallowed.

      If a new instance is created and successfully validated and passivated and adding this instance to the pool causes `maxSleeping` to be exceeded, the oldest (bottom) instance in the pool is destroyed to make room for the newly created instance, which is pushed on top of the stack.

      Specified by:
        `addObject` in interface `ObjectPool<T>`
      Overrides:
        `addObject` in class `BaseObjectPool<T>`
      Throws:
        `Exception` - when the `factory` has a problem creating or passivating an object.

    - #### setFactory

          @Deprecated
          public void setFactory(PoolableObjectFactory<T> factory)
                          throws IllegalStateException

      Deprecated. to be removed in pool 2.0
      Sets the `factory` this pool uses to create new instances. Trying to change the `factory` while there are borrowed objects will throw an `IllegalStateException`.
      Specified by:
        `setFactory` in interface `ObjectPool<T>`
      Overrides:
        `setFactory` in class `BaseObjectPool<T>`
      Parameters:
        `factory` - the `PoolableObjectFactory` used to create new instances.
      Throws:
        `IllegalStateException` - when the factory cannot be set at this time

    - #### getFactory

          public PoolableObjectFactory<T> getFactory()

      Returns the `PoolableObjectFactory` used by this pool to create and manage object instances.
      Returns:
        the factory
      Since:
        1.5.5

    - #### getMaxSleeping

          public int getMaxSleeping()

      Returns the maximum number of idle instances in the pool.
      Returns:
        maxSleeping
      Since:
        1.5.5

Copyright © 2000-2021 Apache Software Foundation. All Rights Reserved.