Class SoftReferenceObjectPool<T>
- java.lang.Object
-
- org.datanucleus.store.rdbms.datasource.dbcp2.pool2.BaseObject
-
- org.datanucleus.store.rdbms.datasource.dbcp2.pool2.BaseObjectPool<T>
-
- org.datanucleus.store.rdbms.datasource.dbcp2.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
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.ArrayList<PooledSoftReference<T>>
allReferences
All references - checked out or waiting to be borrowed.private long
createCount
Total number of instances that have been createdprivate long
destroyCount
Total number of instances that have been destroyedprivate PooledObjectFactory<T>
factory
Factory to source pooled objectsprivate LinkedBlockingDeque<PooledSoftReference<T>>
idleReferences
Idle references - waiting to be borrowedprivate int
numActive
Count of instances that have been checkout out to pool clientsprivate java.lang.ref.ReferenceQueue<T>
refQueue
Queue of broken references that might be able to be removed from_pool
.
-
Constructor Summary
Constructors Constructor Description SoftReferenceObjectPool(PooledObjectFactory<T> factory)
Create aSoftReferenceObjectPool
with the specified factory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addObject()
Creates an object, and places it into the pool.T
borrowObject()
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.private void
destroy(PooledSoftReference<T> toDestroy)
Destroys aPooledSoftReference
and removes it from the idle and all references pools.private PooledSoftReference<T>
findReference(T obj)
Finds the PooledSoftReference in allReferences that points to obj.PooledObjectFactory<T>
getFactory()
Returns thePooledObjectFactory
used by this pool to create and manage object instances.int
getNumActive()
Returns the number of instances currently borrowed from this pool.int
getNumIdle()
Returns 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.private void
pruneClearedReferences()
If any idle objects were garbage collected, remove theirReference
wrappers from the idle object pool.private void
removeClearedReferences(java.util.Iterator<PooledSoftReference<T>> iterator)
Clears cleared references from iterator's collectionvoid
returnObject(T obj)
Returns an instance to the pool after successful validation and passivation.protected void
toStringAppendFields(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.datanucleus.store.rdbms.datasource.dbcp2.pool2.BaseObjectPool
assertOpen, isClosed
-
Methods inherited from class org.datanucleus.store.rdbms.datasource.dbcp2.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.datanucleus.store.rdbms.datasource.dbcp2.pool2.ObjectPool
addObjects
-
-
-
-
Field Detail
-
factory
private final PooledObjectFactory<T> factory
Factory to source pooled objects
-
refQueue
private final java.lang.ref.ReferenceQueue<T> refQueue
Queue of broken references that might be able to be removed from_pool
. This is used to helpgetNumIdle()
be more accurate with minimal performance overhead.
-
numActive
private int numActive
Count of instances that have been checkout out to pool clients
-
destroyCount
private long destroyCount
Total number of instances that have been destroyed
-
createCount
private long createCount
Total number of instances that have been created
-
idleReferences
private final LinkedBlockingDeque<PooledSoftReference<T>> idleReferences
Idle references - waiting to be borrowed
-
allReferences
private final java.util.ArrayList<PooledSoftReference<T>> allReferences
All references - checked out or waiting to be borrowed.
-
-
Constructor Detail
-
SoftReferenceObjectPool
public SoftReferenceObjectPool(PooledObjectFactory<T> factory)
Create aSoftReferenceObjectPool
with the specified factory.- Parameters:
factory
- object factory to use.
-
-
Method Detail
-
borrowObject
public 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
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:
java.util.NoSuchElementException
- if a valid object cannot be providedjava.lang.IllegalStateException
- if invoked on aclosed
pooljava.lang.Exception
- if an exception occurs creating a new instance
-
returnObject
public 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
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:
java.lang.IllegalArgumentException
- if obj is not currently part of this pooljava.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
-
invalidateObject
public void invalidateObject(T obj) throws java.lang.Exception
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:
java.lang.Exception
- if the instance cannot be invalidated
-
addObject
public void addObject() throws java.lang.Exception
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:
java.lang.IllegalStateException
- if invoked on aclosed
pooljava.lang.Exception
- when thefactory
has a problem creating or passivating an object.
-
getNumIdle
public int getNumIdle()
Returns 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
-
getNumActive
public int getNumActive()
Returns 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
-
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 interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceObjectPool<T>
- Overrides:
close
in classBaseObjectPool<T>
-
getFactory
public PooledObjectFactory<T> getFactory()
Returns thePooledObjectFactory
used by this pool to create and manage object instances.- Returns:
- the factory
-
pruneClearedReferences
private void pruneClearedReferences()
If any idle objects were garbage collected, remove theirReference
wrappers from the idle object pool.
-
findReference
private PooledSoftReference<T> findReference(T obj)
Finds the PooledSoftReference in allReferences that points to obj.- Parameters:
obj
- returning object- Returns:
- PooledSoftReference wrapping a soft reference to obj
-
destroy
private void destroy(PooledSoftReference<T> toDestroy) throws java.lang.Exception
Destroys aPooledSoftReference
and removes it from the idle and all references pools.- Parameters:
toDestroy
- PooledSoftReference to destroy- Throws:
java.lang.Exception
- If an error occurs while trying to destroy the object
-
removeClearedReferences
private void removeClearedReferences(java.util.Iterator<PooledSoftReference<T>> iterator)
Clears cleared references from iterator's collection- Parameters:
iterator
- iterator over idle/allReferences
-
toStringAppendFields
protected void toStringAppendFields(java.lang.StringBuilder builder)
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
-
-