views:

55

answers:

1

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: 

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?

jdigital
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 borrowed2) Close objects idle in the pool3) Close borrowed objects as they are returned to pool, blocking until all objects have been returnedThis doesn't seem to be an unusual requirement.
drewzilla
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.
jdigital