2016-06-07 49 views
0

我的設置是MySql.Data.MySqlClient v6.9.8.0Microsoft.Practices.EnterpriseLibrary.Data v6.0.0將MySQL連接與Microsoft企業庫集成

該程序是一個長時間運行的程序,可以持續監聽任務,然後以某種形式的數據庫操作執行作業(取決於請求的內容)。有時候,請求會一個接一個,有時會出現在他們之間幾個小時。 (不是所有的時間 - 這些都是間歇性的問題)

我已經使用在連接字符串中Pooling=true嘗試,但它使我有很多問題

下面是一個例子:

[MySqlException (0x80004005): Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: Reading from the stream has failed.] 

關閉pooling可修復此問題,但同時會導致查詢速度變慢,因爲我們無法重新使用連接。我在網上搜索了很多人都有這個相同的問題,我發現唯一的修復/解決方法是Pooling=false,如果可能,我寧願避免。

這裏是我的查詢代碼的例子:

Database db = this.GetDatabase(databaseName); 

List<dynamic> results = new List<dynamic>(); 

// Run the sql query 
using (DbCommand dbCommand = db.GetSqlStringCommand(query)) 
{ 

    foreach (var parameter in inParameters) 
    { 
     db.AddInParameter(dbCommand, parameter.Key, parameter.Value.Item1, parameter.Value.Item2); 
    } 

    foreach (var parameter in outParameters) 
    { 
     db.AddOutParameter(dbCommand, parameter.Key, parameter.Value.Item1, parameter.Value.Item2); 
    } 

    using (IDataReader dataReader = db.ExecuteReader(dbCommand)) 
    { 
     IDictionary<string, object> instance; 

     do 
     { 
      // Read each row 
      while (dataReader.Read()) 
      { 
       instance = new ExpandoObject() as IDictionary<string, object>; 

       // Populate the object on the fly with the data 
       for (int i = 0; i < dataReader.FieldCount; i++) 
       { 
        instance.Add(dataReader.GetName(i), dataReader[i]); 
       } 

       // Add the object to the results list 
       results.Add(instance); 
      } 
     } while (dataReader.NextResult()); 
    } 

    return results; 
} 

任何想法?

+0

其中EntLib.Data - > MySql「adapter」您正在使用? http://entlibcontrib.codeplex.com/releases我沒有看到一個編碼EntLib 6. – granadaCoder

+0

我安裝它形式的Nuget,這是我顯示的版本 –

+0

請報告nuget包和版本。只需在「packages.config」文件中找到該行即可。 – granadaCoder

回答

1

你可以試試嗎?我知道我知道。使用「使用」應該意味着我不必調用dataReader.Close()方法...但我仍然這樣做。我也稍微改變了dr.Read塊。

這個人談論它。

http://www.joseguay.com/uncategorized/ensure-proper-closure-disposal-of-a-datareader

我知道,我知道。你不應該。即使在使用Ent庫時,我也會執行額外的.Close步驟來嘗試並確保。

Database db = this.GetDatabase(databaseName); 

List<dynamic> results = new List<dynamic>(); 

// Run the sql query 
using (DbCommand dbCommand = db.GetSqlStringCommand(query)) 
{ 

    foreach (var parameter in inParameters) 
    { 
     db.AddInParameter(dbCommand, parameter.Key, parameter.Value.Item1, parameter.Value.Item2); 
    } 

    foreach (var parameter in outParameters) 
    { 
     db.AddOutParameter(dbCommand, parameter.Key, parameter.Value.Item1, parameter.Value.Item2); 
    } 

    using (IDataReader dataReader = db.ExecuteReader(dbCommand)) 
    { 
     IDictionary<string, object> instance; 

     while (dataReader.Read()) 
     { 
      instance = new ExpandoObject() as IDictionary<string, object>; 

      // Populate the object on the fly with the data 
      for (int i = 0; i < dataReader.FieldCount; i++) 
      { 
       instance.Add(dataReader.GetName(i), dataReader[i]); 
      } 

      // Add the object to the results list 
      results.Add(instance); 
     } 

     if (dataReader != null) 
     { 
      try 
      { 
       dataReader.Close(); 
      } 
      catch { } 
     }   

    } 

    return results; 
} 
+0

我會試一試,讓你知道,謝謝 –

+0

幾天後,它仍然像一種享受,謝謝! –

+1

經驗法則是......如果你看到「連接不足」的問題......盡你所能去正確地擺脫它們......不管「你應該'不得不「在互聯網上的聲明。 – granadaCoder