mysql連接的空閑時間超過8小時后 MySQL自動斷開該連接解決方案


 

在連接字符串中  添加設置節點 ConnectionLifeTime(計量單位為 秒)。超過設定的連接會話 會被殺死!

Connection Lifetime, ConnectionLifeTime

0

When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online. A value of zero (0) causes pooled connections to have the maximum connection timeout.

 

    public bool ConnectionLifetimeExpired()
    {
      TimeSpan ts = DateTime.Now.Subtract(creationTime);
      if (Settings.ConnectionLifeTime != 0 &&
        ts.TotalSeconds > Settings.ConnectionLifeTime)
        return true;
      return false;
    }

 

 

MySQL 的默認設置下,當一個連接的空閑時間超過8小時后,MySQL 就會斷開該連接,而 c3p0 連接池則以為該被斷開的連接依然有效。在這種情況下,如果客戶端代碼向 c3p0 連接池請求連接的話,連接池就會把已經失效的連接返回給客戶端,客戶端在使用該失效連接的時候即拋出異常

解決這個問題的辦法有三種:

1. 增加 MySQL 的 wait_timeout 屬性的值。

修改 /etc/mysql/my.cnf文件,在 [mysqld] 節中設置:

# 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;

2. 減少連接池內連接的生存周期,使之小於上一項中所設置的 wait_timeout 的值。
修改 c3p0 的配置文件,設置:

# 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 的配置文件中:

復制代碼 代碼如下:

<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="maxIdleTime"value="${cpool.maxIdleTime}"/>
<!--other properties -->
</bean>



3. 定期使用連接池內的連接,使得它們不會因為閑置超時而被 MySQL 斷開。
修改 c3p0 的配置文件,設置:

# Prevent MySQL raise exception after a long idle timecpool.preferredTestQuery='SELECT 1'cpool.idleConnectionTestPeriod=18000cpool.testConnectionOnCheckout=true
修改 Spring 的配置文件:

復制代碼 代碼如下:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="preferredTestQuery" value="${cpool.preferredTestQuery}"/>
<property name="idleConnectionTestPeriod" value="${cpool.idleConnectionTestPeriod}"/>
<property name="testConnectionOnCheckout" value="${cpool.testConnectionOnCheckout}"/>
<!--other properties --></bean> 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM