2016-11-24 161 views
0

我只是檢查有關連接超時堆棧溢出的其他職位,但看起來使用MySQL連接超時

com.CommandTimeout = int.MaxValue; 

dbaccess.ServerAddress = "Server=mysql;Database=MyDB;Connect Timeout=2147483;Uid=username;Pwd=mypassword"; 

不幸的是像其他人固定它,我仍然得到這個錯誤40秒後的消息:

在操作或服務器完成之前已經過去的超時時間不是響應G。

你可以看看這兩個截圖。

MySQL Workbench工作正常,並且執行相同的查詢需要48秒。

ErrorMessage

Workbench

我不能使用的限制,以減少收購,因爲我集中在不同的表中的信息分割的子集。存儲過程不能使用,因爲信息也存儲在不同的mysql服務器中。

任何反饋將不勝感激。

費爾南多

編輯:

這是代碼。我發佈了圖像來顯示執行時間,而不是錯誤消息。

public DataSet ExecuteQuery(string[] Query, TableMap[] TableMappings) 
{ 
    //Mysql vars 
    MySqlConnection con; 
    MySqlCommand com; 
    MySqlDataAdapter adapter; 
    DataSet ds; 

    con = new MySqlConnection(); 
    con.ConnectionString = _serveraddress; 

    con.Open(); 

    com = con.CreateCommand(); 
    com.CommandType = System.Data.CommandType.Text; 

    foreach (string st in Query) 
    { 
     com.CommandText = com.CommandText + st; 
    } 

    com.CommandTimeout = int.MaxValue; 

    adapter = new MySqlDataAdapter(com.CommandText, con); 

    foreach (TableMap tm in TableMappings) 
    { 
     adapter.TableMappings.Add(tm.SourceTable, tm.TableSet); 
    } 

    ds = new DataSet(); 
    adapter.Fill(ds); 

    return ds; 
} 
+1

請在實例化dbaccess背後直到打開連接時顯示完整代碼。 – C4u

+0

如果將'int.MaxValue'更改爲5000,會發生什麼情況? –

+0

@SimonPrice同樣的錯誤:( –

回答

0

你的問題就出在這行

adapter = new MySqlDataAdapter(com.CommandText, con); 

在這裏,您傳遞命令文本到新的個MySqlDataAdapter。當然,這有個MySqlDataAdapter沒有你的命令集,因爲三三兩兩(電源適配器和命令)沒有連接在一起超時的知識。

如果更改爲

adapter = new MySqlDataAdapter(com); 

然後適配器將使用的MySqlCommand及其CommandTimeout屬性

說,我真的建議你開始接近繼完善的模式數據庫代碼

public DataSet ExecuteQuery(string[] Query, TableMap[] TableMappings) 
{ 
    // using statement will ensure proper closing and DISPOSING of the objects 
    using(MySqlConnection con = new MySqlConnection(_serveraddress)) 
    using(MySqlCommand com = con.CreateCommand()) 
    { 
     con.Open(); 
     // Not needed, it is the default 
     // com.CommandType = System.Data.CommandType.Text; 

     foreach (string st in Query) 
     { 
      // Are you sure the st is properly terminated with a semicolon? 
      com.CommandText = com.CommandText + st; 
     } 
     com.CommandTimeout = int.MaxValue; 
     using(MySqlDataAdapter adapter = new MySqlDataAdapter(com)) 
     { 
      foreach (TableMap tm in TableMappings) 
       adapter.TableMappings.Add(tm.SourceTable, tm.TableSet); 

      DataSet ds = new DataSet(); 
      adapter.Fill(ds); 
      return ds; 
     } 
    } 
} 

using語句在這裏非常重要,特別是對於MySql來說,非常明智的連接未關閉correctl y並且可以通過關於的錯誤消息來停止您的程序,不再有可用的連接

0

變化Pwdpassword。此外,屬性server應設置爲一個IP或域。

conn = "Server=[ip/domain];...password=mypassword"; 

錯誤的憑據會導致登錄失敗,而不是超時。我想它真的試圖連接到mysql。如果服務器在本地計算機上運行,​​只需將localhost設置爲服務器。

+0

@Steve謝謝,它的工作原理!我接受了關於「using」命令的建議,我會修改其餘的mysql調用使用「using」。再一次,謝謝 –

+0

在'server'參數中,我使用的是計算機名稱,因爲它在itnernal網絡或VPN上使用。我應該使用內部域名的FQDN嗎?你在這裏的推薦是什麼?我願意改進 –

+0

這很大程度上取決於你的軟件。在本地網絡上,我猜對其中的一個來說,它們沒有什麼大的好處。我通常會提供設置來更改數據庫連接形式的用戶端,這樣這個問題就會消失。我可以肯定地說的唯一事情就是使用像FQDN或計算機名稱(從未嘗試過)的ip表示來保持連接,即使ip發生變化。 – C4u