com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from

来源:互联网 发布:网络配置方案 编辑:程序博客网 时间:2024/05/01 22:46

出现问题:

   新的应用发布后发现每过一段时间后tomcat就会报错。错误信息大概如下:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 152,219,305 milliseconds ago.  The last packet sent successfully to the server was 152,219,305 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.MySQLNonTransientConnectionException: No operations allowed after connection closed.

No operations allowed after connection closed.

java.sql.SQLException: Operation not allowed after ResultSet closed
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
 at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:803)
 at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6985)
 at com.dz.servlet.LoginServlet.service(LoginServlet.java:54)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
 at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:845)
 at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:688)
 at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1315)
 at java.lang.Thread.run(Unknown Source)

 

在百度里搜到很多,但是都没有解决我的问题,今天看到了一篇写的还算靠谱的文章,http://blog.csdn.net/liuqiyu/article/details/6092377他说的确实没错,但是最关键的地方他说的有点模糊,就是在什么地方修改配置文件,害的我琢磨了好久,我看他也是看别人的方法,而且我估计理解稍微有点错误,修改的位置应该是


[client]

port=3306

[mysql]

default-character-set=gbk


# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
#
[mysqld]
wait_timeout=1814400

# The TCP/IP Port the MySQL Server will listen on
port=3306
上面是我贴出来的数据库my.ini配置文件的部分,把wait_timeout=1814400 写在这个位置就没有问题了!!

解释及解决办法: 
 
    如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接, 得刷新应用才行. 
    不用hibernate的话, connection url加参数: autoReconnect=true 
    用hibernate的话, 加如下属性: 
        <property name="connection.autoReconnect">true</property> 
        <property name="connection.autoReconnectForPools">true</property> 
        <property name="connection.is-connection-validation-required">true</property>
 
    要是还用c3p0连接池: 
        <property name="hibernate.c3p0.acquire_increment">1</property> 
        <property name="hibernate.c3p0.idle_test_period">0</property> 
        <property name="hibernate.c3p0.timeout">0</property> 
        <property name="hibernate.c3p0.validate">true</property>

0 0