Apache Commons Pool close() 行为是什么

发布于 2024-08-15 04:17:00 字数 261 浏览 5 评论 0原文

我一直在寻求在我的应用程序的一部分中实现池化。我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-08-22 04:17:00

正如 Commons Pool 2 中 close 方法的 javadoc 中所述,调用此方法时,空闲实例将被销毁,但签出到客户端的实例不受影响。 close 后,borrowObject 将失败并出现 IllegalStateException,但 returnObject 将成功,并且返回实例在返回时被销毁。因此,如果您的客户端可以在您关闭池后返回对象,则实例将被清理。如果您想阻止直到完成,请观看numActiveclose 还会从 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. 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.

酒废 2024-08-22 04:17:00

一般来说(无论池库如何),销毁正在使用的对象是不安全的。这样做很可能会导致异常。如果您想保证彻底关闭,那么您需要确保所有对象都已返回到池中。

在所有对象返回到池中之前进行关闭是否有原因?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文