Class SoftReferenceObjectPool<T>
- java.lang.Object
- 
- org.apache.tomcat.dbcp.pool2.BaseObject
- 
- org.apache.tomcat.dbcp.pool2.BaseObjectPool<T>
- 
- org.apache.tomcat.dbcp.pool2.impl.SoftReferenceObjectPool<T>
 
 
 
- 
- Type Parameters:
- T- Type of element pooled in this pool.
 - All Implemented Interfaces:
- java.io.Closeable,- java.lang.AutoCloseable,- ObjectPool<T>
 
 public class SoftReferenceObjectPool<T> extends BaseObjectPool<T> - Since:
- 2.0
 
- 
- 
Constructor SummaryConstructors Constructor Description SoftReferenceObjectPool(PooledObjectFactory<T> factory)Create aSoftReferenceObjectPoolwith the specified factory.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddObject()Creates an object, and places it into the pool. addObject() is useful for "pre-loading" a pool with idle objects.TborrowObject()Borrows an object from the pool.voidclear()Clears any objects sitting idle in the pool.voidclose()Closes this pool, and frees any resources associated with it.PooledObjectFactory<T>getFactory()Returns thePooledObjectFactoryused by this pool to create and manage object instances.intgetNumActive()Returns the number of instances currently borrowed from this pool.intgetNumIdle()Returns an approximation not less than the of the number of idle instances in the pool.voidinvalidateObject(T obj)Invalidates an object from the pool.voidinvalidateObject(T obj, DestroyMode mode)Invalidates an object from the pool, using the providedDestroyModevoidreturnObject(T obj)Returns an instance to the pool after successful validation and passivation.protected voidtoStringAppendFields(java.lang.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.BaseObjectPooladdObjects, assertOpen, isClosed
 - 
Methods inherited from class org.apache.tomcat.dbcp.pool2.BaseObjecttoString
 
- 
 
- 
- 
- 
Constructor Detail- 
SoftReferenceObjectPoolpublic SoftReferenceObjectPool(PooledObjectFactory<T> factory) Create aSoftReferenceObjectPoolwith the specified factory.- Parameters:
- factory- object factory to use.
 
 
- 
 - 
Method Detail- 
borrowObjectpublic T borrowObject() throws java.lang.Exception 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 activatedandvalidatedbefore being returned by this method. If validation fails or an exception occurs activating or validating an idle instance, the failing instance isdestroyedand 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'smakeObjectmethod is used to create a new instance. If the created instance either raises an exception on activation or fails validation,NoSuchElementExceptionis thrown. Exceptions thrown byMakeObjectare propagated to the caller; but other thanThreadDeathorVirtualMachineError, exceptions generated by activation, validation or destroy methods are swallowed silently.- Specified by:
- borrowObjectin interface- ObjectPool<T>
- Specified by:
- borrowObjectin class- BaseObjectPool<T>
- Returns:
- a valid, activated object instance
- Throws:
- java.util.NoSuchElementException- if a valid object cannot be provided
- java.lang.IllegalStateException- if invoked on a- closedpool
- java.lang.Exception- if an exception occurs creating a new instance
 
 - 
returnObjectpublic void returnObject(T obj) throws java.lang.Exception 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
- validationfails
- passivationthrows an exception
 - Specified by:
- returnObjectin interface- ObjectPool<T>
- Specified by:
- returnObjectin class- BaseObjectPool<T>
- Parameters:
- obj- instance to return to the pool
- Throws:
- java.lang.IllegalArgumentException- if obj is not currently part of this pool
- java.lang.IllegalStateException- if an attempt is made to return an object to the pool that is in any state other than allocated (i.e. borrowed). Attempting to return an object more than once or attempting to return an object that was never borrowed from the pool will trigger this exception.
- java.lang.Exception- if an instance cannot be returned to the pool
 
 - 
invalidateObjectpublic void invalidateObject(T obj) throws java.lang.Exception Invalidates an object from the pool.By contract, objmust 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:
- invalidateObjectin interface- ObjectPool<T>
- Specified by:
- invalidateObjectin class- BaseObjectPool<T>
- Parameters:
- obj- a- borrowedinstance to be disposed.
- Throws:
- java.lang.Exception- if the instance cannot be invalidated
 
 - 
invalidateObjectpublic void invalidateObject(T obj, DestroyMode mode) throws java.lang.Exception Description copied from interface:ObjectPoolInvalidates an object from the pool, using the providedDestroyModeBy contract, objmust 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. - Parameters:
- obj- a- borrowedinstance to be disposed.
- mode- destroy activation context provided to the factory
- Throws:
- java.lang.Exception- if the instance cannot be invalidated
 
 - 
addObjectpublic void addObject() throws java.lang.ExceptionCreates 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 validatedandpassivated. If validation fails, the new instance isdestroyed. Exceptions generated by the factorymakeObjectorpassivateare propagated to the caller. Exceptions destroying instances are silently swallowed.- Specified by:
- addObjectin interface- ObjectPool<T>
- Overrides:
- addObjectin class- BaseObjectPool<T>
- Throws:
- java.lang.IllegalStateException- if invoked on a- closedpool
- java.lang.Exception- when the- factoryhas a problem creating or passivating an object.
 
 - 
getNumIdlepublic int getNumIdle() Returns an approximation not less than the of the number of idle instances in the pool.- Specified by:
- getNumIdlein interface- ObjectPool<T>
- Overrides:
- getNumIdlein class- BaseObjectPool<T>
- Returns:
- estimated number of idle instances in the pool
 
 - 
getNumActivepublic int getNumActive() Returns the number of instances currently borrowed from this pool.- Specified by:
- getNumActivein interface- ObjectPool<T>
- Overrides:
- getNumActivein class- BaseObjectPool<T>
- Returns:
- the number of instances currently borrowed from this pool
 
 - 
clearpublic void clear() Clears any objects sitting idle in the pool.- Specified by:
- clearin interface- ObjectPool<T>
- Overrides:
- clearin class- BaseObjectPool<T>
 
 - 
closepublic 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:
- closein interface- java.lang.AutoCloseable
- Specified by:
- closein interface- java.io.Closeable
- Specified by:
- closein interface- ObjectPool<T>
- Overrides:
- closein class- BaseObjectPool<T>
 
 - 
getFactorypublic PooledObjectFactory<T> getFactory() Returns thePooledObjectFactoryused by this pool to create and manage object instances.- Returns:
- the factory
 
 - 
toStringAppendFieldsprotected 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 class- BaseObjectPool<T>
- Parameters:
- builder- Field names and values are appended to this object
 
 
- 
 
-