2013-03-18 125 views
0

我們的應用程序使用EF 5和c#.net 4.5。由於我們的應用程序在世界各地都使用一臺Cenral服務器,因此我們遇到了超時並失去與服務器的連接的問題。實體框架句柄連接丟失

目前我們捕獲SaveChanges()方法並再次嘗試,直到用戶取消。我們可以爲代碼中的各種裝載做什麼?

編輯:我想提出的解決方案,但我不明白它的工作正確的:

public class MyRetryStrategy : ITransientErrorDetectionStrategy 
{ 
    public bool IsTransient(Exception ex) 
    { 
     if (ex != null && ex is SqlException) 
     { 
      foreach (SqlError error in (ex as SqlException).Errors) 
      { 
       switch (error.Number) 
       { 
        case 1205: 
         System.Diagnostics.Debug.WriteLine("SQL Error: Deadlock condition. Retrying..."); 
         return true; 

        case -2: 
         System.Diagnostics.Debug.WriteLine("SQL Error: Timeout expired. Retrying..."); 
         return true; 

        case -1: 
         System.Diagnostics.Debug.WriteLine("SQL Error: Timeout expired. Retrying..."); 
         return true; 
       } 
      } 
     } 

     if (ex != null && ex is EntityException) 
     { 
      ex = ex.InnerException; 
      foreach (SqlError error in (ex as SqlException).Errors) 
      { 
       switch (error.Number) 
       { 
        case 1205: 
         System.Diagnostics.Debug.WriteLine("SQL Error: Deadlock condition. Retrying..."); 
         return true; 

        case -2: 
         System.Diagnostics.Debug.WriteLine("SQL Error: Timeout expired. Retrying..."); 
         return true; 

        case -1: 
         System.Diagnostics.Debug.WriteLine("SQL Error: Timeout expired. Retrying..."); 
         return true; 
       } 
      } 
     } 

     // For all others, do not retry. 
     return false; 
    } 
} 

Enity框架只投我EntityExceptions所以我說第二個代碼路徑。

實際使用情況:

RetryPolicy policy = new RetryPolicy<MyRetryStrategy>(5, TimeSpan.FromMilliseconds(1000)); 
     policy.ExecuteAction(()=>_context.ProductStatuses.Include(x => x.Name.Translations).Load()); 

在MyRetryStrategy的IsTransient方法不會被調用,也沒有重試調用。我在測試之前測試過停止Databse。

我到底做錯了什麼?

回答

0

這個樣本讓我離開了地面。

http://hmadrigal.wordpress.com/2012/04/23/automatic-retries-using-the-transient-fault-handling-from-enterprise-libraries-entlib/

IService(下圖)是你自己定製的實現。

private static void RetryPolityUsingCode(IUnityContainer container, IService service, OutputWriterService writer) 
     { 
      writer.WriteLine("Begin sample: RetryPolityUsingCode"); 
      // Define your retry strategy: retry 5 times, starting 1 second apart 
      // and adding 2 seconds to the interval each retry. 
      var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)); 

      // Define your retry policy using the retry strategy and the Windows Azure storage 
      // transient fault detection strategy. 
      var retryPolicy = new RetryPolicy<MyRetryStrategy >(retryStrategy); 


      try 
      { 
       // Do some work that may result in a transient fault. 
       retryPolicy.ExecuteAction(service.AMethodIDefinedInMyService); 
      } 
      catch (Exception exception) 
      { 
       // All the retries failed. 
       writer.WriteLine("An Exception has been thrown:\n{0}", exception); 
      } 
      writer.WriteLine("End sample: RetryPolityUsingCode"); 
     }