日志遇到问题
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 232,596,703 milliseconds ago. The last packet sent successfully to the server was 232,596,704 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.
解释原因
MySQL超时设置的问题。
如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接。
MySQL服务器默认的“wait_timeout”是28800秒即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>
四、修改MySQL的参数
在my.cnf文件中追加2条配置,并重启MySQL:
[mysqld]
wait_timeout=31536000
interactive_timeout=31536000
可执行SQL语句,验证是否修改成功:
show global variables like '%time%' ;