Class SoftReferenceObjectPool<T>
- Type Parameters:
T
- Type of element pooled in this pool.
- All Implemented Interfaces:
Closeable
,AutoCloseable
,ObjectPool<T>
- Since:
- 2.0
-
Constructor Summary
ConstructorDescriptionSoftReferenceObjectPool
(PooledObjectFactory<T> factory) Constructs aSoftReferenceObjectPool
with the specified factory. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Creates an object, and places it into the pool. addObject() is useful for "pre-loading" a pool with idle objects.Borrows an object from the pool.void
clear()
Clears any objects sitting idle in the pool.void
close()
Closes this pool, and frees any resources associated with it.Gets thePooledObjectFactory
used by this pool to create and manage object instances.int
Gets the number of instances currently borrowed from this pool.int
Gets an approximation not less than the of the number of idle instances in the pool.void
invalidateObject
(T obj) Invalidates an object from the pool.void
returnObject
(T obj) Returns an instance to the pool after successful validation and passivation.protected void
toStringAppendFields
(StringBuilder builder) Used by sub-classes to include the fields defined by the sub-class in theBaseObject.toString()
output.Methods inherited from class org.apache.tomcat.dbcp.pool2.BaseObjectPool
assertOpen, isClosed
Methods inherited from class org.apache.tomcat.dbcp.pool2.BaseObject
toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.tomcat.dbcp.pool2.ObjectPool
addObjects, invalidateObject
-
Constructor Details
-
SoftReferenceObjectPool
Constructs aSoftReferenceObjectPool
with the specified factory.- Parameters:
factory
- object factory to use.
-
-
Method Details
-
addObject
Creates an object, and places it into the pool. addObject() is useful for "pre-loading" a pool with idle objects.Before being added to the pool, the newly created instance is
validated
andpassivated
. If validation fails, the new instance isdestroyed
. Exceptions generated by the factorymakeObject
orpassivate
are propagated to the caller. Exceptions destroying instances are silently swallowed.- Specified by:
addObject
in interfaceObjectPool<T>
- Overrides:
addObject
in classBaseObjectPool<T>
- Throws:
IllegalStateException
- if invoked on aclosed
poolException
- when thefactory
has a problem creating or passivating an object.
-
borrowObject
Borrows an object from the pool. If there are no idle instances available in the pool, the configured factory'sPooledObjectFactory.makeObject()
method is invoked to create a new instance.All instances are
activated
andvalidated
before being returned by this method. If validation fails or an exception occurs activating or validating an idle instance, the failing instance isdestroyed
and another instance is retrieved from the pool, validated and activated. This process continues until either the pool is empty or an instance passes validation. If the pool is empty on activation or it does not contain any valid instances, the factory'smakeObject
method is used to create a new instance. If the created instance either raises an exception on activation or fails validation,NoSuchElementException
is thrown. Exceptions thrown byMakeObject
are propagated to the caller; but other thanThreadDeath
orVirtualMachineError
, exceptions generated by activation, validation or destroy methods are swallowed silently.- Specified by:
borrowObject
in interfaceObjectPool<T>
- Specified by:
borrowObject
in classBaseObjectPool<T>
- Returns:
- a valid, activated object instance
- Throws:
NoSuchElementException
- if a valid object cannot be providedIllegalStateException
- if invoked on aclosed
poolException
- if an exception occurs creating a new instance
-
clear
public void clear()Clears any objects sitting idle in the pool.- Specified by:
clear
in interfaceObjectPool<T>
- Overrides:
clear
in classBaseObjectPool<T>
-
close
public void close()Closes this pool, and frees any resources associated with it. Invokesclear()
to destroy and remove instances in the pool.Calling
addObject()
orborrowObject()
after invoking this method on a pool will cause them to throw anIllegalStateException
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceObjectPool<T>
- Overrides:
close
in classBaseObjectPool<T>
-
getFactory
Gets thePooledObjectFactory
used by this pool to create and manage object instances.- Returns:
- the factory
-
getNumActive
public int getNumActive()Gets the number of instances currently borrowed from this pool.- Specified by:
getNumActive
in interfaceObjectPool<T>
- Overrides:
getNumActive
in classBaseObjectPool<T>
- Returns:
- the number of instances currently borrowed from this pool
-
getNumIdle
public int getNumIdle()Gets an approximation not less than the of the number of idle instances in the pool.- Specified by:
getNumIdle
in interfaceObjectPool<T>
- Overrides:
getNumIdle
in classBaseObjectPool<T>
- Returns:
- estimated number of idle instances in the pool
-
invalidateObject
Description copied from interface:ObjectPool
Invalidates an object from the pool.By contract,
obj
must have been obtained usingObjectPool.borrowObject()
or a related method as defined in an implementation or sub-interface.This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.
- Specified by:
invalidateObject
in interfaceObjectPool<T>
- Specified by:
invalidateObject
in classBaseObjectPool<T>
- Parameters:
obj
- aborrowed
instance to be disposed.- Throws:
Exception
- if the instance cannot be invalidated
-
returnObject
Returns an instance to the pool after successful validation and passivation. The returning instance is destroyed if any of the following are true:- the pool is closed
validation
failspassivation
throws an exception
- Specified by:
returnObject
in interfaceObjectPool<T>
- Specified by:
returnObject
in classBaseObjectPool<T>
- Parameters:
obj
- instance to return to the pool- Throws:
IllegalArgumentException
- if obj is not currently part of this poolException
- if an instance cannot be returned to the pool
-
toStringAppendFields
Description copied from class:BaseObject
Used by sub-classes to include the fields defined by the sub-class in theBaseObject.toString()
output.- Overrides:
toStringAppendFields
in classBaseObjectPool<T>
- Parameters:
builder
- Field names and values are appended to this object
-