Top Description Fields Constructors Methods
org.apache.commons.pool2.impl

public Class GenericObjectPool<T>

extends BaseGenericObjectPool<T>
implements ObjectPool<T>, GenericObjectPoolMXBean, UsageTracking<T>
Class Inheritance
All Implemented Interfaces
org.apache.commons.pool2.UsageTracking, org.apache.commons.pool2.impl.GenericObjectPoolMXBean, org.apache.commons.pool2.ObjectPool, java.io.Closeable, java.lang.AutoCloseable
Type Parameters
<T>
Type of element pooled in this pool.
Imports
java.time.Duration, .Instant, java.util.ArrayList, .NoSuchElementException, .Set, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.atomic.AtomicLong, java.util.stream.Collectors, org.apache.commons.pool2.DestroyMode, .ObjectPool, .PoolUtils, .PooledObject, .PooledObjectFactory, .PooledObjectState, .SwallowedExceptionListener, .TrackedUse, .UsageTracking

A configurable 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.

Since
2.0
See Also
GenericKeyedObjectPool

Field Summary

Modifier and TypeField and Description
private final ConcurrentHashMap<BaseGenericObjectPool.IdentityWrapper<T>, PooledObject<T>>
private final AtomicLong
private final PooledObjectFactory<T>
private volatile String
private final LinkedBlockingDeque<PooledObject<T>>
private long
private final Object
private volatile int
private volatile int
private static final String
Inherited from org.apache.commons.pool2.impl.BaseGenericObjectPool:
abandonedConfigclosedcloseLockcreatedCountdestroyedByBorrowValidationCountdestroyedByEvictorCountdestroyedCountevictionIteratorevictionLockMEAN_TIMING_STATS_CACHE_SIZE

Constructor Summary

AccessConstructor 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 GenericObjectPool using defaults from GenericObjectPoolConfig.

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 GenericObjectPool using a specific configuration.

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 GenericObjectPool that tracks and destroys objects that are checked out, but never returned to the pool.

Method Summary

Modifier and TypeMethod 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.apache.commons.pool2.ObjectPool.addObject.

Creates an object, and place it into the pool.
public T
public T

Returns:

object instance from the pool
borrowObject
(final Duration
The time to wait for an object to become available
borrowMaxWaitDuration
)

Borrows an object from the pool using the specific waiting time which only applies if getBlockWhenExhausted() is true.

public T

Returns:

object instance from the pool
borrowObject
(final long
The time to wait in milliseconds for an object to become available
borrowMaxWaitMillis
)

Borrows an object from the pool using the specific waiting time which only applies if getBlockWhenExhausted() is true.

public void
clear()

Implements org.apache.commons.pool2.ObjectPool.clear.

Clears any objects sitting idle in the pool by removing them from the idle instance pool and then invoking the configured PooledObjectFactory#destroyObject(PooledObject) method on each idle instance.
public void
private PooledObject<T>

Returns:

The new wrapped pooled object or null.
create
()

Attempts to create a new wrapped pooled object.

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 idleCount idle instances exist in the pool.

pack-priv void
ensureMinIdle()

Implements abstract org.apache.commons.pool2.impl.BaseGenericObjectPool.ensureMinIdle.

Tries to ensure that the configured minimum number of idle instances are available in the pool.
public void
evict()

Implements abstract org.apache.commons.pool2.impl.BaseGenericObjectPool.evict.

Perform numTests idle object eviction tests, evicting examined objects that meet the criteria for eviction.
public PooledObjectFactory<T>

Returns:

the factory
getFactory
()

Gets 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 type
getFactoryType
()

Implements org.apache.commons.pool2.impl.GenericObjectPoolMXBean.getFactoryType.

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 limit
getMaxIdle
()

Implements org.apache.commons.pool2.impl.GenericObjectPoolMXBean.getMaxIdle.

Gets the cap on the number of "idle" instances in the pool.
public int

Returns:

The minimum number of objects.
getMinIdle
()

Implements org.apache.commons.pool2.impl.GenericObjectPoolMXBean.getMinIdle.

Gets the target for the minimum number of idle objects to maintain in the pool.
public int
public int
private int

Returns:

The number of objects to test for validity
getNumTests
()

Calculates 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 pool
getNumWaiters
()

Implements org.apache.commons.pool2.impl.GenericObjectPoolMXBean.getNumWaiters.

Gets an estimate of the number of threads currently blocked waiting for an object from the pool.
pack-priv PooledObject<T>
getPooledObject(final T obj)

pack-priv String
public void
invalidateObject(final T
a borrowed instance to be disposed.
obj
)

Implements org.apache.commons.pool2.ObjectPool.invalidateObject.

Invalidates an object from the pool.
public void
invalidateObject(final T
a borrowed instance to be disposed.
obj
,
final DestroyMode
destroy activation context provided to the factory
destroyMode
)

Overrides default org.apache.commons.pool2.ObjectPool.invalidateObject.

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.

public Set<DefaultPooledObjectInfo>

Returns:

Information grouped on all the objects in the pool
listAllObjects
()

Implements org.apache.commons.pool2.impl.GenericObjectPoolMXBean.listAllObjects.

Provides information on all the objects in the pool, both idle (waiting to be borrowed) and active (currently borrowed).
public void
preparePool()

Tries to ensure that getMinIdle() idle instances are available in the pool.

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
a borrowed instance to be returned.
obj
)

Implements org.apache.commons.pool2.ObjectPool.returnObject.

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
Field names and values are appended to this object
builder
)

Overrides org.apache.commons.pool2.impl.BaseGenericObjectPool.toStringAppendFields.

Used by sub-classes to include the fields defined by the sub-class in the toString() output.
public void
use(final T
The object that is being used.
pooledObject
)

Implements org.apache.commons.pool2.UsageTracking.use.

Called every time a pooled object is used to enable the pool to better track borrowed objects.
private static void
wait(final Object obj, final Duration duration)

Inherited from org.apache.commons.pool2.impl.BaseGenericObjectPool:
appendStatsassertOpencreateRemoveListgetBlockWhenExhaustedgetBorrowedCountgetCreatedCountgetCreationStackTracegetDestroyedByBorrowValidationCountgetDestroyedByEvictorCountgetDestroyedCountgetDurationBetweenEvictionRunsgetEvictionPolicygetEvictionPolicyClassNamegetEvictorShutdownTimeoutgetEvictorShutdownTimeoutDurationgetEvictorShutdownTimeoutMillisgetFairnessgetJmxNamegetLifogetLogAbandonedgetMaxBorrowWaitDurationgetMaxBorrowWaitTimeMillisgetMaxTotalgetMaxWaitDurationgetMaxWaitMillisgetMeanActiveDurationgetMeanActiveTimeMillisgetMeanBorrowWaitDurationgetMeanBorrowWaitTimeMillisgetMeanIdleDurationgetMeanIdleTimeMillisgetMessageStatisticsgetMinEvictableIdleDurationgetMinEvictableIdleTimegetMinEvictableIdleTimeMillisgetNumTestsPerEvictionRungetRemoveAbandonedOnBorrowgetRemoveAbandonedOnMaintenancegetRemoveAbandonedTimeoutgetRemoveAbandonedTimeoutDurationgetReturnedCountgetSoftMinEvictableIdleDurationgetSoftMinEvictableIdleTimegetSoftMinEvictableIdleTimeMillisgetSwallowedExceptionListenergetTestOnBorrowgetTestOnCreategetTestOnReturngetTestWhileIdlegetTimeBetweenEvictionRunsgetTimeBetweenEvictionRunsMillisisAbandonedConfigisClosedjmxUnregistermarkReturningStatesetAbandonedConfigsetBlockWhenExhaustedsetConfigsetDurationBetweenEvictionRunssetEvictionPolicysetEvictionPolicyClassNamesetEvictionPolicyClassNamesetEvictorShutdownTimeoutsetEvictorShutdownTimeoutMillissetLifosetMaxTotalsetMaxWaitsetMaxWaitMillissetMessagesStatisticssetMinEvictableIdlesetMinEvictableIdleDurationsetMinEvictableIdleTimesetMinEvictableIdleTimeMillissetNumTestsPerEvictionRunsetSoftMinEvictableIdlesetSoftMinEvictableIdleDurationsetSoftMinEvictableIdleTimesetSoftMinEvictableIdleTimeMillissetSwallowedExceptionListenersetTestOnBorrowsetTestOnCreatesetTestOnReturnsetTestWhileIdlesetTimeBetweenEvictionRunssetTimeBetweenEvictionRunsMillisstartEvictorstopEvictorswallowExceptionupdateStatsBorrowupdateStatsReturn

Field Detail

allObjectsback to summary
private final ConcurrentHashMap<BaseGenericObjectPool.IdentityWrapper<T>, PooledObject<T>> allObjects
createCountback to summary
private final AtomicLong createCount
factoryback to summary
private final PooledObjectFactory<T> factory
factoryTypeback to summary
private volatile String factoryType
idleObjectsback to summary
private final LinkedBlockingDeque<PooledObject<T>> idleObjects
makeObjectCountback to summary
private long makeObjectCount
makeObjectCountLockback to summary
private final Object makeObjectCountLock
maxIdleback to summary
private volatile int maxIdle
minIdleback to summary
private volatile int minIdle
ONAME_BASEback to summary
private static final String ONAME_BASE

Constructor Detail

GenericObjectPoolback to summary
public GenericObjectPool(final PooledObjectFactory<T> factory)

Creates a new GenericObjectPool using defaults from GenericObjectPoolConfig.

Parameters
factory:PooledObjectFactory<T>

The object factory to be used to create object instances used by this pool

GenericObjectPoolback to summary
public GenericObjectPool(final PooledObjectFactory<T> factory, final GenericObjectPoolConfig<T> config)

Creates a new GenericObjectPool using a specific configuration.

Parameters
factory:PooledObjectFactory<T>

The object factory to be used to create object instances used by this pool

config: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.

GenericObjectPoolback to summary
public GenericObjectPool(final PooledObjectFactory<T> factory, final GenericObjectPoolConfig<T> config, final AbandonedConfig abandonedConfig)

Creates a new GenericObjectPool that tracks and destroys objects that are checked out, but never returned to the pool.

Parameters
factory:PooledObjectFactory<T>

The object factory to be used to create object instances used by this pool

config: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.

abandonedConfig:AbandonedConfig

Configuration for abandoned object identification and removal. The configuration is used by value.

Method Detail

addIdleObjectback 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 p is null, this is a no-op (no exception, but no impact on the pool).

Parameters
p:PooledObject<T>

The object to make idle

Exceptions
Exception:
If the factory fails to passivate the object
addObjectback to summary
public void addObject() throws Exception

Implements org.apache.commons.pool2.ObjectPool.addObject.

Creates an object, and place 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, this is a no-op (no exception, no impact to the pool).

If the factory returns null when creating an object, a NullPointerException is thrown. If there is no factory set (factory == null), an IllegalStateException is thrown.

Annotations
@Override
Exceptions
Exception:

Doc from org.apache.commons.pool2.ObjectPool.addObject.

when PooledObjectFactory#makeObject fails.

borrowObjectback to summary
public T borrowObject() throws Exception

Implements org.apache.commons.pool2.ObjectPool.borrowObject.

Doc from org.apache.commons.pool2.ObjectPool.borrowObject.

Equivalent to borrowObject(getMaxWaitDuration()). Borrows an instance from this pool.

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

By contract, clients must return the borrowed instance using returnObject, invalidateObject, or a related method as defined in an implementation or sub-interface.

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

Returns:T

an instance from this pool.

Annotations
@Override
Exceptions
Exception:
when PooledObjectFactory#makeObject throws an exception.
borrowObjectback to summary
public T borrowObject(final Duration borrowMaxWaitDuration) throws Exception

Borrows an object from the pool using the specific waiting time which only applies if getBlockWhenExhausted() is true.

If there is one or more idle instance available in the pool, then an idle instance will be selected based on the value of getLifo(), activated and returned. If activation fails, or testOnBorrow is set to true and validation fails, the instance is destroyed and the next available instance is examined. This continues until either a valid instance is returned or there are no more idle instances available.

If there are no idle instances available in the pool, behavior depends on the maxTotal, (if applicable) getBlockWhenExhausted() and the value passed in to the borrowMaxWaitMillis parameter. If the number of instances checked out from the pool is less than maxTotal, a new instance is created, activated and (if applicable) validated and returned to the caller. If validation fails, a NoSuchElementException is thrown. If the factory returns null when creating an instance, a NullPointerException is thrown.

If the pool is exhausted (no available idle instances and no capacity to create new ones), this method will either block (if getBlockWhenExhausted() is true) or throw a NoSuchElementException (if getBlockWhenExhausted() is false). The length of time that this method will block when getBlockWhenExhausted() is true is determined by the value passed in to the borrowMaxWaitMillis parameter.

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.

Parameters
borrowMaxWaitDuration:Duration

The time to wait for an object to become available

Returns:T

object instance from the pool

Exceptions
Exception:
if an object instance cannot be returned due to an error
NoSuchElementException:
if an instance cannot be returned
Since
2.10.0
borrowObjectback to summary
public T borrowObject(final long borrowMaxWaitMillis) throws Exception

Borrows an object from the pool using the specific waiting time which only applies if getBlockWhenExhausted() is true.

If there is one or more idle instance available in the pool, then an idle instance will be selected based on the value of getLifo(), activated and returned. If activation fails, or testOnBorrow is set to true and validation fails, the instance is destroyed and the next available instance is examined. This continues until either a valid instance is returned or there are no more idle instances available.

If there are no idle instances available in the pool, behavior depends on the maxTotal, (if applicable) getBlockWhenExhausted() and the value passed in to the borrowMaxWaitMillis parameter. If the number of instances checked out from the pool is less than maxTotal, a new instance is created, activated and (if applicable) validated and returned to the caller. If validation fails, a NoSuchElementException is thrown. If the factory returns null when creating an instance, a NullPointerException is thrown.

If the pool is exhausted (no available idle instances and no capacity to create new ones), this method will either block (if getBlockWhenExhausted() is true) or throw a NoSuchElementException (if getBlockWhenExhausted() is false). The length of time that this method will block when getBlockWhenExhausted() is true is determined by the value passed in to the borrowMaxWaitMillis parameter.

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.

Parameters
borrowMaxWaitMillis:long

The time to wait in milliseconds for an object to become available

Returns:T

object instance from the pool

Exceptions
Exception:
if an object instance cannot be returned due to an error
NoSuchElementException:
if an instance cannot be returned
clearback to summary
public void clear()

Implements org.apache.commons.pool2.ObjectPool.clear.

Clears any objects sitting idle in the pool by removing them from the idle instance pool and then invoking the configured PooledObjectFactory#destroyObject(PooledObject) method on each idle instance.

Implementation notes:

  • This method does not destroy or effect in any way instances that are checked out of the pool when it is invoked.
  • Invoking this method does not prevent objects being returned to the idle instance pool, even during its execution. Additional instances may be returned while removed items are being destroyed.
  • Exceptions encountered destroying idle instances are swallowed but notified via a SwallowedExceptionListener.
Annotations
@Override
closeback to summary
public void close()

Implements abstract org.apache.commons.pool2.impl.BaseGenericObjectPool.close.

Implements org.apache.commons.pool2.ObjectPool.close, java.lang.AutoCloseable.close.

Closes the pool. Once the pool is closed, borrowObject() will fail with IllegalStateException, but returnObject(Object) and invalidateObject(Object) will continue to work, with returned objects destroyed on return.

Destroys idle instances in the pool by invoking clear().

Annotations
@Override
createback to summary
private PooledObject<T> create() throws Exception

Attempts to create a new wrapped pooled object.

If there are getMaxTotal() objects already in circulation or in process of being created, this method returns null.

If the factory makeObject returns null, this method throws a NullPointerException.

Returns:PooledObject<T>

The new wrapped pooled object or null.

Exceptions
Exception:
if the object factory's makeObject fails
destroyback to summary
private void destroy(final PooledObject<T> toDestroy, final DestroyMode destroyMode) throws Exception

Destroys a wrapped pooled object.

Parameters
toDestroy:PooledObject<T>

The wrapped pooled object to destroy

destroyMode:DestroyMode

DestroyMode context provided to the factory

Exceptions
Exception:
If the factory fails to destroy the pooled object cleanly
ensureIdleback to summary
private void ensureIdle(final int idleCount, final boolean always) throws Exception

Tries to ensure that idleCount idle instances exist in the pool.

Creates and adds idle instances until either getNumIdle() reaches idleCount or the total number of objects (idle, checked out, or being created) reaches getMaxTotal(). If always is false, no instances are created unless there are threads waiting to check out instances from the pool.

If the factory returns null when creating an instance, a NullPointerException is thrown.

Parameters
idleCount:int

the number of idle instances desired

always:boolean

true means create instances even if the pool has no threads waiting

Exceptions
Exception:
if the factory's makeObject throws
ensureMinIdleback to summary
pack-priv void ensureMinIdle() throws Exception

Implements abstract org.apache.commons.pool2.impl.BaseGenericObjectPool.ensureMinIdle.

Doc from org.apache.commons.pool2.impl.BaseGenericObjectPool.ensureMinIdle.

Tries to ensure that the configured minimum number of idle instances are available in the pool.

Annotations
@Override
Exceptions
Exception:
if an error occurs creating idle instances
evictback to summary
public void evict() throws Exception

Implements abstract org.apache.commons.pool2.impl.BaseGenericObjectPool.evict.

Doc from org.apache.commons.pool2.impl.BaseGenericObjectPool.evict.

Perform numTests idle object eviction tests, evicting examined objects that meet the criteria for eviction. If testWhileIdle is true, examined objects are validated when visited (and removed if invalid); otherwise only objects that have been idle for more than minEvicableIdleTimeMillis are removed.

Successive activations of this method examine objects in sequence, cycling through objects in oldest-to-youngest order.

Annotations
@Override
Exceptions
Exception:
when there is a problem evicting idle objects.
getFactoryback to summary
public PooledObjectFactory<T> getFactory()

Gets a reference to the factory used to create, destroy and validate the objects used by this pool.

Returns:PooledObjectFactory<T>

the factory

getFactoryTypeback to summary
public String getFactoryType()

Implements org.apache.commons.pool2.impl.GenericObjectPoolMXBean.getFactoryType.

Gets the type - including the specific type rather than the generic - of the factory.

Returns:String

A string representation of the factory type

Annotations
@Override
getMaxIdleback to summary
public int getMaxIdle()

Implements org.apache.commons.pool2.impl.GenericObjectPoolMXBean.getMaxIdle.

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.

Returns:int

the maximum number of "idle" instances that can be held in the pool or a negative value if there is no limit

Annotations
@Override
See Also
setMaxIdle
getMinIdleback to summary
public int getMinIdle()

Implements org.apache.commons.pool2.impl.GenericObjectPoolMXBean.getMinIdle.

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 getDurationBetweenEvictionRuns() is greater than zero. If this is the case, an attempt is made to ensure that the pool has the required minimum number of instances during idle object eviction runs.

If the configured value of minIdle is greater than the configured value for maxIdle then the value of maxIdle will be used instead.

Returns:int

The minimum number of objects.

Annotations
@Override
See Also
setMinIdle(int), setMaxIdle(int), setDurationBetweenEvictionRuns(Duration)
getNumActiveback to summary
public int getNumActive()

Implements org.apache.commons.pool2.ObjectPool.getNumActive, org.apache.commons.pool2.impl.GenericObjectPoolMXBean.getNumActive.

Doc from org.apache.commons.pool2.ObjectPool.getNumActive.

Gets the number of instances currently borrowed from this pool. Returns a negative value if this information is not available.

Returns:int

the number of instances currently borrowed from this pool.

Annotations
@Override
getNumIdleback to summary
public int getNumIdle()

Implements abstract org.apache.commons.pool2.impl.BaseGenericObjectPool.getNumIdle.

Implements org.apache.commons.pool2.ObjectPool.getNumIdle, org.apache.commons.pool2.impl.GenericObjectPoolMXBean.getNumIdle.

Doc from org.apache.commons.pool2.ObjectPool.getNumIdle.

Gets the number of instances currently idle in this pool. This may be considered an approximation of the number of objects that can be borrowed without creating any new instances. Returns a negative value if this information is not available.

Returns:int

the number of instances currently idle in this pool.

Annotations
@Override
getNumTestsback to summary
private int getNumTests()

Calculates the number of objects to test in a run of the idle object evictor.

Returns:int

The number of objects to test for validity

getNumWaitersback to summary
public int getNumWaiters()

Implements org.apache.commons.pool2.impl.GenericObjectPoolMXBean.getNumWaiters.

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.

Returns:int

The estimate of the number of threads currently blocked waiting for an object from the pool

Annotations
@Override
getPooledObjectback to summary
pack-priv PooledObject<T> getPooledObject(final T obj)
getStatsStringback to summary
pack-priv String getStatsString()

Overrides org.apache.commons.pool2.impl.BaseGenericObjectPool.getStatsString.

Doc from org.apache.commons.pool2.impl.BaseGenericObjectPool.getStatsString.

Gets a statistics string.

Returns:String

a statistics string.

Annotations
@Override
invalidateObjectback to summary
public void invalidateObject(final T obj) throws Exception

Implements org.apache.commons.pool2.ObjectPool.invalidateObject.

Doc from org.apache.commons.pool2.ObjectPool.invalidateObject.

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.

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) DestroyMode.

Parameters
obj:T

a borrowed instance to be disposed.

Annotations
@Override
Exceptions
Exception:
if an exception occurs destroying the
IllegalStateException:
if obj does not belong to this pool
invalidateObjectback to summary
public void invalidateObject(final T obj, final DestroyMode destroyMode) throws Exception

Overrides default org.apache.commons.pool2.ObjectPool.invalidateObject.

Doc from org.apache.commons.pool2.ObjectPool.invalidateObject.

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.

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 DestroyMode.

Parameters
obj:T

a borrowed instance to be disposed.

destroyMode:DestroyMode

destroy activation context provided to the factory

Annotations
@Override
Exceptions
Exception:
if an exception occurs destroying the object
IllegalStateException:
if obj does not belong to this pool
Since
2.9.0
listAllObjectsback to summary
public Set<DefaultPooledObjectInfo> listAllObjects()

Implements org.apache.commons.pool2.impl.GenericObjectPoolMXBean.listAllObjects.

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.

Returns:Set<DefaultPooledObjectInfo>

Information grouped on all the objects in the pool

Annotations
@Override
preparePoolback to summary
public void preparePool() throws Exception

Tries to ensure that getMinIdle() idle instances are available in the pool.

Exceptions
Exception:
If the associated factory throws an exception
Since
2.4
removeAbandonedback to summary
private void removeAbandoned(final AbandonedConfig abandonedConfig)

Recovers abandoned objects which have been checked out but not used since longer than the removeAbandonedTimeout.

Parameters
abandonedConfig:AbandonedConfig

The configuration to use to identify abandoned objects

Annotations
@SuppressWarnings:resource
returnObjectback to summary
public void returnObject(final T obj)

Implements org.apache.commons.pool2.ObjectPool.returnObject.

Doc from org.apache.commons.pool2.ObjectPool.returnObject.

Returns 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.

If maxIdle is set to a positive value and the number of idle instances has reached this value, the returning instance is destroyed.

If testOnReturn == true, the returning instance is validated before being returned to the idle instance pool. In this case, if validation fails, the instance is destroyed.

Exceptions encountered destroying objects for any reason are swallowed but notified via a SwallowedExceptionListener.

Parameters
obj:T

a borrowed instance to be returned.

Annotations
@Override
setConfigback to summary
public void setConfig(final GenericObjectPoolConfig<T> conf)

Sets the base pool configuration.

Parameters
conf:GenericObjectPoolConfig<T>

the new configuration to use. This is used by value.

See Also
GenericObjectPoolConfig
setMaxIdleback 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.

Parameters
maxIdle:int

The cap on the number of "idle" instances in the pool. Use a negative value to indicate an unlimited number of idle instances

See Also
getMaxIdle
setMinIdleback 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 getDurationBetweenEvictionRuns() is greater than zero. If this is the case, an attempt is made to ensure that the pool has the required minimum number of instances during idle object eviction runs.

If the configured value of minIdle is greater than the configured value for maxIdle then the value of maxIdle will be used instead.

Parameters
minIdle:int

The minimum number of objects.

See Also
getMinIdle(), getMaxIdle(), getDurationBetweenEvictionRuns()
toStringAppendFieldsback to summary
protected void toStringAppendFields(final StringBuilder builder)

Overrides org.apache.commons.pool2.impl.BaseGenericObjectPool.toStringAppendFields.

Doc from org.apache.commons.pool2.BaseObject.toStringAppendFields.

Used by sub-classes to include the fields defined by the sub-class in the toString() output.

Parameters
builder:StringBuilder

Field names and values are appended to this object

Annotations
@Override
useback to summary
public void use(final T pooledObject)

Implements org.apache.commons.pool2.UsageTracking.use.

Doc from org.apache.commons.pool2.UsageTracking.use.

Called every time a pooled object is used to enable the pool to better track borrowed objects.

Parameters
pooledObject:T

The object that is being used.

Annotations
@Override
waitback to summary
private static void wait(final Object obj, final Duration duration) throws InterruptedException