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的重连机制》
以上。
原文地址:https://www.cnblogs.com/chevin/p/11122825.html
时间: 2024-11-05 11:22:45