0

目前我正在使用 Apache Commons Pool 1.6 来管理 GenericKeyedObjectPool 连接池。我已经看到线程既抛出异常又无限期挂起,这两者都导致借用的连接从池中泄漏。在使用 returnObject() 引发异常的情况下,我已经纠正了连接泄漏,但理想情况下,我希望池有某种方法来管理此问题并检索挂起或泄漏的连接。

根据我迄今为止的调查,Commons Pool 1.x 不提供此功能,Commons Pool 2.x 提供此功能,但到目前为止我无法找到它是如何完成的。

当使用 Apache Commons Pool 2 挂起或泄漏时,如何将借用的连接拉回池中?

谢谢

4

1 回答 1

1

Apache commons pools2 有一个 AbandonedConfig,如果借用对象没有在“removeAbandonedTimeout”定义的持续时间内返回,则有助于放弃借用对象。
例子 :

        AbandonedConfig abandonedConfig = new AbandonedConfig();
        abandonedConfig.setRemoveAbandonedTimeout(evictAbandonedTime); // in seconds
        abandonedConfig.setRemoveAbandonedOnBorrow(true); // on borrow, test when pool is starving
        abandonedConfig.setRemoveAbandonedOnMaintenance(true); // and test pool when evicting
        pool.setAbandonedConfig(abandonedConfig);

但是,AbandonedConfig 仅适用于 GenericObjectPool 而不是 GenericKeyedObjectPoolConfig。

在 commons pool2 中实现此目的的唯一方法(可能是粗略的方法)是,在 GenericKeyedObjectPool 的单个键下使用多个 GenericObjectPool 实例(其池中只有一个对象),并使用 AbandonedConfig 设置 GenericObjectPool。

我讨厌这个解决方案的地方是两次借用,但从好的方面来说,第二次借用永远不会花费时间。

于 2017-10-21T16:30:26.333 回答