天天看點

mysql逾時:The last packet successfully received from the server was 172,848,658 milliseconds ago.

今天查詢接口報錯了。報錯内容如下:

The last packet successfully received from the server was 172,848,658 milliseconds ago. The last packet sent successfully to the server was 172,848,673 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.

明明上周是好好的,其實以前我出過這個錯誤,這次決定要記錄下來。

檢視一下報錯的内容大緻是:上次伺服器成功接收資訊包是172,848,658 毫秒以前,上次伺服器成功發送資料是172,848,673毫秒以前,已經超出伺服器配置的wait_timeout’逾時時間。如果想避免這種異常就需要增加wait_timeout’的逾時時間。或者在xml中設定autoReconnect=true

第一種方式

在my.ini中mysqld下面增加

wait_timeout=2147483

interactive_timeout=2147483

最大隻能是2147483 解析為24.85天。

大家可以根據自己的需求設定。如果連接配接人數很多,時間可以适當的減少,連接配接人數少可以适當增加。

第二種方式

有的同學可能沒有my.ini 此時可以輸入sql指令

第三種方式(轉)

  • 如果使用的是JDBC,在JDBC URL上添加?autoReconnect=true,如:
<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>      
  • 如果是在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>      

繼續閱讀