简体   繁体   English

JDBC是否找到未关闭连接的来源?

[英]JDBC finding the source of unclosed connections?

I am having issues where somewhere in my project several connections to mysql are not being closed, causing my application to become very slow after a couple of days,essentially forcing me to restart my application server. 我遇到的问题是,在我的项目中某处与mysql的连接未关闭,导致几天后我的应用程序变得非常慢,从本质上迫使我重新启动了我的应用程序服务器。

To find the source of this issue i created a map where the key was the class and method name of what was making the connection, and as the value a count that would get incremented and decremented every time that method made/closed a connection, i thought this would let me see which method making a connection to the database was leaving a lot of connections open, this turned out to be inaccurate. 为了找到此问题的根源,我创建了一个映射,其中的键是建立连接的类和方法的名称,并且作为值,每次该方法建立/关闭连接时,计数都会增加和减少。以为这样可以让我看看与数据库建立连接的方法是使许多连接保持打开状态,事实证明这是不准确的。

What would you suggest i do to find the source of the unclosed connection other than manually going through the code to find the source? 除了手动通过代码查找源以外,您建议我怎么做才能找到未闭合连接的源?

Thanks. 谢谢。

One solution (not straight forward) would to use BTrace. 一种解决方案(并非直接)是使用BTrace。 You would create scripts intercepting the java.sql.connection code 您将创建拦截java.sql.connection代码的脚本

https://kenai.com/projects/btrace https://kenai.com/projects/btrace

The requirement would be however: 但是,要求是:

  1. Either you would be using Oracle JDK6+ 您可能正在使用Oracle JDK6 +
  2. Or running application with Sun JDK5 on Solaris. 或在Solaris上使用Sun JDK5运行应用程序。

You could use aspectj to add tracing/logging/interceptors at various methods ( DriverManager.getConnection etc.). 您可以使用aspectj在各种方法( DriverManager.getConnection等)上添加跟踪/记录/拦截器。

However I suggest centralizing your connection management, and paying attention to handle exceptions correctly (even this can be centralized). 但是,我建议集中化连接管理,并注意正确处理异常(即使可以集中处理)。

使用可以捕获放弃的连接的连接池,例如Apache DNCP。

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

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