如何在 Java 中正确销毁 Apache Commons DBCP 池?
我想使用 PoolingDataSource 作为我的连接池(API 位于: http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/PoolingDataSource.html),但我不知道在什么时候如何处理池我不再需要它了。如果我想连接到新数据库并且不再需要旧池中的连接怎么办?池上没有 close 方法。
I would like to use a PoolingDataSource as my connection pool (API at: http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/PoolingDataSource.html), but I don't know what to do with the pool when I no longer need it. What if I want to connect to a new database and don't need the connections in the old pool anymore? There is no close method on the pool.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不一定需要杀死该池才能创建新池。
您可以使用 maxIdle、timeBetweenEvictionRunsMillis 和 minEvictableIdleTimeMillis 参数管理其中的连接(请参阅此处)确保空闲连接在合理的时间内关闭。
或者您可以配置 GenericObjectPool 以编程方式使用这些参数,并在创建 PoolingDataSource 时使用。如果你想强制它,它有一个 close() 方法。
You don't necessarily need to kill this pool to create a new one.
You can manage the connections in it using the maxIdle, timeBetweenEvictionRunsMillis and minEvictableIdleTimeMillis parameters (see here) to ensure idle connections get closed in a reasonable time.
Or you can configure a GenericObjectPool with those parameters programatically and use when creating your PoolingDataSource. That has a close() method if you want to force it.
很抱歉没有直接回答您的问题,但我可以建议不使用 DBCP 吗?它存在一些严重的问题,其他图书馆从中吸取了教训并进行了改进。
那里有更好的池。
Sorry for not answering your question directly, but may I recommend not using DBCP? It has had a number of serious problems, which other libraries learned from and improved upon.
There are much better pools out there.
您使用什么类型的泳池?如果您使用的是 AbandonedObjectPool,那么这是 Commons Pool 的 GenericObjectPool,它有一个 close() 方法。
What kind of pool are you using? If you're using the AbandonedObjectPool, then that's a subclass of Commons Pool's GenericObjectPool, which has a close() method.