简体   繁体   English

有没有办法关闭jdbc连接池中所有未使用的活动连接

[英]is there a way to close all unused active connections in a jdbc connection pool

I was just thinking that in a large web application where tens of connections are made to database for every client using the application, there is a possibility that very few of the connections are gone unclosed. 我只是在想,在一个大型Web应用程序中,使用该应用程序的每个客户端都与数据库建立了数十个连接,因此很有可能会断开很少的连接。 Though it is strictly up to us that we must close all connections explicitly but, you know, there may be mistakes. 尽管必须完全明确地关闭所有连接,这完全取决于我们,但是,您可能会出错。 If this happens then after some days we may find that our connection pool has reached its maximum size and the application is not running being unable to get a connection. 如果发生这种情况,那么几天后,我们可能会发现我们的连接池已达到最大大小,并且应用程序无法运行,无法获得连接。

Actually I am facing this problem for one of my projects and whenever the connection pool reaches its maximum size I just restart the server which is not good. 实际上,我的一个项目面临此问题,并且只要连接池达到其最大大小,我就只是重新启动服务器,这是不好的。

I am curious to know how to handle this issue in a better manner and I want to know that is there such a thing that I can close all the unused active connections in my connection pool explicitly in a regular interval eg every 72 hours using java code ? 我很好奇如何更好地处理此问题,我想知道是否存在这样的事情,我可以定期(例如,每隔72小时使用Java代码)明确关闭连接池中所有未使用的活动连接?

I am talking about Java/J2ee technologies and my server is Glassfish Application Server2.1. 我正在谈论Java / J2ee技术,我的服务器是Glassfish Application Server2.1。

EDIT: I am using the connection pool that Glassfish Application server provides within, not any third party connection pool and using this pool using JNDI DataSource. 编辑:我使用的是Glassfish应用程序服务器提供的连接池,而不是任何第三方连接池,并且使用此池使用JNDI DataSource。

need your suggestions........ 需要您的建议........

Thanks 谢谢

You can use MiniConnectionPoolManager. 您可以使用MiniConnectionPoolManager。 It is a lightweight standalone Java JDBC connection pool manager. 它是一个轻量级的独立Java JDBC连接池管理器。

Take a look at this article http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcConnectionPool.html . 看看这篇文章http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcConnectionPool.html and the dispose() method. 和dispose()方法。

There are a lot of jdbc drivers supported http://www.source-code.biz/miniconnectionpoolmanager/ 有很多受支持的jdbc驱动程序http://www.source-code.biz/miniconnectionpoolmanager/

If I were to address this issue in the absence of a good connection pooler that provides this out of box, I would do it as follows: 如果我要解决这个问题,而又没有一个好的连接池来提供此功能,那么我将按照以下步骤进行操作:

  1. One entry point for my application to get connections. 我的应用程序获得连接的一个入口点。
  2. Decorate the connection obtained from the self-maintained-pool, associate a timer set to a configured value (to close unused active connections) and return this decorated connection to the clients of the pool. 装饰从自维护池获得的连接,将计时器集与配置值关联(以关闭未使用的活动连接),然后将此经过装饰的连接返回给池的客户端。
  3. If the close method is called before the timer fires, cancel the timer, delegate the call to the wrapped connection. 如果在计时器触发之前调用了close方法,请取消计时器,并将调用委派给包装的连接。
  4. If the close method is not called when the timer fires, close the wrapped connection and invalidate the state of this decorated connection object so that it will throw IllegalStateException if someone tries to work with it again. 如果在计时器触发时未调用close方法,请关闭包装的连接并使该装饰的连接对象的状态无效,以便在有人尝试再次使用它时将抛出IllegalStateException

My two cents! 我的两分钱!

Edit : C3P0 has unreturnedConnectionTimeout that can be configured to handle these kinds of connections taken out from the pool but not returned through a close() call. 编辑C3P0具有unreturnedConnectionTimeout ,可以将其配置为处理从池中取出但不能通过close()调用返回的这类连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM