2013-03-18 3 views
0

우리 응용 프로그램은 EF 5 및 C# .net 4.5를 사용하고 있습니다. 우리 응용 프로그램은 하나의 cenral 서버로 전 세계에서 사용되기 때문에 시간 초과로 실행되고 서버에 대한 연결이 끊어지는 문제가 있습니다.엔터티 프레임 워크 연결 손실 처리

현재 SaveChanges() 메소드를 catch하고 사용자가 취소 할 때까지 다시 시도합니다. 코드에서 수행하는 다양한로드에 대해 우리는 무엇을 할 수 있습니까?

편집 : 제안 된 솔루션을 시도했지만 내가 그것을 제대로 작동하지 않습니다

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

, 엔티티 프레임 워크는 나에게 EntityExceptions를 던졌습니다 내가 두 번째 코드 경로를 추가 있도록.

실제 사용법 :

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

MyRetryStrategy의 IsTransient 메소드가 호출되지 않습니다, 또한 어떤 시도가 호출되지 않습니다. 나는 전화하기 전에 데이터뱅크를 멈추게했다.

내가 뭘 잘못하고 있니?

답변

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"); 
     }