Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Apache Commons Pool close() behaviour

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?

like image 617
DrewEaster Avatar asked Dec 17 '09 15:12

DrewEaster


People also ask

What is Commons pool?

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.

What is GenericObjectPoolConfig?

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.

What is Generic object 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.

What is object pool in Java?

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.


2 Answers

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.

like image 119
Phil Steitz Avatar answered Oct 20 '22 04:10

Phil Steitz


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?

like image 40
jdigital Avatar answered Oct 20 '22 05:10

jdigital