標籤:
查看線上日誌發現了如下的異常:
ERROR 500.jsp -
Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 31,680,464 milliseconds ago. The last packet sent successfully to the server was 31,680,469 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.
線上伺服器wait_timeout的值為:
select @@wait_timeout;
+----------------+
| @@wait_timeout |
+----------------+
| 7200 |
+----------------+
即兩個小時內沒有串連的話,伺服器會斷掉串連。等這時有請求過來的話,請求池會分配一中斷的串連,於是報了如上的異常。
經搜尋可以在串連池中添加一些額外的配置來避免此問題,如DBCP:
<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑串連,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="validationQuery" value="SELECT ‘x‘" />
<property name="testWhileIdle" value="true" />
線上環境資料庫連接逾時原因