Apache Commons Pool close() 行为是什么
我一直在寻求在我的应用程序的一部分中实现池化。我想使用 Commons Pool 库,但有点担心 close()
行为的工作原理。从javadoc和源代码来看,似乎并不清楚当调用close()
方法时,在池中创建的对象是否会被销毁。据我所知,只有池中空闲的对象才会被销毁 - 任何正在使用但尚未返回的对象都不会被触及。
我在这里错过了什么吗?我想确保当池关闭时所有对象都被正确销毁。
有人以前用过这个并且知道它是如何工作的吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Commons Pool 2 中
close
方法的 javadoc 中所述,调用此方法时,空闲实例将被销毁,但签出到客户端的实例不受影响。close
后,borrowObject
将失败并出现IllegalStateException
,但returnObject
将成功,并且返回实例在返回时被销毁。因此,如果您的客户端可以在您关闭池后返回对象,则实例将被清理。如果您想阻止直到完成,请观看numActive
。close
还会从 jmx 取消注册池,因此在这种情况下直接使用getNumActive
。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. Afterclose
,borrowObject
will fail withIllegalStateException
, butreturnObject
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, watchnumActive
.close
also deregisters the pool from jmx, so usegetNumActive
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?