java 如何缓解因连接 com.mysql.jdbc.JDBC4Connection@11d08960 触发的连接泄漏,
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33887002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I mitigate Connection leak triggered for connection com.mysql.jdbc.JDBC4Connection@11d08960,
提问by david
I have an mqtt client getting request subscribing from topics, and then I give it to threadpool of fixed size 50. Im using hikaricp 2.4.2 for DB Pooling MySQL database.
我有一个 mqtt 客户端获取订阅主题的请求,然后我将其提供给固定大小 50 的线程池。我使用 hikaricp 2.4.2 进行 DB Pooling MySQL 数据库。
Im currently using 2.4.2 and this is my setup
我目前使用 2.4.2,这是我的设置
HikariConfig config = new HikariConfig();
config.setDataSourceClassName(CLASS_FOR_NAME);
config.setJdbcUrl(HOST);
config.setUsername(USER);
config.setPassword(PASS);
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(30));
config.setValidationTimeout(TimeUnit.MINUTES.toMillis(1));
config.setMaximumPoolSize(10);
config.setMinimumIdle(0);
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(2)); // 120 seconds
config.setIdleTimeout(TimeUnit.MINUTES.toMillis(1)); // minutes
config.setConnectionTimeout(TimeUnit.MINUTES.toMillis(5));
config.setConnectionTestQuery("/* ping */ SELECT 1");
Heres the full log message :
这是完整的日志消息:
WARNLOG:
警告日志:
811439 [Hikari housekeeper (pool HikariPool-0)] WARN com.zaxxer.hikari.pool.ProxyLeakTask - Connection leak detection triggered for connection com.mysql.jdbc.JDBC4Connection@11d0896, stack trace follows java.lang.Exception: Apparent connection leak detected at com.hcpdatabase.DataSource.getConnection(DataSource.java:69) at com.database.AccessDatabase.create_alert(AccessDatabase.java:3849) at com.runnable.StartTaskRunnable2.execute(StartTaskRunnable2.java:78)
811439 [Hikari 管家(池 HikariPool-0)] WARN com.zaxxer.hikari.pool.ProxyLeakTask - 为连接 com.mysql.jdbc.JDBC4Connection@11d0896 触发的连接泄漏检测,堆栈跟踪遵循 java.lang.Exception:明显的连接泄漏检测到 com.hcpdatabase.DataSource.getConnection(DataSource.java:69) at com.database.AccessDatabase.create_alert(AccessDatabase.java:3849) at com.runnable.StartTaskRunnable2.execute(StartTaskRunnable2.java:78)
Is this normal ? do i have to catch this?
这正常吗?我必须抓住这个吗?
回答by david
As I have reviewed my codes over and over again. I came to realize that I was barking at the wrong tree, Seems like hikari is very reliable when it comes to connection leak. The problem is when amazon aws ec2 instance is stealing some of my cpu and is even greater than what i thought. So after the cpu goes up 99%, Connection leak is detected even though my codes clearly closed it in finally block. So the problem lies with the machine.
因为我一遍又一遍地检查了我的代码。我开始意识到我在错误的树上吠叫,似乎 hikari 在连接泄漏方面非常可靠。问题是当 amazon aws ec2 实例正在窃取我的一些 cpu 时,甚至比我想象的还要大。因此,在 cpu 上升 99% 后,即使我的代码在 finally 块中明确关闭它,也会检测到连接泄漏。所以问题出在机器上。
I thank you for all who participated to answer.
我感谢所有参与回答的人。
回答by Nitin
walk thru the code with 'stack trace' and it would lead you to un-closed connection or the connection that takes longer than threshold.
使用“堆栈跟踪”遍历代码,它会引导您进入未关闭的连接或连接时间超过阈值的连接。