Class PoolUtils.SynchronizedPooledObjectFactory<T>
- java.lang.Object
-
- org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PoolUtils.SynchronizedPooledObjectFactory<T>
-
- Type Parameters:
T
- pooled object factory type
- All Implemented Interfaces:
PooledObjectFactory<T>
- Enclosing class:
- PoolUtils
private static final class PoolUtils.SynchronizedPooledObjectFactory<T> extends java.lang.Object implements PooledObjectFactory<T>
A fully synchronized PooledObjectFactory that wraps a PooledObjectFactory and synchronizes access to the wrapped factory methods.Note: This should not be used on pool implementations that already provide proper synchronization such as the pools provided in the Commons Pool library.
-
-
Field Summary
Fields Modifier and Type Field Description private PooledObjectFactory<T>
factory
Wrapped factoryprivate java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock
writeLock
Synchronization lock
-
Constructor Summary
Constructors Constructor Description SynchronizedPooledObjectFactory(PooledObjectFactory<T> factory)
Creates a SynchronizedPoolableObjectFactory wrapping the given factory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
activateObject(PooledObject<T> p)
Reinitializes an instance to be returned by the pool.void
destroyObject(PooledObject<T> p)
Destroys an instance no longer needed by the pool.PooledObject<T>
makeObject()
Creates an instance that can be served by the pool and wrap it in aPooledObject
to be managed by the pool.void
passivateObject(PooledObject<T> p)
Uninitializes an instance to be returned to the idle object pool.java.lang.String
toString()
boolean
validateObject(PooledObject<T> p)
Ensures that the instance is safe to be returned by the pool.
-
-
-
Field Detail
-
writeLock
private final java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock writeLock
Synchronization lock
-
factory
private final PooledObjectFactory<T> factory
Wrapped factory
-
-
Constructor Detail
-
SynchronizedPooledObjectFactory
SynchronizedPooledObjectFactory(PooledObjectFactory<T> factory) throws java.lang.IllegalArgumentException
Creates a SynchronizedPoolableObjectFactory wrapping the given factory.- Parameters:
factory
- underlying factory to wrap- Throws:
java.lang.IllegalArgumentException
- if the factory is null
-
-
Method Detail
-
makeObject
public PooledObject<T> makeObject() throws java.lang.Exception
Creates an instance that can be served by the pool and wrap it in aPooledObject
to be managed by the pool.- Specified by:
makeObject
in interfacePooledObjectFactory<T>
- Returns:
- a
PooledObject
wrapping an instance that can be served by the pool - Throws:
java.lang.Exception
- if there is a problem creating a new instance, this will be propagated to the code requesting an object.
-
destroyObject
public void destroyObject(PooledObject<T> p) throws java.lang.Exception
Destroys an instance no longer needed by the pool.It is important for implementations of this method to be aware that there is no guarantee about what state
obj
will be in and the implementation should be prepared to handle unexpected errors.Also, an implementation must take in to consideration that instances lost to the garbage collector may never be destroyed.
- Specified by:
destroyObject
in interfacePooledObjectFactory<T>
- Parameters:
p
- aPooledObject
wrapping the instance to be destroyed- Throws:
java.lang.Exception
- should be avoided as it may be swallowed by the pool implementation.- See Also:
PooledObjectFactory.validateObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<T>)
,ObjectPool.invalidateObject(T)
-
validateObject
public boolean validateObject(PooledObject<T> p)
Ensures that the instance is safe to be returned by the pool.- Specified by:
validateObject
in interfacePooledObjectFactory<T>
- Parameters:
p
- aPooledObject
wrapping the instance to be validated- Returns:
false
ifobj
is not valid and should be dropped from the pool,true
otherwise.
-
activateObject
public void activateObject(PooledObject<T> p) throws java.lang.Exception
Reinitializes an instance to be returned by the pool.- Specified by:
activateObject
in interfacePooledObjectFactory<T>
- Parameters:
p
- aPooledObject
wrapping the instance to be activated- Throws:
java.lang.Exception
- if there is a problem activatingobj
, this exception may be swallowed by the pool.- See Also:
PooledObjectFactory.destroyObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<T>)
-
passivateObject
public void passivateObject(PooledObject<T> p) throws java.lang.Exception
Uninitializes an instance to be returned to the idle object pool.- Specified by:
passivateObject
in interfacePooledObjectFactory<T>
- Parameters:
p
- aPooledObject
wrapping the instance to be passivated- Throws:
java.lang.Exception
- if there is a problem passivatingobj
, this exception may be swallowed by the pool.- See Also:
PooledObjectFactory.destroyObject(org.datanucleus.store.rdbms.datasource.dbcp2.pool2.PooledObject<T>)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-