dbcp連接報錯:The last packet successfully received from the server was 50,664,909 milliseconds ago. The last packet sent successfully to the server was 50,664,912 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.
原因:Mysql 數據庫連接8小時后若沒有連接會自動斷開。
分析:
方法一:查看數據庫中連接時間 mysql>SHOW VARIABLES LIKE '%_timeout%'
wait_timeout 等待時間為8小時,網上有解決方案說修改這個時間。
SET GLOBAL interactive_timeout=31536000;
SET GLOBAL wait_timeout=31536000;
再用上面的語句查看發現沒有改變,此時用另一個語句查看:
mysql>SHOW GLOBAL VARIABLES LIKE '%_timeout%'
發現wait_timeout最大只能設置到2147183s,如果程序在這個時間內一定有連接可以使用,但是我的程序不是。
如果這個時間可以的直接修改mysql配置文件,上面的語句設置mysql重啟會失效。
[mysqld]
wait_timeout=31536000
interactive_timeout=31536000
重啟生效,需要同時修改這兩個參數。
解決:
方法二:修改dbcp.properties配置文件
增加以下屬性
validationQuery=select current_date() #請求之前和之后進行連接池測試 testOnBorrow=false testOnReturn=false #空閑時是進行驗證,檢查對象是否有效 testWhileIdle=true #超過removeAbandonedTimeout時間后,是否進行沒用連接(廢棄)的回收(默認為false,調整為true) removeAbandoned=true #超過時間限制,回收沒有用(廢棄)的連接(默認為 300秒) removeAbandonedTimeout=90 #秒對連接池進行一次檢測,將對象閑置時間超過minEvictableIdleTimeMillis秒的對象進行銷毀,創建新的對象來取代 timeBetweenEvictionRunsMillis=30000 minEvictableIdleTimeMillis=1800000 #設定在進行后台對象清理時,每次檢查幾個鏈接。默認值是3. numTestsPerEvictionRun=20
參考:https://blog.csdn.net/liuyangvoid/article/details/25975157
https://blog.csdn.net/pandajava/article/details/41946251