I've been looking to implement pooling in part of my application. I want to use the Commons Pool library but am slightly concerned about how the close()
behaviour works. From looking at the javadocs and source code, it doesn't seem clear whether objects created in the pool will be destroyed when the close()
method is called. From what I can see, only objects that are idle in the pool will be destroyed - any that are being used, and yet to be returned, will not be touched.
Have I missed something here? I want to be sure that all objects are destroyed correctly when the pool is closed.
Anyone used this before and have an idea about how it works?
A common-pool resource is a hybrid between a public and private good in that is shared (non-rivalrous) but also scarce, having a finite supply. Common-pool resources are subject to the tragedy of the commons, where everybody acting for their own benefit actually over-consumes the resource, depleting it for all.
public class GenericObjectPoolConfig extends BaseObjectPoolConfig. A simple "struct" encapsulating the configuration for a GenericObjectPool . This class is not thread-safe; it is only intended to be used to provide attributes used when creating a pool.
A GenericObjectPool provides a number of configurable parameters: maxActive controls the maximum number of objects that can be borrowed from the pool at one time.
An object pool is a collection of a particular object that an application will create and keep on hand for those situations where creating each instance is expensive. A good example would be a database connection or a worker thread. The pool checks instances in and out for users like books out of a library.
As stated in the javadoc for the close
method in Commons Pool 2, when this method is invoked, idle instances are destroyed, but instances checked out to clients are not affected. After close
, borrowObject
will fail with IllegalStateException
, but returnObject
will succeed, with the returning instance destroyed on return. So if your clients can be counted on to return objects once you close the pool, instances will get cleaned up. If you want to block until this is done, watch numActive
. close
also deregisters the pool from jmx, so use getNumActive
directly in this case.
Generally speaking (regardless of the pooling library), it is not safe to destroy an object that is in use. Doing so will most likely result in an exception. If you want to guarantee a clean close, then you'll want to ensure that all objects have been returned to the pool.
Is there a reason you're doing a close before all the objects have been returned to the pool?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With