1.由於MySQL默認是8小時的wait_timeout,當超過8小時的連接時間后,在JAVA中調用將出現如下報錯
SEVERE EXCEPTION com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was175588 seconds ago.The last packet sent successfully to the server was 175588 seconds ago, which 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. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3246) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1917) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922) at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105) at cn.sm.ApplePack.ApplePack.handle(ApplePack.java:35) at cn.sm.DataProcessNSQConsumerApp.handleMessage(DataProcessNSQConsumerApp.java:108) at com.sproutsocial.nsq.SubConnection$3.run(SubConnection.java:178) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.net.SocketException: 斷開的管道 (Write failed) at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111) at java.net.SocketOutputStream.write(SocketOutputStream.java:155) at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82) at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140) at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3227) ... 14 more
解決方法有兩個:修改MySQL的配置或者設置c3p0的屬性
2.可以通過如下語句查看wait_timeout的值:
①查看
show GLOBAL VARIABLES like '%timeout%'; #全局,和mysql中的my.cnf中的配置一致 show VARIABLES like '%timeout%'; #會話
修改(也可以修改my.cnf配置文件並重啟達到相同效果)
set GLOBAL interactive_timeout=604800; #24小時
②里面還有一個interactive_timeout,具體區別可以參看《MySQL - wait_timeout與interactive_timeout詳解》
3.配置c3p0
①修改的配置
<property name="breakAfterAcquireFailure">false</property>
<property name="testConnectionOnCheckout">false</property>
<property name="testConnectionOnCheckin">false</property>
<property name="idleConnectionTestPeriod">3600</property> <!--多長時間檢查一次,單位秒-->
<property name="acquireRetryAttempts">10</property>
<property name="acquireRetryDelay">1000</property>
需要在每次使用的時候getConnection()從連接池中獲取Connection,並在使用完成之后進行close歸還到連接池中。
②關於c3p0的每個配置屬性的詳細信息可參看官網《c3p0官網》
關於c3p0的重連配置以及介紹可參看《關於c3p0的重連機制》
以上。