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

本文介绍了如何解决MySQL连接超时问题,包括在JDBC、Spring框架中使用DBCP、c3p0连接池时的配置调整建议,以及在Tomcat server.xml中使用连接池时的优化方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的时候,添加属性testConnectionOnCheckin和testConnectionOnCheckout

<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增加属性validationQuery和testOnBorrow

<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" /> 


转载于:https://my.oschina.net/pvpCC9IFwqz4/blog/644505

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值