原创

MySQL自动关闭连接导致DBCP报错

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://daichen.blog.csdn.net/article/details/8448894


长时间不操作页面后,再操作时程序就会报错,异常如下:

 

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure

Thelast packet successfully received from the server was 8,064,672 millisecondsago. The last packet sent successfully to the server was 8,064,672 millisecondsago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
atsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
atsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3317)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1941)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696)
atcom.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2264)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
atorg.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
atorg.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
...
Caused by: java.net.SocketException: Connection reset by peer: socket writeerror
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3298)
... 34 common frames omitted

 

原因是DBCP保持连接的超时时间比MySQL连接超时时间长。mysql配置中的wait_timeout值一定要大于等于连接池的idle_timeout值,否则mysql会在wait_timeout的时间后关闭连接,然而连接池还认为该连接可用,这样就会产生异常。

 

通过MySQL客户端连到服务器,查看目前的配置。


 

参考资料

 

1.      DBCP配置

http://commons.apache.org/dbcp/configuration.html

http://wenku.baidu.com/view/06c94707de80d4d8d15a4f9a.html

 

2.      MySQL连接空闲超过8小时DBCP造成的异常如何解决

http://database.51cto.com/art/201107/276325.htm

 

3.      MySQL数据库连接超时(wait_timeout)问题的处理

http://sarin.iteye.com/blog/580311

 


文章最后发布于: 2012-12-28 20:29:08
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览