commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Phil Steitz <>
Subject Re: [commons-pool]: Usage of KeyedObjectPool
Date Fri, 02 Jan 2009 00:19:32 GMT wrote:
> Env:
>    JDK1.6
> I have a hierarchy of object types as in
>       (Animal
>             (mammal
>                    (dog)
>                    (cat)
>                    ...
>              )
>             (oviparous
>                   (..)
>       )
> Since a large number of such animals may need to be instantiated,
> I need to maintain a pool of "animals" of different species. I assume
> that the KeyedObjectPool is the right pool for such an application.
Yes, sounds reasonable.
> Questions:
>   a) Is it generally recommended that I subclass GenericKeyedObjectPool
>       (to, say, AnimalObjectPool) and pool different types of animal objects in there?
Not unless there are features missing that you need or you want to 
change behavior.  Look at the class javadoc for GenericKeyedObjectPool 
for a good description of how it works.  If I understand your objective 
correctly, you can use one GenericKeyedObject pool to manage a set of 
pools keyed on animal type.
>   b) If I do (a), should I also subclass BaseKeyedPoolableObjectFactory (say,
>       AnimalObjectFactory)?
That you do need to do, or more precisely you need to develop a class 
that implements the KeyedPoolableObjectFactory interface.   You then set 
your GenericKeyedObjectPool instance to use this factory to source and 
manage lifecycle events for the keyed objects (in your case animals) in 
your keyed object pool.  See the class javadoc for 
> Is there a sample to demonstrate the usage of KeyedObjectPool?
It is a little complicated, but commons dbcp provides a decent example 
as it uses a GenericKeyedObjectPool to manage pools of prepared 
statements.  The makeObject method of dbcp's PoolableConnectionFactory 
creates a GenericKeyedObjectPool.  The KeyedObjectPoolFactory it uses is 
provided by the PoolingConnection class.
>  Are there any
> common gotchas to watch out for?
The same gotchas that apply to any object pool.   Be careful with 
maxIdle and the various "abandoned object eviction" settings.  Unless 
you really need these things, stay away from the abandoned object 
settings and leave maxIdle at the default.


> Regards,
> /U
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message