GenericObjectPool

A configurable {@link ObjectPool} implementation. <p> When coupled with the appropriate {@link PooledObjectFactory}, <code>GenericObjectPool</code> provides robust pooling functionality for arbitrary objects.</p> <p> 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.</p> <p> 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 {@link AbandonedConfig#getRemoveAbandonedTimeout() removeAbandonedTimeout}. Abandoned object removal can be configured to happen when <code>borrowObject</code> 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 {@link TrackedUse} interface, their last use will be queried using the <code>getLastUsed</code> method on that interface; otherwise abandonment is determined by how long an object has been checked out from the pool.</p> <p> 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.</p> <p> This class is intended to be thread-safe.</p>

@see GenericKeyedObjectPool

@param <T> Type of element pooled in this pool.

Constructors

this
this(PooledObjectFactory!(T) factory)

Creates a new <code>GenericObjectPool</code> using defaults from {@link GenericObjectPoolConfig}.

this
this(PooledObjectFactory!(T) factory, GenericObjectPoolConfig config)

Creates a new <code>GenericObjectPool</code> using a specific configuration.

this
this(PooledObjectFactory!(T) factory, GenericObjectPoolConfig config, AbandonedConfig abandonedConfig)

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

Members

Aliases

setConfig
alias setConfig = BaseGenericObjectPool.setConfig
Undocumented in source.

Functions

addObject
void addObject()

Creates an object, and place it into the pool. addObject() is useful for "pre-loading" a pool with idle objects. <p> If there is no capacity available to add to the pool, this is a no-op (no exception, no impact to the pool). </p>

borrowObject
T borrowObject()

Equivalent to <code>{@link #borrowObject(long) borrowObject}({@link #getMaxWaitMillis()})</code>. <p> {@inheritDoc} </p>

borrowObject
T borrowObject(long borrowMaxWaitMillis)

Borrows an object from the pool using the specific waiting time which only applies if {@link #getBlockWhenExhausted()} is true. <p> If there is one or more idle instance available in the pool, then an idle instance will be selected based on the value of {@link #getLifo()}, activated and returned. If activation fails, or {@link #getTestOnBorrow() testOnBorrow} is set to <code>true</code> 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. </p> <p> If there are no idle instances available in the pool, behavior depends on the {@link #getMaxTotal() maxTotal}, (if applicable) {@link #getBlockWhenExhausted()} and the value passed in to the <code>borrowMaxWaitMillis</code> parameter. If the number of instances checked out from the pool is less than <code>maxTotal,</code> a new instance is created, activated and (if applicable) validated and returned to the caller. If validation fails, a <code>NoSuchElementException</code> is thrown. </p> <p> If the pool is exhausted (no available idle instances and no capacity to create new ones), this method will either block (if {@link #getBlockWhenExhausted()} is true) or throw a <code>NoSuchElementException</code> (if {@link #getBlockWhenExhausted()} is false). The length of time that this method will block when {@link #getBlockWhenExhausted()} is true is determined by the value passed in to the <code>borrowMaxWaitMillis</code> parameter. </p> <p> 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. </p>

clear
void clear()

Clears any objects sitting idle in the pool by removing them from the idle instance pool and then invoking the configured {@link PooledObjectFactory#destroyObject(PooledObject)} method on each idle instance. <p> Implementation notes: </p> <ul> <li>This method does not destroy or effect in any way instances that are checked out of the pool when it is invoked.</li> <li>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.</li> <li>Exceptions encountered destroying idle instances are swallowed but notified via a {@link SwallowedExceptionListener}.</li> </ul>

close
void close()

Closes the pool. Once the pool is closed, {@link #borrowObject()} will fail with IllegalStateException, but {@link #returnObject(Object)} and {@link #invalidateObject(Object)} will continue to work, with returned objects destroyed on return. <p> Destroys idle instances in the pool by invoking {@link #clear()}. </p>

ensureMinIdle
void ensureMinIdle()
Undocumented in source. Be warned that the author may not have intended to support it.
evict
void evict()

{@inheritDoc} <p> Successive activations of this method examine objects in sequence, cycling through objects in oldest-to-youngest order. </p>

getFactory
PooledObjectFactory!(T) getFactory()

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

getFactoryType
string getFactoryType()

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

getLogAbandoned
bool getLogAbandoned()

Gets whether this pool identifies and logs any abandoned objects.

getMaxIdle
int getMaxIdle()

Returns 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
int getMinIdle()

Returns 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 {@link #getTimeBetweenEvictionRunsMillis()} 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. <p> If the configured value of minIdle is greater than the configured value for maxIdle then the value of maxIdle will be used instead. </p>

getNumActive
int getNumActive()
Undocumented in source. Be warned that the author may not have intended to support it.
getNumIdle
int getNumIdle()
Undocumented in source. Be warned that the author may not have intended to support it.
getNumWaiters
int getNumWaiters()

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

getRemoveAbandonedOnBorrow
bool getRemoveAbandonedOnBorrow()

Gets whether a check is made for abandoned objects when an object is borrowed from this pool.

getRemoveAbandonedOnMaintenance
bool getRemoveAbandonedOnMaintenance()

Gets whether a check is made for abandoned objects when the evictor runs.

getRemoveAbandonedTimeout
int getRemoveAbandonedTimeout()

Obtains the timeout before which an object will be considered to be abandoned by this pool.

invalidateObject
void invalidateObject(T obj)

{@inheritDoc} <p> Activation of this method decrements the active count and attempts to destroy the instance. </p>

isAbandonedConfig
bool isAbandonedConfig()

Gets whether or not abandoned object removal is configured for this pool.

listAllObjects
Set!(DefaultPooledObjectInfo) listAllObjects()

Provides information on all the objects in the pool, both idle (waiting to be borrowed) and active (currently borrowed). <p> 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. </p>

preparePool
void preparePool()

Tries to ensure that {@link #getMinIdle()} idle instances are available in the pool.

returnObject
void returnObject(T obj)

{@inheritDoc} <p> If {@link #getMaxIdle() maxIdle} is set to a positive value and the number of idle instances has reached this value, the returning instance is destroyed. </p> <p> If {@link #getTestOnReturn() 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. </p> <p> Exceptions encountered destroying objects for any reason are swallowed but notified via a {@link SwallowedExceptionListener}. </p>

setAbandonedConfig
void setAbandonedConfig(AbandonedConfig abandonedConfig)

Sets the abandoned object removal configuration.

setConfig
void setConfig(GenericObjectPoolConfig conf)

Sets the base pool configuration.

setMaxIdle
void setMaxIdle(int maxIdle)

Returns 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
void setMinIdle(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 {@link #getTimeBetweenEvictionRunsMillis()} 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. <p> If the configured value of minIdle is greater than the configured value for maxIdle then the value of maxIdle will be used instead. </p>

toStringAppendFields
void toStringAppendFields(StringBuilder builder)
Undocumented in source. Be warned that the author may not have intended to support it.
use
void use(T pooledObject)
Undocumented in source. Be warned that the author may not have intended to support it.

Inherited Members

From BaseGenericObjectPool

MEAN_TIMING_STATS_CACHE_SIZE
enum int MEAN_TIMING_STATS_CACHE_SIZE;

The size of the caches used to store historical data for some attributes so that rolling means may be calculated.

closeLock
Object closeLock;
Undocumented in source.
closed
bool closed;
Undocumented in source.
evictionLock
Object evictionLock;
Undocumented in source.
evictionIterator
EvictionIterator evictionIterator;
Undocumented in source.
createdCount
long createdCount;
Undocumented in source.
destroyedCount
long destroyedCount;
Undocumented in source.
destroyedByEvictorCount
long destroyedByEvictorCount;
Undocumented in source.
destroyedByBorrowValidationCount
long destroyedByBorrowValidationCount;
Undocumented in source.
getMaxTotal
int getMaxTotal()

Returns the maximum number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time. When negative, there is no limit to the number of objects that can be managed by the pool at one time.

setMaxTotal
void setMaxTotal(int maxTotal)

Sets the cap on the number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time. Use a negative value for no limit.

getBlockWhenExhausted
bool getBlockWhenExhausted()

Returns whether to block when the <code>borrowObject()</code> method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).

setBlockWhenExhausted
void setBlockWhenExhausted(bool blockWhenExhausted)

Sets whether to block when the <code>borrowObject()</code> method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).

setConfig
void setConfig(BaseObjectPoolConfig conf)
Undocumented in source. Be warned that the author may not have intended to support it.
getMaxWaitMillis
long getMaxWaitMillis()

Returns the maximum amount of time (in milliseconds) the <code>borrowObject()</code> method should block before throwing an exception when the pool is exhausted and {@link #getBlockWhenExhausted} is true. When less than 0, the <code>borrowObject()</code> method may block indefinitely.

setMaxWaitMillis
void setMaxWaitMillis(long maxWaitMillis)

Sets the maximum amount of time (in milliseconds) the <code>borrowObject()</code> method should block before throwing an exception when the pool is exhausted and {@link #getBlockWhenExhausted} is true. When less than 0, the <code>borrowObject()</code> method may block indefinitely.

getLifo
bool getLifo()

Returns whether the pool has LIFO (last in, first out) behaviour with respect to idle objects - always returning the most recently used object from the pool, or as a FIFO (first in, first out) queue, where the pool always returns the oldest object in the idle object pool.

getFairness
bool getFairness()

Returns whether or not the pool serves threads waiting to borrow objects fairly. True means that waiting threads are served as if waiting in a FIFO queue.

setLifo
void setLifo(bool lifo)

Sets whether the pool has LIFO (last in, first out) behaviour with respect to idle objects - always returning the most recently used object from the pool, or as a FIFO (first in, first out) queue, where the pool always returns the oldest object in the idle object pool.

getTestOnCreate
bool getTestOnCreate()

Returns whether objects created for the pool will be validated before being returned from the <code>borrowObject()</code> method. Validation is performed by the <code>validateObject()</code> method of the factory associated with the pool. If the object fails to validate, then <code>borrowObject()</code> will fail.

setTestOnCreate
void setTestOnCreate(bool testOnCreate)

Sets whether objects created for the pool will be validated before being returned from the <code>borrowObject()</code> method. Validation is performed by the <code>validateObject()</code> method of the factory associated with the pool. If the object fails to validate, then <code>borrowObject()</code> will fail.

getTestOnBorrow
bool getTestOnBorrow()

Returns whether objects borrowed from the pool will be validated before being returned from the <code>borrowObject()</code> method. Validation is performed by the <code>validateObject()</code> method of the factory associated with the pool. If the object fails to validate, it will be removed from the pool and destroyed, and a new attempt will be made to borrow an object from the pool.

setTestOnBorrow
void setTestOnBorrow(bool testOnBorrow)

Sets whether objects borrowed from the pool will be validated before being returned from the <code>borrowObject()</code> method. Validation is performed by the <code>validateObject()</code> method of the factory associated with the pool. If the object fails to validate, it will be removed from the pool and destroyed, and a new attempt will be made to borrow an object from the pool.

getTestOnReturn
bool getTestOnReturn()

Returns whether objects borrowed from the pool will be validated when they are returned to the pool via the <code>returnObject()</code> method. Validation is performed by the <code>validateObject()</code> method of the factory associated with the pool. Returning objects that fail validation are destroyed rather then being returned the pool.

setTestOnReturn
void setTestOnReturn(bool testOnReturn)

Sets whether objects borrowed from the pool will be validated when they are returned to the pool via the <code>returnObject()</code> method. Validation is performed by the <code>validateObject()</code> method of the factory associated with the pool. Returning objects that fail validation are destroyed rather then being returned the pool.

getTestWhileIdle
bool getTestWhileIdle()

Returns whether objects sitting idle in the pool will be validated by the idle object evictor (if any - see {@link #setTimeBetweenEvictionRunsMillis(long)}). Validation is performed by the <code>validateObject()</code> method of the factory associated with the pool. If the object fails to validate, it will be removed from the pool and destroyed.

setTestWhileIdle
void setTestWhileIdle(bool testWhileIdle)

Returns whether objects sitting idle in the pool will be validated by the idle object evictor (if any - see {@link #setTimeBetweenEvictionRunsMillis(long)}). Validation is performed by the <code>validateObject()</code> method of the factory associated with the pool. If the object fails to validate, it will be removed from the pool and destroyed. Note that setting this property has no effect unless the idle object evictor is enabled by setting <code>timeBetweenEvictionRunsMillis</code> to a positive value.

getTimeBetweenEvictionRunsMillis
long getTimeBetweenEvictionRunsMillis()

Returns the number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.

setTimeBetweenEvictionRunsMillis
void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)

Sets the number of milliseconds to sleep between runs of the idle object evictor thread. <ul> <li>When positive, the idle object evictor thread starts.</li> <li>When non-positive, no idle object evictor thread runs.</li> </ul>

getNumTestsPerEvictionRun
int getNumTestsPerEvictionRun()

Returns the maximum number of objects to examine during each run (if any) of the idle object evictor thread. When positive, the number of tests performed for a run will be the minimum of the configured value and the number of idle instances in the pool. When negative, the number of tests performed will be <code>ceil({@link #getNumIdle}/ abs({@link #getNumTestsPerEvictionRun}))</code> which means that when the value is <code>-n</code> roughly one nth of the idle objects will be tested per run.

setNumTestsPerEvictionRun
void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)

Sets the maximum number of objects to examine during each run (if any) of the idle object evictor thread. When positive, the number of tests performed for a run will be the minimum of the configured value and the number of idle instances in the pool. When negative, the number of tests performed will be <code>ceil({@link #getNumIdle}/ abs({@link #getNumTestsPerEvictionRun}))</code> which means that when the value is <code>-n</code> roughly one nth of the idle objects will be tested per run.

getMinEvictableIdleTimeMillis
long getMinEvictableIdleTimeMillis()

Returns 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 - see {@link #setTimeBetweenEvictionRunsMillis(long)}). When non-positive, no objects will be evicted from the pool due to idle time alone.

setMinEvictableIdleTimeMillis
void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)

Sets 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 - see {@link #setTimeBetweenEvictionRunsMillis(long)}). When non-positive, no objects will be evicted from the pool due to idle time alone.

getSoftMinEvictableIdleTimeMillis
long getSoftMinEvictableIdleTimeMillis()

Returns 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 - see {@link #setTimeBetweenEvictionRunsMillis(long)}), with the extra condition that at least <code>minIdle</code> object instances remain in the pool. This setting is overridden by {@link #getMinEvictableIdleTimeMillis} (that is, if {@link #getMinEvictableIdleTimeMillis} is positive, then {@link #getSoftMinEvictableIdleTimeMillis} is ignored).

setSoftMinEvictableIdleTimeMillis
void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis)

Sets 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 - see {@link #setTimeBetweenEvictionRunsMillis(long)}), with the extra condition that at least <code>minIdle</code> object instances remain in the pool. This setting is overridden by {@link #getMinEvictableIdleTimeMillis} (that is, if {@link #getMinEvictableIdleTimeMillis} is positive, then {@link #getSoftMinEvictableIdleTimeMillis} is ignored).

getEvictionPolicyClassName
string getEvictionPolicyClassName()

Returns the name of the {@link EvictionPolicy} implementation that is used by this pool.

setEvictionPolicy
void setEvictionPolicy(EvictionPolicy evictionPolicy)

Sets the eviction policy for this pool.

getEvictorShutdownTimeoutMillis
long getEvictorShutdownTimeoutMillis()

Gets the timeout that will be used when waiting for the Evictor to shutdown if this pool is closed and it is the only pool still using the the value for the Evictor.

setEvictorShutdownTimeoutMillis
void setEvictorShutdownTimeoutMillis(long evictorShutdownTimeoutMillis)

Sets the timeout that will be used when waiting for the Evictor to shutdown if this pool is closed and it is the only pool still using the the value for the Evictor.

close
void close()

Closes the pool, destroys the remaining idle objects and, if registered in JMX, deregisters it.

isClosed
bool isClosed()

Has this pool instance been closed. @return <code>true</code> when this pool has been closed.

evict
void evict()

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

getEvictionPolicy
EvictionPolicy getEvictionPolicy()

Returns the {@link EvictionPolicy} defined for this pool.

assertOpen
void assertOpen()

Verifies that the pool is open. @throws IllegalStateException if the pool is closed.

startEvictor
void startEvictor(long delay)

<p>Starts the evictor with the given delay. If there is an evictor running when this method is called, it is stopped and replaced with a new evictor with the specified delay.</p>

stopEvitor
void stopEvitor()

Stops the evictor.

ensureMinIdle
void ensureMinIdle()

Tries to ensure that the configured minimum number of idle instances are available in the pool. @throws Exception if an error occurs creating idle instances

getCreationStackTrace
string getCreationStackTrace()

Provides the stack trace for the call that created this pool. JMX registration may trigger a memory leak so it is important that pools are deregistered when no longer used by calling the {@link #close()} method. This method is provided to assist with identifying code that creates but does not close it thereby creating a memory leak. @return pool creation stack trace

getBorrowedCount
long getBorrowedCount()

The total number of objects successfully borrowed from this pool over the lifetime of the pool. @return the borrowed object count

getReturnedCount
long getReturnedCount()

The total number of objects returned to this pool over the lifetime of the pool. This excludes attempts to return the same object multiple times. @return the returned object count

getCreatedCount
long getCreatedCount()

The total number of objects created for this pool over the lifetime of the pool. @return the created object count

getDestroyedCount
long getDestroyedCount()

The total number of objects destroyed by this pool over the lifetime of the pool. @return the destroyed object count

getDestroyedByEvictorCount
long getDestroyedByEvictorCount()

The total number of objects destroyed by the evictor associated with this pool over the lifetime of the pool. @return the evictor destroyed object count

getDestroyedByBorrowValidationCount
long getDestroyedByBorrowValidationCount()

The total number of objects destroyed by this pool as a result of failing validation during <code>borrowObject()</code> over the lifetime of the pool. @return validation destroyed object count

getMeanActiveTimeMillis
long getMeanActiveTimeMillis()

The mean time objects are active for based on the last {@link #MEAN_TIMING_STATS_CACHE_SIZE} objects returned to the pool. @return mean time an object has been checked out from the pool among recently returned objects

getMeanIdleTimeMillis
long getMeanIdleTimeMillis()

The mean time objects are idle for based on the last {@link #MEAN_TIMING_STATS_CACHE_SIZE} objects borrowed from the pool. @return mean time an object has been idle in the pool among recently borrowed objects

getMeanBorrowWaitTimeMillis
long getMeanBorrowWaitTimeMillis()

The mean time threads wait to borrow an object based on the last {@link #MEAN_TIMING_STATS_CACHE_SIZE} objects borrowed from the pool. @return mean time in milliseconds that a recently served thread has had to wait to borrow an object from the pool

getMaxBorrowWaitTimeMillis
long getMaxBorrowWaitTimeMillis()

The maximum time a thread has waited to borrow objects from the pool. @return maximum wait time in milliseconds since the pool was created

getNumIdle
int getNumIdle()

The number of instances currently idle in this pool. @return count of instances available for checkout from the pool

getSwallowedExceptionListener
SwallowedExceptionListener getSwallowedExceptionListener()

The listener used (if any) to receive notifications of exceptions unavoidably swallowed by the pool.

setSwallowedExceptionListener
void setSwallowedExceptionListener(SwallowedExceptionListener swallowedExceptionListener)

The listener used (if any) to receive notifications of exceptions unavoidably swallowed by the pool.

swallowException
void swallowException(Exception swallowException)

Swallows an exception and notifies the configured listener for swallowed exceptions queue.

updateStatsBorrow
void updateStatsBorrow(IPooledObject p, long waitTime)

Updates statistics after an object is borrowed from the pool. @param p object borrowed from the pool @param waitTime time (in milliseconds) that the borrowing thread had to wait

updateStatsReturn
void updateStatsReturn(long activeTime)

Updates statistics after an object is returned to the pool. @param activeTime the amount of time (in milliseconds) that the returning object was checked out

markReturningState
void markReturningState(IPooledObject pooledObject)

Marks the object as returning to the pool. @param pooledObject instance to return to the keyed pool

Evictor
class Evictor

The idle object evictor {@link TimerTask}.

toStringAppendFields
void toStringAppendFields(StringBuilder builder)
Undocumented in source. Be warned that the author may not have intended to support it.

Meta