zoukankan      html  css  js  c++  java
  • MySQL自动关闭连接导致DBCP报错


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

    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


  • 相关阅读:
    2017寒假作业二 汇总随笔
    2017寒假作业一
    UVA 1601 POJ 3523 The Morning after Halloween 【双向BFS】【A*】 (好题)
    UVA 10570 Meeting with Aliens 【枚举+结论题】
    UVA 1614 Hell on the Markets 【贪心+结论题】
    UVA 10603 Fill【BFS】
    Codevs 1288 埃及分数 【IDA*】
    UVA 11212 Editing a Book 【IDA*】
    UVA 11624 Fire! 【特殊BFS】
    UVA 1599 Ideal Path 【两次BFS+贪心】 (好题)
  • 原文地址:https://www.cnblogs.com/xiaomaohai/p/6157789.html
Copyright © 2011-2022 走看看