ObjectPool implementation.
When coupled with the appropriate PooledObjectFactory,
GenericObjectPool provides robust pooling functionality for
arbitrary objects.
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 contend with client threads for access to objects in the pool, so if they run too frequently performance issues may result.
The pool can also be configured to detect and remove "abandoned" objects,
i.e. objects that have been checked out of the pool but neither used nor
returned before the configured
removeAbandonedTimeout.
Abandoned object removal can be configured to happen when
borrowObject is invoked and the pool is close to starvation, or
it can be executed by the idle object evictor, or both. If pooled objects
implement the TrackedUse interface, their last use will be queried
using the getLastUsed method on that interface; otherwise
abandonment is determined by how long an object has been checked out from
the pool.
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.
This class is intended to be thread-safe.
GenericKeyedObjectPool
| Modifier and Type | Field and Description |
|---|---|
| private final ConcurrentHashMap | |
| private final AtomicLong | |
| private final PooledObjectFactory | |
| private volatile String | |
| private final LinkedBlockingDeque | |
| private long | |
| private final Object | |
| private volatile int | |
| private volatile int | |
| private static final String |
| Access | Constructor and Description |
|---|---|
| public | GenericObjectPool(final PooledObjectFactory<T>
The object factory to be used to create object instances
used by this pool factory)Creates a new |
| public | GenericObjectPool(final PooledObjectFactory<T>
The object factory to be used to create object instances
used by this pool factory, final GenericObjectPoolConfig<T> The configuration to use for this pool instance. The
configuration is used by value. Subsequent changes to
the configuration object will not be reflected in the
pool. config)Creates a new |
| public | GenericObjectPool(final PooledObjectFactory<T>
The object factory to be used to create object instances
used by this pool factory, final GenericObjectPoolConfig<T> The base pool configuration to use for this pool instance.
The configuration is used by value. Subsequent changes to
the configuration object will not be reflected in the
pool. config, final AbandonedConfig Configuration for abandoned object identification
and removal. The configuration is used by value. abandonedConfig)Creates a new |
| Modifier and Type | Method and Description |
|---|---|
| private void | addIdleObject(final PooledObject<T>
The object to make idle p)Adds the provided wrapped pooled object to the set of idle objects for this pool. |
| public void | addObject()
Implements org. Creates an object, and places it into the pool. |
| public T | borrowObject()
Implements org. Equivalent to |
| public T | Returns: object instance from the poolThe time to wait for an object to become available, not null. maxWaitDuration)Borrows an object from the pool using the specific waiting time which only
applies if |
| public T | Returns: object instance from the poolThe time to wait in milliseconds for an object
to become available maxWaitMillis)
Deprecated
Use
borrowObject(Duration).
Borrows an object from the pool using the specific waiting time which only
applies if |
| public void | clear()
Implements org. Clears any objects sitting idle in the pool by removing them from the
idle instance pool and then invoking the configured
|
| public void | close()
Implements abstract org. Implements org. Closes the pool. |
| private PooledObject | |
| private void | destroy(final PooledObject<T>
The wrapped pooled object to destroy toDestroy, final DestroyMode DestroyMode context provided to the factory destroyMode)Destroys a wrapped pooled object. |
| private void | ensureIdle(final int
the number of idle instances desired idleCount, final boolean true means create instances even if the pool has no threads waiting always)Tries to ensure that |
| pack-priv void | ensureMinIdle()
Implements abstract org. Tries to ensure that the configured minimum number of idle instances are available in the pool. |
| public void | evict()
Implements abstract org. Perform |
| public PooledObjectFactory | Returns: the factoryGets a reference to the factory used to create, destroy and validate the objects used by this pool. |
| public String | Returns: A string representation of the factory typeImplements org. Gets the type - including the specific type rather than the generic - of the factory. |
| public int | Returns: the maximum number of "idle" instances that can be held in the pool or a negative value if there is no limitImplements org. Gets the cap on the number of "idle" instances in the pool. |
| public int | Returns: The minimum number of objects.Implements org. Gets the target for the minimum number of idle objects to maintain in the pool. |
| public int | getNumActive()
Implements org. Gets the number of instances currently borrowed from this pool. |
| public int | getNumIdle()
Implements abstract org. Implements org. Gets the number of instances currently idle in this pool. |
| private int | Returns: The number of objects to test for validityCalculates the number of objects to test in a run of the idle object evictor. |
| public int | Returns: The estimate of the number of threads currently blocked waiting for an object from the poolImplements org. Gets an estimate of the number of threads currently blocked waiting for an object from the pool. |
| pack-priv PooledObject | |
| pack-priv String | getStatsString()
Overrides org. Gets a statistics string. |
| public void | invalidateObject(final T obj)
Implements org. Invalidates an object from the pool. |
| public void | invalidateObject(final T obj, final DestroyMode destroyMode)
Overrides default org. Invalidates an object from the pool, using the provided
By contract, |
| public Set | Returns: Information grouped on all the objects in the poolImplements org. Provides information on all the objects in the pool, both idle (waiting to be borrowed) and active (currently borrowed). |
| public void | |
| private void | removeAbandoned(final AbandonedConfig
The configuration to use to identify abandoned objects abandonedConfig)Recovers abandoned objects which have been checked out but not used since longer than the removeAbandonedTimeout. |
| public void | returnObject(final T obj)
Implements org. Returns an instance to the pool. |
| public void | setConfig(final GenericObjectPoolConfig<T>
the new configuration to use. This is used by value. conf)Sets the base pool configuration. |
| public void | setMaxIdle(final int
The cap on the number of "idle" instances in the pool. Use a
negative value to indicate an unlimited number of idle
instances maxIdle)Sets the cap on the number of "idle" instances in the pool. |
| public void | setMinIdle(final int
The minimum number of objects. minIdle)Sets the target for the minimum number of idle objects to maintain in the pool. |
| protected void | toStringAppendFields(final StringBuilder builder)
Overrides org. Used by sub-classes to include the fields defined by the sub-class in the
|
| public void | use(final T pooledObject)
Implements org. Called every time a pooled object is used to enable the pool to better track borrowed objects. |
| allObjects | back to summary |
|---|---|
| private final ConcurrentHashMap<BaseGenericObjectPool. | |
| createCount | back to summary |
|---|---|
| private final AtomicLong createCount | |
| factory | back to summary |
|---|---|
| private final PooledObjectFactory<T> factory | |
| factoryType | back to summary |
|---|---|
| private volatile String factoryType | |
| idleObjects | back to summary |
|---|---|
| private final LinkedBlockingDeque<PooledObject<T>> idleObjects | |
| makeObjectCount | back to summary |
|---|---|
| private long makeObjectCount | |
| makeObjectCountLock | back to summary |
|---|---|
| private final Object makeObjectCountLock | |
| maxIdle | back to summary |
|---|---|
| private volatile int maxIdle | |
| minIdle | back to summary |
|---|---|
| private volatile int minIdle | |
| ONAME_BASE | back to summary |
|---|---|
| private static final String ONAME_BASE | |
| GenericObjectPool | back to summary |
|---|---|
| public GenericObjectPool(final PooledObjectFactory<T> factory) Creates a new
| |
| GenericObjectPool | back to summary |
|---|---|
| public GenericObjectPool(final PooledObjectFactory<T> factory, final GenericObjectPoolConfig<T> config) Creates a new
| |
| GenericObjectPool | back to summary |
|---|---|
| public GenericObjectPool(final PooledObjectFactory<T> factory, final GenericObjectPoolConfig<T> config, final AbandonedConfig abandonedConfig) Creates a new
| |
| addIdleObject | back to summary |
|---|---|
| private void addIdleObject(final PooledObject<T> p) throws Exception Adds the provided wrapped pooled object to the set of idle objects for
this pool. The object must already be part of the pool. If
| |
| addObject | back to summary |
|---|---|
| public void addObject() throws Exception Implements org. Creates an object, and places it into the pool. addObject() is useful for "pre-loading" a pool with idle objects.
If there is no capacity available to add to the pool, or there are already
If the factory returns null when creating an object, a
| |
| borrowObject | back to summary |
|---|---|
| public T borrowObject() throws Exception Implements org. Doc from org. Equivalent to
Instances returned from this method will have been either newly created
with
By contract, clients must return the borrowed instance
using The behavior of this method when the pool has been exhausted is not strictly specified (although it may be specified by implementations).
| |
| borrowObject | back to summary |
|---|---|
| public T borrowObject(final Duration maxWaitDuration) throws Exception Borrows an object from the pool using the specific waiting time which only
applies if
If there is one or more idle instance available in the pool, then an
idle instance will be selected based on the value of
If there are no idle instances available in the pool, behavior depends on
the
If the pool is exhausted (no available idle instances and no capacity to
create new ones), this method will either block (if
When the pool is exhausted, multiple calling threads may be simultaneously blocked waiting for instances to become available. A "fairness" algorithm has been implemented to ensure that threads receive available instances in request arrival order.
| |
| borrowObject | back to summary |
|---|---|
| public T borrowObject(final long maxWaitMillis) throws Exception
Deprecated Borrows an object from the pool using the specific waiting time which only
applies if
If there is one or more idle instance available in the pool, then an
idle instance will be selected based on the value of
If there are no idle instances available in the pool, behavior depends on
the
If the pool is exhausted (no available idle instances and no capacity to
create new ones), this method will either block (if
When the pool is exhausted, multiple calling threads may be simultaneously blocked waiting for instances to become available. A "fairness" algorithm has been implemented to ensure that threads receive available instances in request arrival order.
| |
| clear | back to summary |
|---|---|
| public void clear() Implements org. Clears any objects sitting idle in the pool by removing them from the
idle instance pool and then invoking the configured
Implementation notes:
| |
| close | back to summary |
|---|---|
| public void close() Implements abstract org. Implements org. Closes the pool. Once the pool is closed,
Destroys idle instances in the pool by invoking
| |
| create | back to summary |
|---|---|
| private PooledObject Attempts to create a new wrapped pooled object.
If there are If the factory makeObject returns null, this method throws a NullPointerException.
| |
| destroy | back to summary |
|---|---|
| private void destroy(final PooledObject<T> toDestroy, final DestroyMode destroyMode) throws Exception Destroys a wrapped pooled object.
| |
| ensureIdle | back to summary |
|---|---|
| private void ensureIdle(final int idleCount, final boolean always) throws Exception Tries to ensure that
Creates and adds idle instances until either
If the factory returns null when creating an instance, a
| |
| ensureMinIdle | back to summary |
|---|---|
| pack-priv void ensureMinIdle() throws Exception Implements abstract org. Doc from org. Tries to ensure that the configured minimum number of idle instances are available in the pool. | |
| evict | back to summary |
|---|---|
| public void evict() throws Exception Implements abstract org. Doc from org. Perform Successive activations of this method examine objects in sequence, cycling through objects in oldest-to-youngest order. | |
| getFactory | back to summary |
|---|---|
| public PooledObjectFactory Gets a reference to the factory used to create, destroy and validate the objects used by this pool.
| |
| getFactoryType | back to summary |
|---|---|
| public String getFactoryType() Implements org. Gets the type - including the specific type rather than the generic - of the factory. | |
| getMaxIdle | back to summary |
|---|---|
| public int getMaxIdle() Implements org. Gets the cap on the number of "idle" instances in the pool. If maxIdle is set too low on heavily loaded systems it is possible you will see objects being destroyed and almost immediately new objects being created. This is a result of the active threads momentarily returning objects faster than they are requesting them, causing the number of idle objects to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a good starting point.
| |
| getMinIdle | back to summary |
|---|---|
| public int getMinIdle() Implements org. Gets the target for the minimum number of idle objects to maintain in
the pool. This setting only has an effect if it is positive and
If the configured value of minIdle is greater than the configured value for maxIdle then the value of maxIdle will be used instead.
| |
| getNumActive | back to summary |
|---|---|
| public int getNumActive() Implements org. Doc from org. Gets the number of instances currently borrowed from this pool. Returns a negative value if this information is not available.
| |
| getNumIdle | back to summary |
|---|---|
| public int getNumIdle() Implements abstract org. Implements org. Doc from org. Gets the number of instances currently idle in this pool. This may be
considered an approximation of the number of objects that can be
| |
| getNumTests | back to summary |
|---|---|
| private int getNumTests() Calculates the number of objects to test in a run of the idle object evictor.
| |
| getNumWaiters | back to summary |
|---|---|
| public int getNumWaiters() Implements org. Gets an estimate of the number of threads currently blocked waiting for an object from the pool. This is intended for monitoring only, not for synchronization control.
| |
| getPooledObject | back to summary |
|---|---|
| pack-priv PooledObject | |
| getStatsString | back to summary |
|---|---|
| pack-priv String getStatsString() Overrides org. Doc from org. Gets a statistics string.
| |
| invalidateObject | back to summary |
|---|---|
| public void invalidateObject(final T obj) throws Exception Implements org. Doc from org. Invalidates an object from the pool.
By contract, This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.
Activation of this method decrements the active count and attempts to destroy the instance, using the default
(NORMAL)
| |
| invalidateObject | back to summary |
|---|---|
| public void invalidateObject(final T obj, final DestroyMode destroyMode) throws Exception Overrides default org. Doc from org. Invalidates an object from the pool, using the provided
By contract, This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.
Activation of this method decrements the active count and attempts to destroy the instance, using the provided
| |
| listAllObjects | back to summary |
|---|---|
| public Set Implements org. Provides information on all the objects in the pool, both idle (waiting to be borrowed) and active (currently borrowed). Note This is named listAllObjects so it is presented as an operation via JMX. That means it won't be invoked unless the explicitly requested whereas all attributes will be automatically requested when viewing the attributes for an object in a tool like JConsole.
| |
| preparePool | back to summary |
|---|---|
| public void preparePool() throws Exception Tries to ensure that
| |
| removeAbandoned | back to summary |
|---|---|
| private void removeAbandoned(final AbandonedConfig abandonedConfig) Recovers abandoned objects which have been checked out but
not used since longer than the removeAbandonedTimeout. For each object
deemed abandoned,
| |
| returnObject | back to summary |
|---|---|
| public void returnObject(final T obj) Implements org. Doc from org. Returns an instance to the pool. By contract,
If
If
Exceptions encountered destroying objects for any reason are swallowed
but notified via a
| |
| setConfig | back to summary |
|---|---|
| public void setConfig(final GenericObjectPoolConfig<T> conf) Sets the base pool configuration.
| |
| setMaxIdle | back to summary |
|---|---|
| public void setMaxIdle(final int maxIdle) Sets the cap on the number of "idle" instances in the pool. If maxIdle is set too low on heavily loaded systems it is possible you will see objects being destroyed and almost immediately new objects being created. This is a result of the active threads momentarily returning objects faster than they are requesting them, causing the number of idle objects to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a good starting point.
| |
| setMinIdle | back to summary |
|---|---|
| public void setMinIdle(final int minIdle) Sets the target for the minimum number of idle objects to maintain in
the pool. This setting only has an effect if it is positive and
If the configured value of minIdle is greater than the configured value for maxIdle then the value of maxIdle will be used instead.
| |
| toStringAppendFields | back to summary |
|---|---|
| protected void toStringAppendFields(final StringBuilder builder) Overrides org. Doc from org. Used by sub-classes to include the fields defined by the sub-class in the
| |
| use | back to summary |
|---|---|
| public void use(final T pooledObject) Implements org. Doc from org. Called every time a pooled object is used to enable the pool to better track borrowed objects.
| |