Class GenericObjectPool<T>
- Type Parameters:
T- the type of objects held in this pool
- All Implemented Interfaces:
ObjectPool<T>
ObjectPool implementation.
When coupled with the appropriate PoolableObjectFactory,
GenericObjectPool provides robust pooling functionality for
arbitrary objects.
A GenericObjectPool provides a number of configurable parameters:
-
maxActivecontrols 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 non-positive, there is no limit to the number of objects that can be managed by the pool at one time. WhenmaxActiveis reached, the pool is said to be exhausted. The default setting for this parameter is 8. -
maxIdlecontrols the maximum number of objects that can sit idle in the pool at any time. When negative, there is no limit to the number of objects that may be idle at one time. The default setting for this parameter is 8. -
whenExhaustedActionspecifies the behavior of theborrowObject()method when the pool is exhausted:-
When
whenExhaustedActionisWHEN_EXHAUSTED_FAIL,borrowObject()will throw aNoSuchElementException -
When
whenExhaustedActionisWHEN_EXHAUSTED_GROW,borrowObject()will create a new object and return it (essentially makingmaxActivemeaningless.) -
When
whenExhaustedActionisWHEN_EXHAUSTED_BLOCK,borrowObject()will block (invokeObject.wait()) until a new or idle object is available. If a positivemaxWaitvalue is supplied, thenborrowObject()will block for at most that many milliseconds, after which aNoSuchElementExceptionwill be thrown. IfmaxWaitis non-positive, theborrowObject()method will block indefinitely.
whenExhaustedActionsetting isWHEN_EXHAUSTED_BLOCKand the defaultmaxWaitsetting is -1. By default, therefore,borrowObjectwill block indefinitely until an idle instance becomes available. -
When
-
When
testOnBorrowis set, the pool will attempt to validate each object before it is returned from theborrowObject()method. (Using the provided factory'sPoolableObjectFactory.validateObject(T)method.) Objects that fail to validate will be dropped from the pool, and a different object will be borrowed. The default setting for this parameter isfalse. -
When
testOnReturnis set, the pool will attempt to validate each object before it is returned to the pool in thereturnObject(T)method. (Using the provided factory'sPoolableObjectFactory.validateObject(T)method.) Objects that fail to validate will be dropped from the pool. The default setting for this parameter isfalse.
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 idle object eviction thread may be configured using the following attributes:
-
timeBetweenEvictionRunsMillisindicates how long the eviction thread should sleep before "runs" of examining idle objects. When non-positive, no eviction thread will be launched. The default setting for this parameter is -1 (i.e., idle object eviction is disabled by default). -
minEvictableIdleTimeMillisspecifies the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction due to idle time. When non-positive, no object will be dropped from the pool due to idle time alone. This setting has no effect unlesstimeBetweenEvictionRunsMillis > 0.The default setting for this parameter is 30 minutes. -
testWhileIdleindicates whether or not idle objects should be validated using the factory'sPoolableObjectFactory.validateObject(T)method. Objects that fail to validate will be dropped from the pool. This setting has no effect unlesstimeBetweenEvictionRunsMillis > 0.The default setting for this parameter isfalse. -
softMinEvictableIdleTimeMillisspecifies the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any), with the extra condition that at least "minIdle" object instances remain in the pool. When non-positive, no objects will be evicted from the pool due to idle time alone. This setting has no effect unlesstimeBetweenEvictionRunsMillis > 0.and it is superceded byminEvictableIdleTimeMillis(that is, ifminEvictableIdleTimeMillisis positive, thensoftMinEvictableIdleTimeMillisis ignored). The default setting for this parameter is -1 (disabled). -
numTestsPerEvictionRundetermines the number of objects examined in each run of the idle object evictor. This setting has no effect unlesstimeBetweenEvictionRunsMillis > 0.The default setting for this parameter is 3.
The pool can be configured to behave as a LIFO queue with respect to idle objects - always returning the most recently used object from the pool, or as a FIFO queue, where borrowObject always returns the oldest object in the idle object pool.
-
lifodetermines whether or not the pool returns idle objects in last-in-first-out order. The default setting for this parameter istrue.
GenericObjectPool is not usable without a PoolableObjectFactory. A
non-null factory must be provided either as a constructor argument
or via a call to setFactory(org.apache.commons.pool.PoolableObjectFactory<T>) before the pool is used.
Implementation note: To prevent possible deadlocks, care has been taken to ensure that no call to a factory method will occur within a synchronization block. See POOL-125 and DBCP-44 for more information.
- Since:
- Pool 1.0
- Version:
- $Revision: 1222396 $ $Date: 2011-12-22 14:02:25 -0500 (Thu, 22 Dec 2011) $
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA simple "struct" encapsulating the configuration information for aGenericObjectPool.private classThe idle object evictorTimerTask.private static final classLatch used to control allocation order of objects to threads to ensure fairness. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final LinkedList<GenericObjectPool.Latch<T>> Used to track the order in which threads callborrowObject()so that objects can be allocated in the order in which the threads requested them.Eviction cursor - keeps track of idle object evictor positionprivate GenericObjectPool<T>.EvictorMy idle object evictionTimerTask, if any.private PoolableObjectFactory<T> private booleanWhether or not the pool behaves as a LIFO queue (last in first out)private intThe cap on the total number of active instances from the pool.private intThe cap on the number of idle instances in the pool.private longThe maximum amount of time (in millis) theborrowObject()method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK.private longThe 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).private intThe cap on the minimum number of idle instances in the pool.private intThe number of objectsborrowObject()borrowed from the pool, but not yet returned.private intThe number of objects subject to some form of internal processing (usually creation or destruction) that should be included in the total number of objects but are neither active nor idle.private intThe max number of objects to examine during each run of the idle object evictor thread (if any).My pool.private longThe minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any), with the extra condition that at least "minIdle" amount of object remain in the pool.private booleanWhen true, objects will bevalidatedbefore being returned by theborrowObject()method.private booleanWhen true, objects will bevalidatedbefore being returned to the pool within thereturnObject(T).private booleanWhen true, objects will bevalidatedby the idle object evictor (if any).private longThe number of milliseconds to sleep between runs of the idle object evictor thread.private byteThe action to take when theborrowObject()method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).static final booleanThe default LIFO status.static final intThe default cap on the total number of active instances from the pool.static final intThe default cap on the number of "sleeping" instances in the pool.static final longThe default maximum amount of time (in milliseconds) theborrowObject()method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK.static final longThe default value forgetMinEvictableIdleTimeMillis().static final intThe default minimum number of "sleeping" instances in the pool before before the evictor thread (if active) spawns new objects.static final intThe default number of objects to examine per run in the idle object evictor.static final longThe default value forgetSoftMinEvictableIdleTimeMillis().static final booleanThe default "test on borrow" value.static final booleanThe default "test on return" value.static final booleanThe default "test while idle" value.static final longThe default "time between eviction runs" value.static final byteThe default "when exhausted action" for the pool.static final byteA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject()method should block until a new object is available, or themaximum wait timehas been reached.static final byteA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject()method should fail, throwing aNoSuchElementException.static final byteA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject()method should simply create a new object anyway. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new GenericObjectPool with default properties.GenericObjectPool(PoolableObjectFactory<T> factory) Create a new GenericObjectPool using the specified factory.GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive) Create a new GenericObjectPool using the specified values.GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait) Create a new GenericObjectPool using the specified values.GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) Create a new GenericObjectPool using the specified values.GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) Create a new GenericObjectPool using the specified values.GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) Create a new GenericObjectPool using the specified values.GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) Create a new GenericObjectPool using the specified values.GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) Create a new GenericObjectPool using the specified values.GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, long softMinEvictableIdleTimeMillis) Create a new GenericObjectPool using the specified values.GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, long softMinEvictableIdleTimeMillis, boolean lifo) Create a new GenericObjectPool using the specified values.GenericObjectPool(PoolableObjectFactory<T> factory, GenericObjectPool.Config config) Create a new GenericObjectPool using the specified values. -
Method Summary
Modifier and TypeMethodDescriptionvoidCreate an object, and place it into the pool.private voidaddObjectToPool(T obj, boolean decrementNumActive) Adds an object to the pool.private voidallocate()Allocate available instances to latches in the allocation queue.Borrows an object from the pool.private intcalculateDeficit(boolean incrementInternal) This returns the number of objects to create during the pool sustain cycle.voidclear()Clears any objects sitting idle in the pool by removing them from the idle instance pool and then invoking the configuredPoolableObjectFactory.destroyObject(Object)method on each idle instance.voidclose()Closes the pool.(package private) StringReturns pool info includinggetNumActive(),getNumIdle()and a list of objects idle in the pool with their idle times.private voiddestroy(Collection<GenericKeyedObjectPool.ObjectTimestampPair<T>> c, PoolableObjectFactory<T> factory) Private method to destroy all the objects in a collection using the supplied object factory.private voidCheck to see if we are below our minimum number of objects if so enough to bring us back to our minimum.voidevict()PerformnumTestsidle object eviction tests, evicting examined objects that meet the criteria for eviction.booleangetLifo()Whether or not the idle object pool acts as a LIFO queue.intReturns the maximum number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time.intReturns the cap on the number of "idle" instances in the pool.longReturns the maximum amount of time (in milliseconds) theborrowObject()method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK.longReturns 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).intReturns the minimum number of objects allowed in the pool before the evictor thread (if active) spawns new objects.intReturn the number of instances currently borrowed from this pool.intReturn the number of instances currently idle in this pool.private intReturns the number of tests to be performed in an Evictor run, based on the current value ofnumTestsPerEvictionRunand the number of idle instances in the pool.intReturns the max number of objects to examine during each run of the idle object evictor thread (if any).longReturns the minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any), with the extra condition that at least "minIdle" amount of object remain in the pool.booleanWhen true, objects will bevalidatedbefore being returned by theborrowObject()method.booleanWhen true, objects will bevalidatedbefore being returned to the pool within thereturnObject(T).booleanWhen true, objects will bevalidatedby the idle object evictor (if any).longReturns the number of milliseconds to sleep between runs of the idle object evictor thread.byteReturns the action to take when theborrowObject()method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).voidinvalidateObject(T obj) Invalidates an object from the pool.voidreturnObject(T obj) Returns an object instance to the pool.voidSets my configuration.voidsetFactory(PoolableObjectFactory<T> factory) Deprecated.to be removed in version 2.0voidsetLifo(boolean lifo) Sets the LIFO property of the pool.voidsetMaxActive(int maxActive) 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.voidsetMaxIdle(int maxIdle) Sets the cap on the number of "idle" instances in the pool.voidsetMaxWait(long maxWait) Sets the maximum amount of time (in milliseconds) theborrowObject()method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK.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).voidsetMinIdle(int minIdle) Sets the minimum number of objects allowed in the pool before the evictor thread (if active) spawns new objects.voidsetNumTestsPerEvictionRun(int numTestsPerEvictionRun) Sets the max number of objects to examine during each run of the idle object evictor thread (if any).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), with the extra condition that at least "minIdle" object instances remain in the pool.voidsetTestOnBorrow(boolean testOnBorrow) When true, objects will bevalidatedbefore being returned by theborrowObject()method.voidsetTestOnReturn(boolean testOnReturn) When true, objects will bevalidatedbefore being returned to the pool within thereturnObject(T).voidsetTestWhileIdle(boolean testWhileIdle) When true, objects will bevalidatedby the idle object evictor (if any).voidsetTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) Sets the number of milliseconds to sleep between runs of the idle object evictor thread.voidsetWhenExhaustedAction(byte whenExhaustedAction) Sets the action to take when theborrowObject()method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).protected voidstartEvictor(long delay) Start the eviction thread or service, or when delay is non-positive, stop it if it is already running.Methods inherited from class org.apache.commons.pool.BaseObjectPool
assertOpen, isClosed
-
Field Details
-
WHEN_EXHAUSTED_FAIL
public static final byte WHEN_EXHAUSTED_FAILA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject()method should fail, throwing aNoSuchElementException.- See Also:
-
WHEN_EXHAUSTED_BLOCK
public static final byte WHEN_EXHAUSTED_BLOCKA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject()method should block until a new object is available, or themaximum wait timehas been reached.- See Also:
-
WHEN_EXHAUSTED_GROW
public static final byte WHEN_EXHAUSTED_GROWA "when exhausted action" type indicating that when the pool is exhausted (i.e., the maximum number of active objects has been reached), theborrowObject()method should simply create a new object anyway.- See Also:
-
DEFAULT_MAX_IDLE
public static final int DEFAULT_MAX_IDLEThe default cap on the number of "sleeping" instances in the pool.- See Also:
-
DEFAULT_MIN_IDLE
public static final int DEFAULT_MIN_IDLEThe default minimum number of "sleeping" instances in the pool before before the evictor thread (if active) spawns new objects.- See Also:
-
DEFAULT_MAX_ACTIVE
public static final int DEFAULT_MAX_ACTIVEThe default cap on the total number of active instances from the pool.- See Also:
-
DEFAULT_WHEN_EXHAUSTED_ACTION
public static final byte DEFAULT_WHEN_EXHAUSTED_ACTIONThe default "when exhausted action" for the pool.- See Also:
-
DEFAULT_LIFO
public static final boolean DEFAULT_LIFOThe default LIFO status. True means that borrowObject returns the most recently used ("last in") idle object in the pool (if there are idle instances available). False means that the pool behaves as a FIFO queue - objects are taken from the idle object pool in the order that they are returned to the pool.- Since:
- 1.4
- See Also:
-
DEFAULT_MAX_WAIT
public static final long DEFAULT_MAX_WAITThe default maximum amount of time (in milliseconds) theborrowObject()method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK.- See Also:
-
DEFAULT_TEST_ON_BORROW
public static final boolean DEFAULT_TEST_ON_BORROWThe default "test on borrow" value.- See Also:
-
DEFAULT_TEST_ON_RETURN
public static final boolean DEFAULT_TEST_ON_RETURNThe default "test on return" value.- See Also:
-
DEFAULT_TEST_WHILE_IDLE
public static final boolean DEFAULT_TEST_WHILE_IDLEThe default "test while idle" value.- See Also:
-
DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS
public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLISThe default "time between eviction runs" value.- See Also:
-
DEFAULT_NUM_TESTS_PER_EVICTION_RUN
public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUNThe default number of objects to examine per run in the idle object evictor.- See Also:
-
DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS
public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLISThe default value forgetMinEvictableIdleTimeMillis().- See Also:
-
DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS
public static final long DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLISThe default value forgetSoftMinEvictableIdleTimeMillis().- See Also:
-
_maxIdle
private int _maxIdleThe cap on the number of idle instances in the pool.- See Also:
-
_minIdle
private int _minIdleThe cap on the minimum number of idle instances in the pool.- See Also:
-
_maxActive
private int _maxActiveThe cap on the total number of active instances from the pool.- See Also:
-
_maxWait
private long _maxWaitThe maximum amount of time (in millis) theborrowObject()method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK. When less than or equal to 0, theborrowObject()method may block indefinitely.- See Also:
-
_whenExhaustedAction
private byte _whenExhaustedActionThe action to take when theborrowObject()method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).- See Also:
-
_testOnBorrow
private volatile boolean _testOnBorrowWhen true, objects will bevalidatedbefore being returned by theborrowObject()method. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another.- See Also:
-
_testOnReturn
private volatile boolean _testOnReturnWhen true, objects will bevalidatedbefore being returned to the pool within thereturnObject(T).- See Also:
-
_testWhileIdle
private boolean _testWhileIdleWhen true, objects will bevalidatedby the idle object evictor (if any). If an object fails to validate, it will be dropped from the pool.- See Also:
-
_timeBetweenEvictionRunsMillis
private long _timeBetweenEvictionRunsMillisThe number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.- See Also:
-
_numTestsPerEvictionRun
private int _numTestsPerEvictionRunThe max number of objects to examine during each run of the idle object evictor thread (if any).When a negative value is supplied, ceil(
getNumIdle())/abs(getNumTestsPerEvictionRun()) tests will be run. I.e., when the value is -n, roughly one nth of the idle objects will be tested per run.- See Also:
-
_minEvictableIdleTimeMillis
private long _minEvictableIdleTimeMillisThe 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). When non-positive, no objects will be evicted from the pool due to idle time alone.- See Also:
-
_softMinEvictableIdleTimeMillis
private long _softMinEvictableIdleTimeMillisThe minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any), with the extra condition that at least "minIdle" amount of object remain in the pool. When non-positive, no objects will be evicted from the pool due to idle time alone.- See Also:
-
_lifo
private boolean _lifoWhether or not the pool behaves as a LIFO queue (last in first out) -
_pool
My pool. -
_evictionCursor
Eviction cursor - keeps track of idle object evictor position -
_factory
-
_numActive
private int _numActiveThe number of objectsborrowObject()borrowed from the pool, but not yet returned. -
_evictor
My idle object evictionTimerTask, if any. -
_numInternalProcessing
private int _numInternalProcessingThe number of objects subject to some form of internal processing (usually creation or destruction) that should be included in the total number of objects but are neither active nor idle. -
_allocationQueue
Used to track the order in which threads callborrowObject()so that objects can be allocated in the order in which the threads requested them.
-
-
Constructor Details
-
GenericObjectPool
public GenericObjectPool()Create a new GenericObjectPool with default properties. -
GenericObjectPool
Create a new GenericObjectPool using the specified factory.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objects
-
GenericObjectPool
Create a new GenericObjectPool using the specified values.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objectsconfig- a non-nullGenericObjectPool.Configdescribing my configuration
-
GenericObjectPool
Create a new GenericObjectPool using the specified values.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objectsmaxActive- the maximum number of objects that can be borrowed from me at one time (seesetMaxActive(int))
-
GenericObjectPool
public GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait) Create a new GenericObjectPool using the specified values.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objectsmaxActive- the maximum number of objects that can be borrowed from me at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seegetWhenExhaustedAction())maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted an and whenExhaustedAction isWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seegetMaxWait())
-
GenericObjectPool
public GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, boolean testOnBorrow, boolean testOnReturn) Create a new GenericObjectPool using the specified values.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objectsmaxActive- the maximum number of objects that can be borrowed at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seegetWhenExhaustedAction())maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted an and whenExhaustedAction isWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seegetMaxWait())testOnBorrow- whether or not to validate objects before they are returned by theborrowObject()method (seegetTestOnBorrow())testOnReturn- whether or not to validate objects after they are returned to thereturnObject(T)method (seegetTestOnReturn())
-
GenericObjectPool
public GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle) Create a new GenericObjectPool using the specified values.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objectsmaxActive- the maximum number of objects that can be borrowed at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seegetWhenExhaustedAction())maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted and whenExhaustedAction isWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seegetMaxWait())maxIdle- the maximum number of idle objects in my pool (seegetMaxIdle())
-
GenericObjectPool
public GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn) Create a new GenericObjectPool using the specified values.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objectsmaxActive- the maximum number of objects that can be borrowed at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seegetWhenExhaustedAction())maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted and whenExhaustedAction isWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seegetMaxWait())maxIdle- the maximum number of idle objects in my pool (seegetMaxIdle())testOnBorrow- whether or not to validate objects before they are returned by theborrowObject()method (seegetTestOnBorrow())testOnReturn- whether or not to validate objects after they are returned to thereturnObject(T)method (seegetTestOnReturn())
-
GenericObjectPool
public GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) Create a new GenericObjectPool using the specified values.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objectsmaxActive- the maximum number of objects that can be borrowed at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted and whenExhaustedAction isWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))maxIdle- the maximum number of idle objects in my pool (seesetMaxIdle(int))testOnBorrow- whether or not to validate objects before they are returned by theborrowObject()method (seesetTestOnBorrow(boolean))testOnReturn- whether or not to validate objects after they are returned to thereturnObject(T)method (seesetTestOnReturn(boolean))timeBetweenEvictionRunsMillis- the amount of time (in milliseconds) to sleep between examining idle objects for eviction (seesetTimeBetweenEvictionRunsMillis(long))numTestsPerEvictionRun- the number of idle objects to examine per run within the idle object eviction thread (if any) (seesetNumTestsPerEvictionRun(int))minEvictableIdleTimeMillis- the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction (seesetMinEvictableIdleTimeMillis(long))testWhileIdle- whether or not to validate objects in the idle object eviction thread, if any (seesetTestWhileIdle(boolean))
-
GenericObjectPool
public GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle) Create a new GenericObjectPool using the specified values.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objectsmaxActive- the maximum number of objects that can be borrowed at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted and whenExhaustedAction isWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))maxIdle- the maximum number of idle objects in my pool (seesetMaxIdle(int))minIdle- the minimum number of idle objects in my pool (seesetMinIdle(int))testOnBorrow- whether or not to validate objects before they are returned by theborrowObject()method (seesetTestOnBorrow(boolean))testOnReturn- whether or not to validate objects after they are returned to thereturnObject(T)method (seesetTestOnReturn(boolean))timeBetweenEvictionRunsMillis- the amount of time (in milliseconds) to sleep between examining idle objects for eviction (seesetTimeBetweenEvictionRunsMillis(long))numTestsPerEvictionRun- the number of idle objects to examine per run within the idle object eviction thread (if any) (seesetNumTestsPerEvictionRun(int))minEvictableIdleTimeMillis- the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction (seesetMinEvictableIdleTimeMillis(long))testWhileIdle- whether or not to validate objects in the idle object eviction thread, if any (seesetTestWhileIdle(boolean))
-
GenericObjectPool
public GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, long softMinEvictableIdleTimeMillis) Create a new GenericObjectPool using the specified values.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objectsmaxActive- the maximum number of objects that can be borrowed at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted and whenExhaustedAction isWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))maxIdle- the maximum number of idle objects in my pool (seesetMaxIdle(int))minIdle- the minimum number of idle objects in my pool (seesetMinIdle(int))testOnBorrow- whether or not to validate objects before they are returned by theborrowObject()method (seesetTestOnBorrow(boolean))testOnReturn- whether or not to validate objects after they are returned to thereturnObject(T)method (seesetTestOnReturn(boolean))timeBetweenEvictionRunsMillis- the amount of time (in milliseconds) to sleep between examining idle objects for eviction (seesetTimeBetweenEvictionRunsMillis(long))numTestsPerEvictionRun- the number of idle objects to examine per run within the idle object eviction thread (if any) (seesetNumTestsPerEvictionRun(int))minEvictableIdleTimeMillis- the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction (seesetMinEvictableIdleTimeMillis(long))testWhileIdle- whether or not to validate objects in the idle object eviction thread, if any (seesetTestWhileIdle(boolean))softMinEvictableIdleTimeMillis- the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction with the extra condition that at least "minIdle" amount of object remain in the pool. (seesetSoftMinEvictableIdleTimeMillis(long))- Since:
- Pool 1.3
-
GenericObjectPool
public GenericObjectPool(PoolableObjectFactory<T> factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle, long softMinEvictableIdleTimeMillis, boolean lifo) Create a new GenericObjectPool using the specified values.- Parameters:
factory- the (possibly null)PoolableObjectFactory to use to create, validate and destroy objectsmaxActive- the maximum number of objects that can be borrowed at one time (seesetMaxActive(int))whenExhaustedAction- the action to take when the pool is exhausted (seesetWhenExhaustedAction(byte))maxWait- the maximum amount of time to wait for an idle object when the pool is exhausted and whenExhaustedAction isWHEN_EXHAUSTED_BLOCK(otherwise ignored) (seesetMaxWait(long))maxIdle- the maximum number of idle objects in my pool (seesetMaxIdle(int))minIdle- the minimum number of idle objects in my pool (seesetMinIdle(int))testOnBorrow- whether or not to validate objects before they are returned by theborrowObject()method (seesetTestOnBorrow(boolean))testOnReturn- whether or not to validate objects after they are returned to thereturnObject(T)method (seesetTestOnReturn(boolean))timeBetweenEvictionRunsMillis- the amount of time (in milliseconds) to sleep between examining idle objects for eviction (seesetTimeBetweenEvictionRunsMillis(long))numTestsPerEvictionRun- the number of idle objects to examine per run within the idle object eviction thread (if any) (seesetNumTestsPerEvictionRun(int))minEvictableIdleTimeMillis- the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction (seesetMinEvictableIdleTimeMillis(long))testWhileIdle- whether or not to validate objects in the idle object eviction thread, if any (seesetTestWhileIdle(boolean))softMinEvictableIdleTimeMillis- the minimum number of milliseconds an object can sit idle in the pool before it is eligible for eviction with the extra condition that at least "minIdle" amount of object remain in the pool. (seesetSoftMinEvictableIdleTimeMillis(long))lifo- whether or not objects are returned in last-in-first-out order from the idle object pool (seesetLifo(boolean))- Since:
- Pool 1.4
-
-
Method Details
-
getMaxActive
public int getMaxActive()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 non-positive, 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:
-
setMaxActive
public void setMaxActive(int maxActive) 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:
maxActive- 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:
-
getWhenExhaustedAction
public byte getWhenExhaustedAction()Returns the action to take when theborrowObject()method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).- Returns:
- one of
WHEN_EXHAUSTED_BLOCK,WHEN_EXHAUSTED_FAILorWHEN_EXHAUSTED_GROW - See Also:
-
setWhenExhaustedAction
public void setWhenExhaustedAction(byte whenExhaustedAction) Sets the action to take when theborrowObject()method is invoked when the pool is exhausted (the maximum number of "active" objects has been reached).- Parameters:
whenExhaustedAction- the action code, which must be one ofWHEN_EXHAUSTED_BLOCK,WHEN_EXHAUSTED_FAIL, orWHEN_EXHAUSTED_GROW- See Also:
-
getMaxWait
public long getMaxWait()Returns the maximum amount of time (in milliseconds) theborrowObject()method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK. When less than or equal to 0, theborrowObject()method may block indefinitely.- Returns:
- maximum number of milliseconds to block when borrowing an object.
- See Also:
-
setMaxWait
public void setMaxWait(long maxWait) Sets the maximum amount of time (in milliseconds) theborrowObject()method should block before throwing an exception when the pool is exhausted and the"when exhausted" actionisWHEN_EXHAUSTED_BLOCK. When less than or equal to 0, theborrowObject()method may block indefinitely.- Parameters:
maxWait- maximum number of milliseconds to block when borrowing an object.- See Also:
-
getMaxIdle
public int getMaxIdle()Returns the cap on the number of "idle" instances in the pool.- Returns:
- the cap on the number of "idle" instances in the pool.
- See Also:
-
setMaxIdle
public void setMaxIdle(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 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- 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:
-
setMinIdle
public void setMinIdle(int minIdle) Sets the minimum number of objects allowed in the pool before the evictor thread (if active) spawns new objects. Note that no objects are created whennumActive + numIdle >= maxActive.This setting has no effect if the idle object evictor is disabled (i.e. iftimeBetweenEvictionRunsMillis invalid input: '<'= 0).- Parameters:
minIdle- The minimum number of objects.- See Also:
-
getMinIdle
public int getMinIdle()Returns the minimum number of objects allowed in the pool before the evictor thread (if active) spawns new objects. (Note no objects are created when: numActive + numIdle >= maxActive)- Returns:
- The minimum number of objects.
- See Also:
-
getTestOnBorrow
public boolean getTestOnBorrow()When true, objects will bevalidatedbefore being returned by theborrowObject()method. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another.- Returns:
trueif objects are validated before being borrowed.- See Also:
-
setTestOnBorrow
public void setTestOnBorrow(boolean testOnBorrow) When true, objects will bevalidatedbefore being returned by theborrowObject()method. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another.- Parameters:
testOnBorrow-trueif objects should be validated before being borrowed.- See Also:
-
getTestOnReturn
public boolean getTestOnReturn()When true, objects will bevalidatedbefore being returned to the pool within thereturnObject(T).- Returns:
truewhen objects will be validated after returned toreturnObject(T).- See Also:
-
setTestOnReturn
public void setTestOnReturn(boolean testOnReturn) When true, objects will bevalidatedbefore being returned to the pool within thereturnObject(T).- Parameters:
testOnReturn-trueso objects will be validated after returned toreturnObject(T).- See Also:
-
getTimeBetweenEvictionRunsMillis
public 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
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) Sets 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.- Parameters:
timeBetweenEvictionRunsMillis- number of milliseconds to sleep between evictor runs.- See Also:
-
getNumTestsPerEvictionRun
public int getNumTestsPerEvictionRun()Returns the max number of objects to examine during each run of the idle object evictor thread (if any).- Returns:
- max number of objects to examine during each evictor run.
- See Also:
-
setNumTestsPerEvictionRun
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) Sets the max number of objects to examine during each run of the idle object evictor thread (if any).When a negative value is supplied, ceil(
getNumIdle())/abs(getNumTestsPerEvictionRun()) tests will be run. That is, when the value is -n, roughly one nth of the idle objects will be tested per run. When the value is positive, the number of tests actually performed in each run will be the minimum of this value and the number of instances idle in the pool.- Parameters:
numTestsPerEvictionRun- max number of objects to examine during each evictor run.- See Also:
-
getMinEvictableIdleTimeMillis
public 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).- Returns:
- minimum amount of time an object may sit idle in the pool before it is eligible for eviction.
- See Also:
-
setMinEvictableIdleTimeMillis
public 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). 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:
-
getSoftMinEvictableIdleTimeMillis
public 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), with the extra condition that at least "minIdle" amount of object remain in the pool.- Returns:
- minimum amount of time an object may sit idle in the pool before it is eligible for eviction.
- Since:
- Pool 1.3
- See Also:
-
setSoftMinEvictableIdleTimeMillis
public 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), with the extra condition that at least "minIdle" object instances remain in the pool. When non-positive, no objects will be evicted from the pool due to idle time alone.- Parameters:
softMinEvictableIdleTimeMillis- minimum amount of time an object may sit idle in the pool before it is eligible for eviction.- Since:
- Pool 1.3
- See Also:
-
getTestWhileIdle
public boolean getTestWhileIdle()When true, objects will bevalidatedby the idle object evictor (if any). If an object fails to validate, it will be dropped from the pool.- Returns:
truewhen objects will be validated by the evictor.- See Also:
-
setTestWhileIdle
public void setTestWhileIdle(boolean testWhileIdle) When true, objects will bevalidatedby the idle object evictor (if any). If an object fails to validate, it will be dropped from the pool.- Parameters:
testWhileIdle-trueso objects will be validated by the evictor.- See Also:
-
getLifo
public boolean getLifo()Whether or not the idle object pool acts as a LIFO queue. True means that borrowObject returns the most recently used ("last in") idle object in the pool (if there are idle instances available). False means that the pool behaves as a FIFO queue - objects are taken from the idle object pool in the order that they are returned to the pool.- Returns:
true if the pool is configured to act as a LIFO queue- Since:
- 1.4
-
setLifo
public void setLifo(boolean lifo) Sets the LIFO property of the pool. True means that borrowObject returns the most recently used ("last in") idle object in the pool (if there are idle instances available). False means that the pool behaves as a FIFO queue - objects are taken from the idle object pool in the order that they are returned to the pool.- Parameters:
lifo- the new value for the LIFO property- Since:
- 1.4
-
setConfig
Sets my configuration.- Parameters:
conf- configuration to use.- See Also:
-
borrowObject
Borrows an object from the pool.
If there is an idle instance available in the pool, then either the most-recently returned (if
lifo== true) or "oldest" (lifo == false) instance sitting idle in the pool will be activated and returned. If activation fails, ortestOnBorrowis 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
maxActiveand (if applicable)whenExhaustedActionandmaxWaitproperties. If the number of instances checked out from the pool is less thanmaxActive,a new instance is created, activated and (if applicable) validated and returned to the caller.If the pool is exhausted (no available idle instances and no capacity to create new ones), this method will either block (
WHEN_EXHAUSTED_BLOCK), throw aNoSuchElementException(WHEN_EXHAUSTED_FAIL), or grow (WHEN_EXHAUSTED_GROW- ignoring maxActive). The length of time that this method will block whenwhenExhaustedAction == WHEN_EXHAUSTED_BLOCKis determined by themaxWaitproperty.When the pool is exhausted, multiple calling threads may be simultaneously blocked waiting for instances to become available. As of pool 1.5, a "fairness" algorithm has been implemented to ensure that threads receive available instances in request arrival order.
- Specified by:
borrowObjectin interfaceObjectPool<T>- Specified by:
borrowObjectin classBaseObjectPool<T>- Returns:
- object instance
- Throws:
NoSuchElementException- if an instance cannot be returnedException- if an instance cannot be obtained from the pool
-
allocate
private void allocate()Allocate available instances to latches in the allocation queue. Then set _mayCreate to true for as many additional latches remaining in queue as _maxActive allows. While it is safe for GOP, for consistency with GKOP this method should not be called from inside a sync block. -
invalidateObject
Invalidates an object from the pool.
By contract,
objmust have been obtained usingborrowObject.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.
- Specified by:
invalidateObjectin interfaceObjectPool<T>- Specified by:
invalidateObjectin classBaseObjectPool<T>- Parameters:
obj- aborrowedinstance to be disposed.- Throws:
Exception- if the configuredPoolableObjectFactorythrows an exception destroying obj
-
clear
public void clear()Clears any objects sitting idle in the pool by removing them from the idle instance pool and then invoking the configuredPoolableObjectFactory.destroyObject(Object)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. It locks the pool only during instance removal. Additional instances may be returned while removed items are being destroyed.
- Exceptions encountered destroying idle instances are swallowed.
- Specified by:
clearin interfaceObjectPool<T>- Overrides:
clearin classBaseObjectPool<T>
-
destroy
private void destroy(Collection<GenericKeyedObjectPool.ObjectTimestampPair<T>> c, PoolableObjectFactory<T> factory) Private method to destroy all the objects in a collection using the supplied object factory. Assumes that objects in the collection are instances of ObjectTimestampPair and that the object instances that they wrap were created by the factory.- Parameters:
c- Collection of objects to destroyfactory- PoolableConnectionFactory used to destroy the objects
-
getNumActive
public int getNumActive()Return the number of instances currently borrowed from this pool.- Specified by:
getNumActivein interfaceObjectPool<T>- Overrides:
getNumActivein classBaseObjectPool<T>- Returns:
- the number of instances currently borrowed from this pool
-
getNumIdle
public int getNumIdle()Return the number of instances currently idle in this pool.- Specified by:
getNumIdlein interfaceObjectPool<T>- Overrides:
getNumIdlein classBaseObjectPool<T>- Returns:
- the number of instances currently idle in this pool
-
returnObject
Returns an object instance to the pool.
If
maxIdleis 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.Note: There is no guard to prevent an object being returned to the pool multiple times. Clients are expected to discard references to returned objects and ensure that an object is not returned to the pool multiple times in sequence (i.e., without being borrowed again between returns). Violating this contract will result in the same object appearing multiple times in the pool and pool counters (numActive, numIdle) returning incorrect values.
- Specified by:
returnObjectin interfaceObjectPool<T>- Specified by:
returnObjectin classBaseObjectPool<T>- Parameters:
obj- instance to return to the pool- Throws:
Exception
-
addObjectToPool
Adds an object to the pool.
Validates the object if testOnReturn == true and passivates it before returning it to the pool. if validation or passivation fails, or maxIdle is set and there is no room in the pool, the instance is destroyed.
Calls
allocate()on successful completion- Parameters:
obj- instance to add to the pooldecrementNumActive- whether or not to decrement the active count- Throws:
Exception
-
close
Closes the pool. Once the pool is closed,
borrowObject()will fail with IllegalStateException, butreturnObject(Object)andinvalidateObject(Object)will continue to work, with returned objects destroyed on return.Destroys idle instances in the pool by invoking
clear().- Specified by:
closein interfaceObjectPool<T>- Overrides:
closein classBaseObjectPool<T>- Throws:
Exception
-
setFactory
Deprecated.to be removed in version 2.0Sets thefactorythis pool uses to create new instances. Trying to change thefactorywhile there are borrowed objects will throw anIllegalStateException. If there are instances idle in the pool when this method is invoked, these will be destroyed using the original factory.- Specified by:
setFactoryin interfaceObjectPool<T>- Overrides:
setFactoryin classBaseObjectPool<T>- Parameters:
factory- thePoolableObjectFactoryused to create new instances.- Throws:
IllegalStateException- when the factory cannot be set at this time
-
evict
Perform
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.Successive activations of this method examine objects in in sequence, cycling through objects in oldest-to-youngest order.
- Throws:
Exception- if the pool is closed or eviction fails.
-
ensureMinIdle
Check to see if we are below our minimum number of objects if so enough to bring us back to our minimum.- Throws:
Exception- whenaddObject()fails.
-
calculateDeficit
private int calculateDeficit(boolean incrementInternal) This returns the number of objects to create during the pool sustain cycle. This will ensure that the minimum number of idle instances is maintained without going past the maxActive value.- Parameters:
incrementInternal- - Should the count of objects currently under some form of internal processing be incremented?- Returns:
- The number of objects to be created
-
addObject
Create an object, and place it into the pool. addObject() is useful for "pre-loading" a pool with idle objects.- Specified by:
addObjectin interfaceObjectPool<T>- Overrides:
addObjectin classBaseObjectPool<T>- Throws:
Exception- whenPoolableObjectFactory.makeObject()fails.
-
startEvictor
protected void startEvictor(long delay) Start the eviction thread or service, or when delay is non-positive, stop it if it is already running.- Parameters:
delay- milliseconds between evictor runs.
-
debugInfo
String debugInfo()Returns pool info includinggetNumActive(),getNumIdle()and a list of objects idle in the pool with their idle times.- Returns:
- string containing debug information
-
getNumTests
private int getNumTests()Returns the number of tests to be performed in an Evictor run, based on the current value ofnumTestsPerEvictionRunand the number of idle instances in the pool.- Returns:
- the number of tests for the Evictor to run
- See Also:
-