Package org.apache.commons.pool
Class PoolUtils.SynchronizedKeyedPoolableObjectFactory<K,V>
java.lang.Object
org.apache.commons.pool.PoolUtils.SynchronizedKeyedPoolableObjectFactory<K,V>
- All Implemented Interfaces:
KeyedPoolableObjectFactory<K,
V>
- Enclosing class:
- PoolUtils
private static class PoolUtils.SynchronizedKeyedPoolableObjectFactory<K,V>
extends Object
implements KeyedPoolableObjectFactory<K,V>
A fully synchronized KeyedPoolableObjectFactory that wraps a KeyedPoolableObjectFactory 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
FieldsModifier and TypeFieldDescriptionprivate final KeyedPoolableObjectFactory<K,
V> Wrapped factoryprivate final Object
Synchronization lock -
Constructor Summary
ConstructorsConstructorDescriptionSynchronizedKeyedPoolableObjectFactory
(KeyedPoolableObjectFactory<K, V> keyedFactory) Create a SynchronizedKeyedPoolableObjectFactory wrapping the given factory. -
Method Summary
Modifier and TypeMethodDescriptionvoid
activateObject
(K key, V obj) Reinitialize an instance to be returned by the pool.void
destroyObject
(K key, V obj) Destroy an instance no longer needed by the pool.makeObject
(K key) Create an instance that can be served by the pool.void
passivateObject
(K key, V obj) Uninitialize an instance to be returned to the idle object pool.toString()
boolean
validateObject
(K key, V obj) Ensures that the instance is safe to be returned by the pool.
-
Field Details
-
lock
Synchronization lock -
keyedFactory
Wrapped factory
-
-
Constructor Details
-
SynchronizedKeyedPoolableObjectFactory
SynchronizedKeyedPoolableObjectFactory(KeyedPoolableObjectFactory<K, V> keyedFactory) throws IllegalArgumentExceptionCreate a SynchronizedKeyedPoolableObjectFactory wrapping the given factory.- Parameters:
keyedFactory
- underlying factory to wrap- Throws:
IllegalArgumentException
- if the factory is null
-
-
Method Details
-
makeObject
Create an instance that can be served by the pool.- Specified by:
makeObject
in interfaceKeyedPoolableObjectFactory<K,
V> - Parameters:
key
- the key used when constructing the object- Returns:
- an instance that can be served by the pool.
- Throws:
Exception
- if there is a problem creating a new instance, this will be propagated to the code requesting an object.
-
destroyObject
Destroy 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 interfaceKeyedPoolableObjectFactory<K,
V> - Parameters:
key
- the key used when selecting the instanceobj
- the instance to be destroyed- Throws:
Exception
- should be avoided as it may be swallowed by the pool implementation.- See Also:
-
validateObject
Ensures that the instance is safe to be returned by the pool. Returnsfalse
ifobj
should be destroyed.- Specified by:
validateObject
in interfaceKeyedPoolableObjectFactory<K,
V> - Parameters:
key
- the key used when selecting the objectobj
- the instance to be validated- Returns:
false
ifobj
is not valid and should be dropped from the pool,true
otherwise.
-
activateObject
Reinitialize an instance to be returned by the pool.- Specified by:
activateObject
in interfaceKeyedPoolableObjectFactory<K,
V> - Parameters:
key
- the key used when selecting the objectobj
- the instance to be activated- Throws:
Exception
- if there is a problem activatingobj
, this exception may be swallowed by the pool.- See Also:
-
passivateObject
Uninitialize an instance to be returned to the idle object pool.- Specified by:
passivateObject
in interfaceKeyedPoolableObjectFactory<K,
V> - Parameters:
key
- the key used when selecting the objectobj
- the instance to be passivated- Throws:
Exception
- if there is a problem passivatingobj
, this exception may be swallowed by the pool.- See Also:
-
toString
-