Class BaseGenericObjectPool<T>
- java.lang.Object
-
- org.datanucleus.store.rdbms.datasource.dbcp2.pool2.BaseObject
-
- org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.BaseGenericObjectPool<T>
-
- Type Parameters:
T- Type of element pooled in this pool. This class is intended to be thread-safe.
- Direct Known Subclasses:
GenericKeyedObjectPool,GenericObjectPool
public abstract class BaseGenericObjectPool<T> extends BaseObject
Base class that provides common functionality forGenericObjectPoolandGenericKeyedObjectPool. The primary reason this class exists is reduce code duplication between the two pool implementations.- Since:
- 2.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classBaseGenericObjectPool.EvictionIteratorThe idle object eviction iterator.(package private) classBaseGenericObjectPool.EvictorThe idle object evictorTimerTask.(package private) static classBaseGenericObjectPool.IdentityWrapper<T>Wrapper for objects under management by the pool.private classBaseGenericObjectPool.StatsStoreMaintains a cache of values for a single metric and reports statistics on the cached values.
-
Field Summary
Fields Modifier and Type Field Description private BaseGenericObjectPool.StatsStoreactiveTimesprivate booleanblockWhenExhaustedprivate java.util.concurrent.atomic.AtomicLongborrowedCount(package private) booleanclosed(package private) java.lang.ObjectcloseLock(package private) java.util.concurrent.atomic.AtomicLongcreatedCountprivate java.lang.StringcreationStackTrace(package private) java.util.concurrent.atomic.AtomicLongdestroyedByBorrowValidationCount(package private) java.util.concurrent.atomic.AtomicLongdestroyedByEvictorCount(package private) java.util.concurrent.atomic.AtomicLongdestroyedCountprivate static java.lang.StringEVICTION_POLICY_TYPE_NAME(package private) BaseGenericObjectPool.EvictionIteratorevictionIterator(package private) java.lang.ObjectevictionLockprivate EvictionPolicy<T>evictionPolicyprivate BaseGenericObjectPool.Evictorevictorprivate longevictorShutdownTimeoutMillisprivate java.lang.ref.WeakReference<java.lang.ClassLoader>factoryClassLoaderprivate booleanfairnessprivate BaseGenericObjectPool.StatsStoreidleTimesprivate booleanlifoprivate java.util.concurrent.atomic.AtomicLongmaxBorrowWaitTimeMillisprivate intmaxTotalprivate longmaxWaitMillisstatic intMEAN_TIMING_STATS_CACHE_SIZEThe size of the caches used to store historical data for some attributes so that rolling means may be calculated.private longminEvictableIdleTimeMillisprivate intnumTestsPerEvictionRunprivate javax.management.ObjectNameobjectNameprivate java.util.concurrent.atomic.AtomicLongreturnedCountprivate longsoftMinEvictableIdleTimeMillisprivate SwallowedExceptionListenerswallowedExceptionListenerprivate booleantestOnBorrowprivate booleantestOnCreateprivate booleantestOnReturnprivate booleantestWhileIdleprivate longtimeBetweenEvictionRunsMillisprivate BaseGenericObjectPool.StatsStorewaitTimes
-
Constructor Summary
Constructors Constructor Description BaseGenericObjectPool(BaseObjectPoolConfig<T> config, java.lang.String jmxNameBase, java.lang.String jmxNamePrefix)Handles JMX registration (if required) and the initialization required for monitoring.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) voidassertOpen()Verifies that the pool is open.abstract voidclose()Closes the pool, destroys the remaining idle objects and, if registered in JMX, deregisters it.(package private) abstract voidensureMinIdle()Tries to ensure that the configured minimum number of idle instances are available in the pool.abstract voidevict()PerformnumTestsidle object eviction tests, evicting examined objects that meet the criteria for eviction.booleangetBlockWhenExhausted()Returns whether to block when theborrowObject()method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).longgetBorrowedCount()The total number of objects successfully borrowed from this pool over the lifetime of the pool.longgetCreatedCount()The total number of objects created for this pool over the lifetime of the pool.java.lang.StringgetCreationStackTrace()Provides the stack trace for the call that created this pool.longgetDestroyedByBorrowValidationCount()The total number of objects destroyed by this pool as a result of failing validation duringborrowObject()over the lifetime of the pool.longgetDestroyedByEvictorCount()The total number of objects destroyed by the evictor associated with this pool over the lifetime of the pool.longgetDestroyedCount()The total number of objects destroyed by this pool over the lifetime of the pool.EvictionPolicy<T>getEvictionPolicy()Returns theEvictionPolicydefined for this pool.java.lang.StringgetEvictionPolicyClassName()Returns the name of theEvictionPolicyimplementation that is used by this pool.longgetEvictorShutdownTimeoutMillis()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.booleangetFairness()Returns whether or not the pool serves threads waiting to borrow objects fairly.javax.management.ObjectNamegetJmxName()Provides the name under which the pool has been registered with the platform MBean server ornullif the pool has not been registered.booleangetLifo()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.longgetMaxBorrowWaitTimeMillis()The maximum time a thread has waited to borrow objects from the pool.intgetMaxTotal()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.longgetMaxWaitMillis()Returns the maximum amount of time (in milliseconds) theborrowObject()method should block before throwing an exception when the pool is exhausted andgetBlockWhenExhausted()is true.longgetMeanActiveTimeMillis()The mean time objects are active for based on the lastMEAN_TIMING_STATS_CACHE_SIZEobjects returned to the pool.longgetMeanBorrowWaitTimeMillis()The mean time threads wait to borrow an object based on the lastMEAN_TIMING_STATS_CACHE_SIZEobjects borrowed from the pool.longgetMeanIdleTimeMillis()The mean time objects are idle for based on the lastMEAN_TIMING_STATS_CACHE_SIZEobjects borrowed from the pool.longgetMinEvictableIdleTimeMillis()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 - seesetTimeBetweenEvictionRunsMillis(long)).abstract intgetNumIdle()The number of instances currently idle in this pool.intgetNumTestsPerEvictionRun()Returns the maximum number of objects to examine during each run (if any) of the idle object evictor thread.longgetReturnedCount()The total number of objects returned to this pool over the lifetime of the pool.longgetSoftMinEvictableIdleTimeMillis()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 - seesetTimeBetweenEvictionRunsMillis(long)), with the extra condition that at leastminIdleobject instances remain in the pool.private java.lang.StringgetStackTrace(java.lang.Exception e)Gets the stack trace of an exception as a string.SwallowedExceptionListenergetSwallowedExceptionListener()The listener used (if any) to receive notifications of exceptions unavoidably swallowed by the pool.booleangetTestOnBorrow()Returns whether objects borrowed from the pool will be validated before being returned from theborrowObject()method.booleangetTestOnCreate()Returns whether objects created for the pool will be validated before being returned from theborrowObject()method.booleangetTestOnReturn()Returns whether objects borrowed from the pool will be validated when they are returned to the pool via thereturnObject()method.booleangetTestWhileIdle()Returns whether objects sitting idle in the pool will be validated by the idle object evictor (if any - seesetTimeBetweenEvictionRunsMillis(long)).longgetTimeBetweenEvictionRunsMillis()Returns the number of milliseconds to sleep between runs of the idle object evictor thread.booleanisClosed()Has this pool instance been closed.private javax.management.ObjectNamejmxRegister(BaseObjectPoolConfig<T> config, java.lang.String jmxNameBase, java.lang.String jmxNamePrefix)Registers the pool with the platform MBean server.(package private) voidjmxUnregister()Unregisters this pool's MBean.protected voidmarkReturningState(PooledObject<T> pooledObject)Marks the object as returning to the pool.voidsetBlockWhenExhausted(boolean blockWhenExhausted)Sets whether to block when theborrowObject()method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).protected voidsetConfig(BaseObjectPoolConfig<T> config)Initializes the receiver with the given configuration.private voidsetEvictionPolicy(java.lang.String className, java.lang.ClassLoader classLoader)Sets the eviction policy.voidsetEvictionPolicy(EvictionPolicy<T> evictionPolicy)Sets the eviction policy for this pool.voidsetEvictionPolicyClassName(java.lang.String evictionPolicyClassName)Sets the name of theEvictionPolicyimplementation that is used by this pool.voidsetEvictionPolicyClassName(java.lang.String evictionPolicyClassName, java.lang.ClassLoader classLoader)Sets the name of theEvictionPolicyimplementation that is used by this pool.voidsetEvictorShutdownTimeoutMillis(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.voidsetLifo(boolean 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.voidsetMaxTotal(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.voidsetMaxWaitMillis(long maxWaitMillis)Sets the maximum amount of time (in milliseconds) theborrowObject()method should block before throwing an exception when the pool is exhausted andgetBlockWhenExhausted()is true.voidsetMinEvictableIdleTimeMillis(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 - seesetTimeBetweenEvictionRunsMillis(long)).voidsetNumTestsPerEvictionRun(int numTestsPerEvictionRun)Sets the maximum number of objects to examine during each run (if any) of the idle object evictor thread.voidsetSoftMinEvictableIdleTimeMillis(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 - seesetTimeBetweenEvictionRunsMillis(long)), with the extra condition that at leastminIdleobject instances remain in the pool.voidsetSwallowedExceptionListener(SwallowedExceptionListener swallowedExceptionListener)The listener used (if any) to receive notifications of exceptions unavoidably swallowed by the pool.voidsetTestOnBorrow(boolean testOnBorrow)Sets whether objects borrowed from the pool will be validated before being returned from theborrowObject()method.voidsetTestOnCreate(boolean testOnCreate)Sets whether objects created for the pool will be validated before being returned from theborrowObject()method.voidsetTestOnReturn(boolean testOnReturn)Sets whether objects borrowed from the pool will be validated when they are returned to the pool via thereturnObject()method.voidsetTestWhileIdle(boolean testWhileIdle)Returns whether objects sitting idle in the pool will be validated by the idle object evictor (if any - seesetTimeBetweenEvictionRunsMillis(long)).voidsetTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)Sets the number of milliseconds to sleep between runs of the idle object evictor thread.(package private) voidstartEvictor(long delay)Starts the evictor with the given delay.(package private) voidstopEvictor()Stops the evictor.(package private) voidswallowException(java.lang.Exception swallowException)Swallows an exception and notifies the configured listener for swallowed exceptions queue.protected voidtoStringAppendFields(java.lang.StringBuilder builder)Used by sub-classes to include the fields defined by the sub-class in theBaseObject.toString()output.(package private) voidupdateStatsBorrow(PooledObject<T> p, long waitTime)Updates statistics after an object is borrowed from the pool.(package private) voidupdateStatsReturn(long activeTime)Updates statistics after an object is returned to the pool.-
Methods inherited from class org.datanucleus.store.rdbms.datasource.dbcp2.pool2.BaseObject
toString
-
-
-
-
Field Detail
-
MEAN_TIMING_STATS_CACHE_SIZE
public static final 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.- See Also:
- Constant Field Values
-
EVICTION_POLICY_TYPE_NAME
private static final java.lang.String EVICTION_POLICY_TYPE_NAME
-
maxTotal
private volatile int maxTotal
-
blockWhenExhausted
private volatile boolean blockWhenExhausted
-
maxWaitMillis
private volatile long maxWaitMillis
-
lifo
private volatile boolean lifo
-
fairness
private final boolean fairness
-
testOnCreate
private volatile boolean testOnCreate
-
testOnBorrow
private volatile boolean testOnBorrow
-
testOnReturn
private volatile boolean testOnReturn
-
testWhileIdle
private volatile boolean testWhileIdle
-
timeBetweenEvictionRunsMillis
private volatile long timeBetweenEvictionRunsMillis
-
numTestsPerEvictionRun
private volatile int numTestsPerEvictionRun
-
minEvictableIdleTimeMillis
private volatile long minEvictableIdleTimeMillis
-
softMinEvictableIdleTimeMillis
private volatile long softMinEvictableIdleTimeMillis
-
evictionPolicy
private volatile EvictionPolicy<T> evictionPolicy
-
evictorShutdownTimeoutMillis
private volatile long evictorShutdownTimeoutMillis
-
closeLock
final java.lang.Object closeLock
-
closed
volatile boolean closed
-
evictionLock
final java.lang.Object evictionLock
-
evictor
private BaseGenericObjectPool.Evictor evictor
-
evictionIterator
BaseGenericObjectPool.EvictionIterator evictionIterator
-
factoryClassLoader
private final java.lang.ref.WeakReference<java.lang.ClassLoader> factoryClassLoader
-
objectName
private final javax.management.ObjectName objectName
-
creationStackTrace
private final java.lang.String creationStackTrace
-
borrowedCount
private final java.util.concurrent.atomic.AtomicLong borrowedCount
-
returnedCount
private final java.util.concurrent.atomic.AtomicLong returnedCount
-
createdCount
final java.util.concurrent.atomic.AtomicLong createdCount
-
destroyedCount
final java.util.concurrent.atomic.AtomicLong destroyedCount
-
destroyedByEvictorCount
final java.util.concurrent.atomic.AtomicLong destroyedByEvictorCount
-
destroyedByBorrowValidationCount
final java.util.concurrent.atomic.AtomicLong destroyedByBorrowValidationCount
-
activeTimes
private final BaseGenericObjectPool.StatsStore activeTimes
-
idleTimes
private final BaseGenericObjectPool.StatsStore idleTimes
-
waitTimes
private final BaseGenericObjectPool.StatsStore waitTimes
-
maxBorrowWaitTimeMillis
private final java.util.concurrent.atomic.AtomicLong maxBorrowWaitTimeMillis
-
swallowedExceptionListener
private volatile SwallowedExceptionListener swallowedExceptionListener
-
-
Constructor Detail
-
BaseGenericObjectPool
public BaseGenericObjectPool(BaseObjectPoolConfig<T> config, java.lang.String jmxNameBase, java.lang.String jmxNamePrefix)
Handles JMX registration (if required) and the initialization required for monitoring.- Parameters:
config- Pool configurationjmxNameBase- The default base JMX name for the new pool unless overridden by the configjmxNamePrefix- Prefix to be used for JMX name for the new pool
-
-
Method Detail
-
getMaxTotal
public final 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.- Returns:
- the cap on the total number of object instances managed by the pool.
- See Also:
setMaxTotal(int)
-
setMaxTotal
public final 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.- Parameters:
maxTotal- The cap on the total number of object instances managed by the pool. Negative values mean that there is no limit to the number of objects allocated by the pool.- See Also:
getMaxTotal()
-
getBlockWhenExhausted
public final boolean getBlockWhenExhausted()
Returns whether to block when theborrowObject()method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).- Returns:
trueifborrowObject()should block when the pool is exhausted- See Also:
setBlockWhenExhausted(boolean)
-
setBlockWhenExhausted
public final void setBlockWhenExhausted(boolean blockWhenExhausted)
Sets whether to block when theborrowObject()method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).- Parameters:
blockWhenExhausted-trueifborrowObject()should block when the pool is exhausted- See Also:
getBlockWhenExhausted()
-
setConfig
protected void setConfig(BaseObjectPoolConfig<T> config)
Initializes the receiver with the given configuration.- Parameters:
config- Initialization source.
-
getMaxWaitMillis
public final long getMaxWaitMillis()
Returns the maximum amount of time (in milliseconds) theborrowObject()method should block before throwing an exception when the pool is exhausted andgetBlockWhenExhausted()is true. When less than 0, theborrowObject()method may block indefinitely.- Returns:
- the maximum number of milliseconds
borrowObject()will block. - See Also:
setMaxWaitMillis(long),setBlockWhenExhausted(boolean)
-
setMaxWaitMillis
public final void setMaxWaitMillis(long maxWaitMillis)
Sets the maximum amount of time (in milliseconds) theborrowObject()method should block before throwing an exception when the pool is exhausted andgetBlockWhenExhausted()is true. When less than 0, theborrowObject()method may block indefinitely.- Parameters:
maxWaitMillis- the maximum number of millisecondsborrowObject()will block or negative for indefinitely.- See Also:
getMaxWaitMillis(),setBlockWhenExhausted(boolean)
-
getLifo
public final boolean 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.- Returns:
trueif the pool is configured with LIFO behaviour orfalseif the pool is configured with FIFO behaviour- See Also:
setLifo(boolean)
-
getFairness
public final boolean 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.- Returns:
trueif waiting threads are to be served by the pool in arrival order
-
setLifo
public final void setLifo(boolean 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.- Parameters:
lifo-trueif the pool is to be configured with LIFO behaviour orfalseif the pool is to be configured with FIFO behaviour- See Also:
getLifo()
-
getTestOnCreate
public final boolean getTestOnCreate()
Returns whether objects created for the pool will be validated before being returned from theborrowObject()method. Validation is performed by thevalidateObject()method of the factory associated with the pool. If the object fails to validate, thenborrowObject()will fail.- Returns:
trueif newly created objects are validated before being returned from theborrowObject()method- Since:
- 2.2
- See Also:
setTestOnCreate(boolean)
-
setTestOnCreate
public final void setTestOnCreate(boolean testOnCreate)
Sets whether objects created for the pool will be validated before being returned from theborrowObject()method. Validation is performed by thevalidateObject()method of the factory associated with the pool. If the object fails to validate, thenborrowObject()will fail.- Parameters:
testOnCreate-trueif newly created objects should be validated before being returned from theborrowObject()method- Since:
- 2.2
- See Also:
getTestOnCreate()
-
getTestOnBorrow
public final boolean getTestOnBorrow()
Returns whether objects borrowed from the pool will be validated before being returned from theborrowObject()method. Validation is performed by thevalidateObject()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.- Returns:
trueif objects are validated before being returned from theborrowObject()method- See Also:
setTestOnBorrow(boolean)
-
setTestOnBorrow
public final void setTestOnBorrow(boolean testOnBorrow)
Sets whether objects borrowed from the pool will be validated before being returned from theborrowObject()method. Validation is performed by thevalidateObject()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.- Parameters:
testOnBorrow-trueif objects should be validated before being returned from theborrowObject()method- See Also:
getTestOnBorrow()
-
getTestOnReturn
public final boolean getTestOnReturn()
Returns whether objects borrowed from the pool will be validated when they are returned to the pool via thereturnObject()method. Validation is performed by thevalidateObject()method of the factory associated with the pool. Returning objects that fail validation are destroyed rather then being returned the pool.- Returns:
trueif objects are validated on return to the pool via thereturnObject()method- See Also:
setTestOnReturn(boolean)
-
setTestOnReturn
public final void setTestOnReturn(boolean testOnReturn)
Sets whether objects borrowed from the pool will be validated when they are returned to the pool via thereturnObject()method. Validation is performed by thevalidateObject()method of the factory associated with the pool. Returning objects that fail validation are destroyed rather then being returned the pool.- Parameters:
testOnReturn-trueif objects are validated on return to the pool via thereturnObject()method- See Also:
getTestOnReturn()
-
getTestWhileIdle
public final boolean getTestWhileIdle()
Returns whether objects sitting idle in the pool will be validated by the idle object evictor (if any - seesetTimeBetweenEvictionRunsMillis(long)). Validation is performed by thevalidateObject()method of the factory associated with the pool. If the object fails to validate, it will be removed from the pool and destroyed.- Returns:
trueif objects will be validated by the evictor- See Also:
setTestWhileIdle(boolean),setTimeBetweenEvictionRunsMillis(long)
-
setTestWhileIdle
public final void setTestWhileIdle(boolean testWhileIdle)
Returns whether objects sitting idle in the pool will be validated by the idle object evictor (if any - seesetTimeBetweenEvictionRunsMillis(long)). Validation is performed by thevalidateObject()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 settingtimeBetweenEvictionRunsMillisto a positive value.- Parameters:
testWhileIdle-trueso objects will be validated by the evictor- See Also:
getTestWhileIdle(),setTimeBetweenEvictionRunsMillis(long)
-
getTimeBetweenEvictionRunsMillis
public final 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.- Returns:
- number of milliseconds to sleep between evictor runs
- See Also:
setTimeBetweenEvictionRunsMillis(long)
-
setTimeBetweenEvictionRunsMillis
public final void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
Sets the number of milliseconds to sleep between runs of the idle object evictor thread.- When positive, the idle object evictor thread starts.
- When non-positive, no idle object evictor thread runs.
- Parameters:
timeBetweenEvictionRunsMillis- number of milliseconds to sleep between evictor runs- See Also:
getTimeBetweenEvictionRunsMillis()
-
getNumTestsPerEvictionRun
public final 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 beceil(which means that when the value isgetNumIdle()/ abs(getNumTestsPerEvictionRun()))-nroughly one nth of the idle objects will be tested per run.- Returns:
- max number of objects to examine during each evictor run
- See Also:
setNumTestsPerEvictionRun(int),setTimeBetweenEvictionRunsMillis(long)
-
setNumTestsPerEvictionRun
public final 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 beceil(which means that when the value isgetNumIdle()/ abs(getNumTestsPerEvictionRun()))-nroughly one nth of the idle objects will be tested per run.- Parameters:
numTestsPerEvictionRun- max number of objects to examine during each evictor run- See Also:
getNumTestsPerEvictionRun(),setTimeBetweenEvictionRunsMillis(long)
-
getMinEvictableIdleTimeMillis
public final 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 - seesetTimeBetweenEvictionRunsMillis(long)). When non-positive, no objects will be evicted from the pool due to idle time alone.- Returns:
- minimum amount of time an object may sit idle in the pool before it is eligible for eviction
- See Also:
setMinEvictableIdleTimeMillis(long),setTimeBetweenEvictionRunsMillis(long)
-
setMinEvictableIdleTimeMillis
public final 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 - seesetTimeBetweenEvictionRunsMillis(long)). When non-positive, no objects will be evicted from the pool due to idle time alone.- Parameters:
minEvictableIdleTimeMillis- minimum amount of time an object may sit idle in the pool before it is eligible for eviction- See Also:
getMinEvictableIdleTimeMillis(),setTimeBetweenEvictionRunsMillis(long)
-
getSoftMinEvictableIdleTimeMillis
public final 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 - seesetTimeBetweenEvictionRunsMillis(long)), with the extra condition that at leastminIdleobject instances remain in the pool. This setting is overridden bygetMinEvictableIdleTimeMillis()(that is, ifgetMinEvictableIdleTimeMillis()is positive, thengetSoftMinEvictableIdleTimeMillis()is ignored).- Returns:
- minimum amount of time an object may sit idle in the pool before it is eligible for eviction if minIdle instances are available
- See Also:
setSoftMinEvictableIdleTimeMillis(long)
-
setSoftMinEvictableIdleTimeMillis
public final 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 - seesetTimeBetweenEvictionRunsMillis(long)), with the extra condition that at leastminIdleobject instances remain in the pool. This setting is overridden bygetMinEvictableIdleTimeMillis()(that is, ifgetMinEvictableIdleTimeMillis()is positive, thengetSoftMinEvictableIdleTimeMillis()is ignored).- Parameters:
softMinEvictableIdleTimeMillis- minimum amount of time an object may sit idle in the pool before it is eligible for eviction if minIdle instances are available- See Also:
getSoftMinEvictableIdleTimeMillis()
-
getEvictionPolicyClassName
public final java.lang.String getEvictionPolicyClassName()
Returns the name of theEvictionPolicyimplementation that is used by this pool.- Returns:
- The fully qualified class name of the
EvictionPolicy - See Also:
setEvictionPolicyClassName(String)
-
setEvictionPolicy
public void setEvictionPolicy(EvictionPolicy<T> evictionPolicy)
Sets the eviction policy for this pool.- Parameters:
evictionPolicy- the eviction policy for this pool.- Since:
- 2.6.0
-
setEvictionPolicyClassName
public final void setEvictionPolicyClassName(java.lang.String evictionPolicyClassName, java.lang.ClassLoader classLoader)Sets the name of theEvictionPolicyimplementation that is used by this pool. The Pool will attempt to load the class using the given class loader. If that fails, use the class loader for theEvictionPolicyinterface.- Parameters:
evictionPolicyClassName- the fully qualified class name of the new eviction policyclassLoader- the class loader to load the givenevictionPolicyClassName.- Since:
- 2.6.0 If loading the class using the given class loader fails, use the class loader for the
EvictionPolicyinterface. - See Also:
getEvictionPolicyClassName()
-
setEvictionPolicy
private void setEvictionPolicy(java.lang.String className, java.lang.ClassLoader classLoader) throws java.lang.ClassNotFoundException, java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodExceptionSets the eviction policy.- Parameters:
className- Eviction policy class name.classLoader- Load the class from this class loader.- Throws:
java.lang.ClassNotFoundExceptionjava.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetExceptionjava.lang.NoSuchMethodException
-
setEvictionPolicyClassName
public final void setEvictionPolicyClassName(java.lang.String evictionPolicyClassName)
Sets the name of theEvictionPolicyimplementation that is used by this pool. The Pool will attempt to load the class using the thread context class loader. If that fails, the use the class loader for theEvictionPolicyinterface.- Parameters:
evictionPolicyClassName- the fully qualified class name of the new eviction policy- Since:
- 2.6.0 If loading the class using the thread context class loader fails, use the class loader for the
EvictionPolicyinterface. - See Also:
getEvictionPolicyClassName()
-
getEvictorShutdownTimeoutMillis
public final 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.- Returns:
- The timeout in milliseconds that will be used while waiting for the Evictor to shut down.
-
setEvictorShutdownTimeoutMillis
public final 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.- Parameters:
evictorShutdownTimeoutMillis- the timeout in milliseconds that will be used while waiting for the Evictor to shut down.
-
close
public abstract void close()
Closes the pool, destroys the remaining idle objects and, if registered in JMX, deregisters it.
-
isClosed
public final boolean isClosed()
Has this pool instance been closed.- Returns:
truewhen this pool has been closed.
-
evict
public abstract void evict() throws java.lang.ExceptionPerform
numTestsidle object eviction tests, evicting examined objects that meet the criteria for eviction. IftestWhileIdleis true, examined objects are validated when visited (and removed if invalid); otherwise only objects that have been idle for more thanminEvicableIdleTimeMillisare removed.- Throws:
java.lang.Exception- when there is a problem evicting idle objects.
-
getEvictionPolicy
public EvictionPolicy<T> getEvictionPolicy()
Returns theEvictionPolicydefined for this pool.- Returns:
- the eviction policy
- Since:
- 2.4, 2.6.0 Changed access from protected to public.
-
assertOpen
final void assertOpen() throws java.lang.IllegalStateExceptionVerifies that the pool is open.- Throws:
java.lang.IllegalStateException- if the pool is closed.
-
startEvictor
final void startEvictor(long delay)
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.
This method needs to be final, since it is called from a constructor. See POOL-195.
- Parameters:
delay- time in milliseconds before start and between eviction runs
-
stopEvictor
void stopEvictor()
Stops the evictor.
-
ensureMinIdle
abstract void ensureMinIdle() throws java.lang.ExceptionTries to ensure that the configured minimum number of idle instances are available in the pool.- Throws:
java.lang.Exception- if an error occurs creating idle instances
-
getJmxName
public final javax.management.ObjectName getJmxName()
Provides the name under which the pool has been registered with the platform MBean server ornullif the pool has not been registered.- Returns:
- the JMX name
-
getCreationStackTrace
public final java.lang.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 theclose()method. This method is provided to assist with identifying code that creates but does not close it thereby creating a memory leak.- Returns:
- pool creation stack trace
-
getBorrowedCount
public final long getBorrowedCount()
The total number of objects successfully borrowed from this pool over the lifetime of the pool.- Returns:
- the borrowed object count
-
getReturnedCount
public final 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.- Returns:
- the returned object count
-
getCreatedCount
public final long getCreatedCount()
The total number of objects created for this pool over the lifetime of the pool.- Returns:
- the created object count
-
getDestroyedCount
public final long getDestroyedCount()
The total number of objects destroyed by this pool over the lifetime of the pool.- Returns:
- the destroyed object count
-
getDestroyedByEvictorCount
public final long getDestroyedByEvictorCount()
The total number of objects destroyed by the evictor associated with this pool over the lifetime of the pool.- Returns:
- the evictor destroyed object count
-
getDestroyedByBorrowValidationCount
public final long getDestroyedByBorrowValidationCount()
The total number of objects destroyed by this pool as a result of failing validation duringborrowObject()over the lifetime of the pool.- Returns:
- validation destroyed object count
-
getMeanActiveTimeMillis
public final long getMeanActiveTimeMillis()
The mean time objects are active for based on the lastMEAN_TIMING_STATS_CACHE_SIZEobjects returned to the pool.- Returns:
- mean time an object has been checked out from the pool among recently returned objects
-
getMeanIdleTimeMillis
public final long getMeanIdleTimeMillis()
The mean time objects are idle for based on the lastMEAN_TIMING_STATS_CACHE_SIZEobjects borrowed from the pool.- Returns:
- mean time an object has been idle in the pool among recently borrowed objects
-
getMeanBorrowWaitTimeMillis
public final long getMeanBorrowWaitTimeMillis()
The mean time threads wait to borrow an object based on the lastMEAN_TIMING_STATS_CACHE_SIZEobjects borrowed from the pool.- Returns:
- mean time in milliseconds that a recently served thread has had to wait to borrow an object from the pool
-
getMaxBorrowWaitTimeMillis
public final long getMaxBorrowWaitTimeMillis()
The maximum time a thread has waited to borrow objects from the pool.- Returns:
- maximum wait time in milliseconds since the pool was created
-
getNumIdle
public abstract int getNumIdle()
The number of instances currently idle in this pool.- Returns:
- count of instances available for checkout from the pool
-
getSwallowedExceptionListener
public final SwallowedExceptionListener getSwallowedExceptionListener()
The listener used (if any) to receive notifications of exceptions unavoidably swallowed by the pool.- Returns:
- The listener or
nullfor no listener
-
setSwallowedExceptionListener
public final void setSwallowedExceptionListener(SwallowedExceptionListener swallowedExceptionListener)
The listener used (if any) to receive notifications of exceptions unavoidably swallowed by the pool.- Parameters:
swallowedExceptionListener- The listener ornullfor no listener
-
swallowException
final void swallowException(java.lang.Exception swallowException)
Swallows an exception and notifies the configured listener for swallowed exceptions queue.- Parameters:
swallowException- exception to be swallowed
-
updateStatsBorrow
final void updateStatsBorrow(PooledObject<T> p, long waitTime)
Updates statistics after an object is borrowed from the pool.- Parameters:
p- object borrowed from the poolwaitTime- time (in milliseconds) that the borrowing thread had to wait
-
updateStatsReturn
final void updateStatsReturn(long activeTime)
Updates statistics after an object is returned to the pool.- Parameters:
activeTime- the amount of time (in milliseconds) that the returning object was checked out
-
markReturningState
protected void markReturningState(PooledObject<T> pooledObject)
Marks the object as returning to the pool.- Parameters:
pooledObject- instance to return to the keyed pool
-
jmxUnregister
final void jmxUnregister()
Unregisters this pool's MBean.
-
jmxRegister
private javax.management.ObjectName jmxRegister(BaseObjectPoolConfig<T> config, java.lang.String jmxNameBase, java.lang.String jmxNamePrefix)
Registers the pool with the platform MBean server. The registered name will bejmxNameBase + jmxNamePrefix + iwhere i is the least integer greater than or equal to 1 such that the name is not already registered. Swallows MBeanRegistrationException, NotCompliantMBeanException returning null.- Parameters:
config- Pool configurationjmxNameBase- default base JMX name for this pooljmxNamePrefix- name prefix- Returns:
- registered ObjectName, null if registration fails
-
getStackTrace
private java.lang.String getStackTrace(java.lang.Exception e)
Gets the stack trace of an exception as a string.- Parameters:
e- exception to trace- Returns:
- exception stack trace as a string
-
toStringAppendFields
protected void toStringAppendFields(java.lang.StringBuilder builder)
Description copied from class:BaseObjectUsed by sub-classes to include the fields defined by the sub-class in theBaseObject.toString()output.- Overrides:
toStringAppendFieldsin classBaseObject- Parameters:
builder- Field names and values are appended to this object
-
-