What is Apache Commons Pool close() behaviour

0 votes
asked Dec 17, 2009 by dreweaster

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?

2 Answers

0 votes
answered Dec 17, 2009 by jdigital

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?

0 votes
answered Sep 15, 2017 by phil-steitz

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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter