mysql重连,连接丢失:The last packet successfully received

The last packet successfully received from the server was 77,150,600 milliseconds ago.  The last packet sent successfully to the server was 77,150,601 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.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 77,150,600 milliseconds ago.  The last packet sent successfully to the server was 77,150,601 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.


###############################################################################


如果使用的是JDBC,在JDBC URL上添加?autoReconnect=true

jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly=false




如果是在Spring中使用c3p0连接池,则在定义datasource的时候,添加属性testConnectionOnCheckintestConnectionOnCheckout

<bean name="cacheCloudDB" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${cache.url}"/> <property name="user" value="${cache.user}"/> <property name="password" value="${cache.password}"/> <property name="initialPoolSize" value="10"/> <property name="maxPoolSize" value="${cache.maxPoolSize}"/> <property name="testConnectionOnCheckin" value="false"/> <property name="testConnectionOnCheckout" value="true"/> <property name="preferredTestQuery" value="SELECT 1"/> </bean>

如果是在Spring中使用DBCP连接池,在定义datasource增加属性validationQuerytestOnBorrow

<bean id="vrsRankDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${countNew.jdbc.url}" /> <property name="username" value="${countNew.jdbc.user}" /> <property name="password" value="${countNew.jdbc.pwd}" /> <property name="validationQuery" value="SELECT 1" /> <property name="testOnBorrow" value="true"/> </bean>


对于tomcat的server.xml中使用的连接池,http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html,http://commons.apache.org/dbcp/configuration.html使用DBCP的连接池可以采用 
<Resource name="jdbc/test" auth="Container"  
              type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"  
              url="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"  
              username="root" password="test" maxActive="500" maxIdle="10"  
              maxWait="-1" timeBetweenEvictionRunsMillis="10000" minEvictableIdleTimeMillis="10000" /> 


你可能感兴趣的:(mysql重连,连接丢失:The last packet successfully received)