A configurable {@link ObjectPool} implementation.
When coupled with the appropriate {@link PoolableObjectFactory}, GenericObjectPool provides robust pooling functionality for arbitrary objects.
A GenericObjectPool provides a number of configurable parameters:
- {@link #setMaxActive maxActive} controls the maximum number ofobjects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time. When non-positive, there is no limit to the number of objects that can be managed by the pool at one time. When {@link #setMaxActive maxActive} is reached, the pool is saidto be exhausted. The default setting for this parameter is 8.
- {@link #setMaxIdle maxIdle} controls the maximum number of objectsthat can sit idle in the pool at any time. When negative, there is no limit to the number of objects that may be idle at one time. The default setting for this parameter is 8.
- {@link #setWhenExhaustedAction whenExhaustedAction} specifies thebehavior of the {@link #borrowObject} method when the pool is exhausted:
- When {@link #setWhenExhaustedAction whenExhaustedAction} is{@link #WHEN_EXHAUSTED_FAIL}, {@link #borrowObject} will throwa {@link NoSuchElementException}
- When {@link #setWhenExhaustedAction whenExhaustedAction} is{@link #WHEN_EXHAUSTED_GROW}, {@link #borrowObject} will create a newobject and return it (essentially making {@link #setMaxActive maxActive}meaningless.)
- When {@link #setWhenExhaustedAction whenExhaustedAction}is {@link #WHEN_EXHAUSTED_BLOCK}, {@link #borrowObject} will block(invoke {@link Object#wait()}) until a new or idle object is available. If a positive {@link #setMaxWait maxWait}value is supplied, then {@link #borrowObject} will block for atmost that many milliseconds, after which a {@link NoSuchElementException}will be thrown. If {@link #setMaxWait maxWait} is non-positive,the {@link #borrowObject} method will block indefinitely.
The default whenExhaustedAction
setting is {@link #WHEN_EXHAUSTED_BLOCK} and the default maxWait
setting is -1. By default, therefore, borrowObject
will block indefinitely until an idle instance becomes available. - When {@link #setTestOnBorrow testOnBorrow} is set, the pool willattempt to validate each object before it is returned from the {@link #borrowObject} method. (Using the provided factory's{@link PoolableObjectFactory#validateObject} method.) Objects that failto validate will be dropped from the pool, and a different object will be borrowed. The default setting for this parameter is
false.
- When {@link #setTestOnReturn testOnReturn} is set, the pool willattempt to validate each object before it is returned to the pool in the {@link #returnObject} method. (Using the provided factory's{@link PoolableObjectFactory#validateObject}method.) Objects that fail to validate will be dropped from the pool. The default setting for this parameter is
false.
Optionally, one may configure the pool to examine and possibly evict objects as they sit idle in the pool and to ensure that a minimum number of idle objects are available. This is performed by an "idle object eviction" thread, which runs asynchronously. Caution should be used when configuring this optional feature. Eviction runs require an exclusive synchronization lock on the pool, so if they run too frequently and / or incur excessive latency when creating, destroying or validating object instances, performance issues may result. The idle object eviction thread may be configured using the following attributes:
- {@link #setTimeBetweenEvictionRunsMillis timeBetweenEvictionRunsMillis}indicates how long the eviction thread should sleep before "runs" of examining idle objects. When non-positive, no eviction thread will be launched. The default setting for this parameter is -1 (i.e., idle object eviction is disabled by default).
- {@link #setMinEvictableIdleTimeMillis minEvictableIdleTimeMillis}specifies the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction due to idle time. When non-positive, no object will be dropped from the pool due to idle time alone. This setting has no effect unless
timeBetweenEvictionRunsMillis > 0.
The default setting for this parameter is 30 minutes. - {@link #setTestWhileIdle testWhileIdle} indicates whether or not idleobjects should be validated using the factory's {@link PoolableObjectFactory#validateObject} method. Objects that fail tovalidate will be dropped from the pool. This setting has no effect unless
timeBetweenEvictionRunsMillis > 0.
The default setting for this parameter is false.
- {@link #setSoftMinEvictableIdleTimeMillis softMinEvictableIdleTimeMillis}specifies the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any), with the extra condition that at least "minIdle" object instances remain in the pool. When non-positive, no objects will be evicted from the pool due to idle time alone. This setting has no effect unless
timeBetweenEvictionRunsMillis > 0.
and it is superceded by {@link #setMinEvictableIdleTimeMillis minEvictableIdleTimeMillis}(that is, if minEvictableIdleTimeMillis
is positive, then softMinEvictableIdleTimeMillis
is ignored). The default setting for this parameter is -1 (disabled). - {@link #setNumTestsPerEvictionRun numTestsPerEvictionRun}determines the number of objects examined in each run of the idle object evictor. This setting has no effect unless
timeBetweenEvictionRunsMillis > 0.
The default setting for this parameter is 3.
The pool can be configured to behave as a LIFO queue with respect to idle objects - always returning the most recently used object from the pool, or as a FIFO queue, where borrowObject always returns the oldest object in the idle object pool.
- {@link #setLifo lifo}determines whether or not the pool returns idle objects in last-in-first-out order. The default setting for this parameter is
true.
GenericObjectPool is not usable without a {@link PoolableObjectFactory}. A non-null
factory must be provided either as a constructor argument or via a call to {@link #setFactory} before the pool is used.
Implementation note: To prevent possible deadlocks, care has been taken to ensure that no call to a factory method will occur within a synchronization block. See POOL-125 and DBCP-44 for more information.
@see GenericKeyedObjectPool
@author Rodney Waldhoff
@author Dirk Verbeeck
@author Sandy McArthur
@version $Revision: 784441 $ $Date: 2009-06-13 13:42:28 -0400 (Sat, 13 Jun 2009) $
@since Pool 1.0