首页 > 代码库 > 开发中出现的异常com.mysql.jdbc.CommunicationsException

开发中出现的异常com.mysql.jdbc.CommunicationsException

web开发遇到的异常:

com.mysql.jdbc.CommunicationsException:The last packet successfully received from the server was58129 seconds ago.Thelast packet sent successfully to the server was 58129 seconds ago, which islonger than the server configured value of ‘wait_timeout‘. You should considereither expiring and/or testing connection validity before use in yourapplication, increasing the server configured values for client timeouts, orusing the Connector/J connection property ‘autoReconnect=true‘ to avoid thisproblem.


解决办法:

如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接, 要重启tomcat.

不用hibernate的话, connection url加参数: autoReconnect=true


hibernate的话, 加如下属性: 

<propertyname="connection.autoReconnect">true</property>

<propertyname="connection.autoReconnectForPools">true</property>

<propertyname="connection.is-connection-validation-required">true</property>

 

要是还用c3p0连接池:

<propertyname="hibernate.c3p0.acquire_increment">1</property>

<propertyname="hibernate.c3p0.idle_test_period">0</property>

<propertyname="hibernate.c3p0.timeout">0</property>

<propertyname="hibernate.c3p0.validate">true</property>

 


或者最王道的解决办法,那就是直接修改mysql的配置文件my.ini 在配置文件的最后增加wait_timeout=343232后面的数字是以秒计算。改个10100年多方便的。


开发中出现的异常com.mysql.jdbc.CommunicationsException