What is Apache Commons Pool close() behaviour
Asked Answered
F

2

8

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?

Falsify answered 17/12, 2009 at 15:35 Comment(1)
which close() are you referring to?Bennett
F
4

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.

Favors answered 23/7, 2017 at 16:58 Comment(1)
For pools that have had close() called on them, AND an object is borrowed when pool.close() is called AND a duration has passed (that otherwise would have invoked the "Abandoned" configuration)... will said "Abandoned" calls kick in?Highroad
S
3

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?

Sailplane answered 17/12, 2009 at 15:43 Comment(3)
At the point my application is shutdown (possibly by a sysadmin), pooled objects might be in use. However, in order to release resources allocated to those objects, I'll still need to close them before the application exits. Ideally, a call to close() on the pool would work like this: 1) Prevent any new objects being borrowed 2) Close objects idle in the pool 3) Close borrowed objects as they are returned to pool, blocking until all objects have been returned This doesn't seem to be an unusual requirement.Falsify
Apache handles (1) and (2). To address issue (3), consider subclassing the pooling class and handle close() yourself. I'd guess that clearing/draining the pool is not in the Apache implementation because it's tricky to handle the general case due to threading and timeout issues.Sailplane
@drewzilla: (3a) Apache Commons Pool destoys the borrowed objects on return if the pool was closed, it's in the javadoc now. (3b) #13356252Sacaton

© 2022 - 2024 — McMap. All rights reserved.