Class PoolUtils.SynchronizedObjectPool<T>
- java.lang.Object
-
- org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PoolUtils.SynchronizedObjectPool<T>
-
- Type Parameters:
T
- type of objects in the pool
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,ObjectPool<T>
- Enclosing class:
- PoolUtils
private static final class PoolUtils.SynchronizedObjectPool<T> extends java.lang.Object implements ObjectPool<T>
A synchronized (thread-safe) ObjectPool backed by the specified ObjectPool.Note: This should not be used on pool implementations that already provide proper synchronization such as the pools provided in the Commons Pool library. Wrapping a pool that
waits
for poolable objects to be returned before allowing another one to be borrowed with another layer of synchronization will cause liveliness issues or a deadlock.
-
-
Field Summary
Fields Modifier and Type Field Description private ObjectPool<T>
pool
the underlying object poolprivate java.util.concurrent.locks.ReentrantReadWriteLock
readWriteLock
Object whose monitor is used to synchronize methods on the wrapped pool.
-
Constructor Summary
Constructors Constructor Description SynchronizedObjectPool(ObjectPool<T> pool)
Creates a new SynchronizedObjectPool wrapping the given pool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addObject()
Creates an object using thefactory
or other implementation dependent mechanism, passivate it, and then place it in the idle object pool.T
borrowObject()
Obtains an instance from this pool.void
clear()
Clears any objects sitting idle in the pool, releasing any associated resources (optional operation).void
close()
Closes this pool, and free any resources associated with it.int
getNumActive()
Returns the number of instances currently borrowed from this pool.int
getNumIdle()
Returns the number of instances currently idle in this pool.void
invalidateObject(T obj)
Invalidates an object from the pool.void
returnObject(T obj)
Returns an instance to the pool.java.lang.String
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
-
readWriteLock
private final java.util.concurrent.locks.ReentrantReadWriteLock readWriteLock
Object whose monitor is used to synchronize methods on the wrapped pool.
-
pool
private final ObjectPool<T> pool
the underlying object pool
-
-
Constructor Detail
-
SynchronizedObjectPool
SynchronizedObjectPool(ObjectPool<T> pool) throws java.lang.IllegalArgumentException
Creates a new SynchronizedObjectPool wrapping the given pool.- Parameters:
pool
- the ObjectPool to be "wrapped" in a synchronized ObjectPool.- Throws:
java.lang.IllegalArgumentException
- if the pool is null
-
-
Method Detail
-
borrowObject
public T borrowObject() throws java.lang.Exception, java.util.NoSuchElementException, java.lang.IllegalStateException
Obtains an instance from this pool.Instances returned from this method will have been either newly created with
PooledObjectFactory.makeObject()
or will be a previously idle object and have been activated withPooledObjectFactory.activateObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<T>)
and then validated withPooledObjectFactory.validateObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<T>)
.By contract, clients must return the borrowed instance using
ObjectPool.returnObject(T)
,ObjectPool.invalidateObject(T)
, or a related method as defined in an implementation or sub-interface.The behaviour of this method when the pool has been exhausted is not strictly specified (although it may be specified by implementations).
- Specified by:
borrowObject
in interfaceObjectPool<T>
- Returns:
- an instance from this pool.
- Throws:
java.lang.IllegalStateException
- afterclose
has been called on this pool.java.lang.Exception
- whenPooledObjectFactory.makeObject()
throws an exception.java.util.NoSuchElementException
- when the pool is exhausted and cannot or will not return another instance.
-
returnObject
public void returnObject(T obj)
Returns an instance to the pool. By contract,obj
must have been obtained usingObjectPool.borrowObject()
or a related method as defined in an implementation or sub-interface.- Specified by:
returnObject
in interfaceObjectPool<T>
- Parameters:
obj
- aborrowed
instance to be returned.
-
invalidateObject
public void invalidateObject(T obj)
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>
- Parameters:
obj
- aborrowed
instance to be disposed.
-
addObject
public void addObject() throws java.lang.Exception, java.lang.IllegalStateException, java.lang.UnsupportedOperationException
Creates an object using thefactory
or other implementation dependent mechanism, passivate it, and then place it in the idle object pool.addObject
is useful for "pre-loading" a pool with idle objects. (Optional operation).- Specified by:
addObject
in interfaceObjectPool<T>
- Throws:
java.lang.Exception
- whenPooledObjectFactory.makeObject()
fails.java.lang.IllegalStateException
- afterObjectPool.close()
has been called on this pool.java.lang.UnsupportedOperationException
- when this pool cannot add new idle objects.
-
getNumIdle
public int getNumIdle()
Returns the number of instances currently idle in this pool. This may be considered an approximation of the number of objects that can beborrowed
without creating any new instances. Returns a negative value if this information is not available.- Specified by:
getNumIdle
in interfaceObjectPool<T>
- Returns:
- the number of instances currently idle in this pool.
-
getNumActive
public int getNumActive()
Returns the number of instances currently borrowed from this pool. Returns a negative value if this information is not available.- Specified by:
getNumActive
in interfaceObjectPool<T>
- Returns:
- the number of instances currently borrowed from this pool.
-
clear
public void clear() throws java.lang.Exception, java.lang.UnsupportedOperationException
Clears any objects sitting idle in the pool, releasing any associated resources (optional operation). Idle objects cleared must bePooledObjectFactory.destroyObject(PooledObject)
.- Specified by:
clear
in interfaceObjectPool<T>
- Throws:
java.lang.UnsupportedOperationException
- if this implementation does not support the operationjava.lang.Exception
- if the pool cannot be cleared
-
close
public void close()
Closes this pool, and free any resources associated with it.Calling
ObjectPool.addObject()
orObjectPool.borrowObject()
after invoking this method on a pool will cause them to throw anIllegalStateException
.Implementations should silently fail if not all resources can be freed.
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceObjectPool<T>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-