首页新闻找找看学习计划

关闭数据库连接时出现异常

0
悬赏园豆:5 [已解决问题] 解决于 2017-03-27 12:08

使用的是数据库连接池commons-dbcp-1.3.jar,在程序运行一段时间后,执行下面的语句抛出异常:

if (null != conn) try { conn.close(); } catch (Exception ex) {ex.printStackTrace();}

异常信息:

java.sql.SQLException: Already closed.
....

ERROR [09:36:31 26-三月-17 CST][servlet.alarm] BAlarmServlet.java-initPointMap: error = The last packet successfully received from the server was 128,406,334 milliseconds ago. The last packet sent successfully to the server was 128,406,334 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
ERROR [09:36:31 26-三月-17 CST][servlet.alarm] BAlarmServlet.java-getAlarmOneDay: The last packet successfully received from the server was 128,406,355 milliseconds ago. The last packet sent successfully to the server was 128,406,355 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 128,406,355 milliseconds ago. The last packet sent successfully to the server was 128,406,355 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

 

判断为null,关闭,应该是正常的啊?为什么会这样呢?

不过,看到过的程序里面是不会写后面的ex.printStackTrace();异常输出语句的。

智人N的主页 智人N | 小虾三级 | 园豆:601
提问于:2017-03-27 09:52
< >
分享
最佳答案
0

http://blog.csdn.net/pq258280920/article/details/17136335

 

上面的博文讲的符合我的情况,很好!这时由于服务器那边主动断开了空闲连接造成的。

智人N | 小虾三级 |园豆:601 | 2017-03-27 11:36
其他回答(2)
0

日志不是说的很清楚了么?连接已经被关闭掉了,你再close就报错了。

收获园豆:3
Daniel Cai | 园豆:10374 (专家六级) | 2017-03-27 10:18

的确和日志说的一样。但是,我在获取连接后没有做任何关闭操作的。

为什么就是关闭的呢? 

支持(0) 反对(0) 智人N | 园豆:601 (小虾三级) | 2017-03-27 11:12
0

不是什么时候都要用 try

这些关闭,连接数据库的代码可以封装起来!方便操作

收获园豆:2
男人要爽 | 园豆:10 (初学一级) | 2017-03-27 10:51

我用的数DBCP数据库连接池,连接是从那里面获取的。

之前直接建立连接时是没有这样的问题的,该关闭时就关闭了。

检查了一下代码,里面最大连接数为400,但setMinIdle的值设置为15*1000了。

当时我还注释为15秒,可笑可笑。

 

这个问题应该和mysql数据库连接超时时间、dhcp模块对超时的配置有关系。

更改后的代码:

// Fixed: 15 idle connections
datasrc.setMinIdle(15);

// 120 seconds
datasrc.setRemoveAbandonedTimeout(120);
datasrc.setRemoveAbandoned(true);

 

支持(0) 反对(0) 智人N | 园豆:601 (小虾三级) | 2017-03-27 11:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册