MySQL connection timeout mechanisms for handling reconnection of c3p0

1. Since the default MySQL the wait_timeout 8 hours, more than 8 hours when the connection time, the call appears in JAVA given below

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

 

There are two solutions: Modify the MySQL configuration or set of attributes c3p0

2. You can view the value of wait_timeout by the following statement:

① View

the GLOBAL the VARIABLES Show like  ' %% timeout ' ; # globally, and the mysql my.cnf same configuration 
Show the VARIABLES like  ' %% timeout ' ; session #

Modification (can also modify the configuration files and reboot my.cnf achieve the same effect)

SET the GLOBAL interactive_timeout seconds = 604800 ; # 24 hours

② there is also a interactive_timeout, specific differences can be found in " MySQL - Detailed wait_timeout and interactive_timeout "

 

3. Configure c3p0

① modified configuration

       <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的重连机制

 

以上。

Guess you like

Origin www.cnblogs.com/chevin/p/11122825.html