# Set a connection to wait 8hours in idle status. wait_timeout =86400 相關參數,紅色部分 mysql> show variables like '%timeout%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | connect_timeout | 5 | | delayed_insert_timeout | 300 | | innodb_lock_wait_timeout | 50 | | interactive_timeout | 28800 | | net_read_timeout | 30 | | net_write_timeout | 60 | | slave_net_timeout | 3600 | | wait_timeout | 28800 | +--------------------------+-------+ 同一時間,這兩個參數只有一個起作用。到底是哪個參數起作用,和用戶連接時指定的連接參數相關,缺省情況下是使用wait_timeout。我建議是將這兩個參數都修改,以免引起不必要的麻煩。
這兩個參數的默認值是8小時(60*60*8=28800)。我測試過將這兩個參數改為0,結果出人意料,系統自動將這個值設置為。換句話說,不能將該值設置為永久。 將這2個參數設置為24小時(60*60*24=604800)即可。 set interactive_timeout=604800; set wait_timeout=604800;
# How long to keep unused connections around(in seconds) # Note: MySQL times out idle connections after 8hours(28,800seconds) # so ensure this value is below MySQL idle timeout cpool.maxIdleTime=25200 在 Spring 的配置文件中:
3. 定期使用連接池內的連接,使得它們不會因為閑置超時而被 MySQL 斷開。 修改 c3p0 的配置文件,設置:
# Prevent MySQL raise exception after a long idle timecpool.preferredTestQuery='SELECT 1'cpool.idleConnectionTestPeriod=18000cpool.testConnectionOnCheckout=true 修改 Spring 的配置文件: