locked
Retrying SaveChanges with Entity Framework DbContext in Sql Azure RRS feed

  • Question

  • I using entity framework 4.3.1 for an application running in Azure.  I am putting retry logic in place, and I have taken a look at the Sql CAT team blog entries as well as the patterns and practices transient fault handling block (I am using a RetryPolicy from this block).

    I'm not certain of the behavior of the DbContext on a failure of SaveChanges.  For simple changes involving a single DbContext, is it sufficient to retry SaveChanges, or must I reconstruct the state of the context object before resubmitting?  For example, given:

    using (var context = new CaseDb())
    {
       context.SetModified(existingCase);
       context.SaveChanges();
       return existingCase;
    }

    Could I simply extend SaveChanges to retry:

    using (var context = new CaseDb())
    {
        context.SetModified(existingCase);
        context.SaveChangesWithRetry();
        return existingCase;
    }
    
    //from CaseDb
    public int SaveChangesWithRetry()
    {
        try
        {
            var policy = GetRetryPolicy();
            return policy.ExecuteAction<int>(
                     () => base.SaveChanges());
        }
        catch (Exception e)
        {
           // log it
           throw;
        }
    }

    Or do I need to retry the entire operation:

    var strategy = new SqlAzureTransientErrorDetectionStrategy();
    RetryPolicy policy = new RetryPolicy(strategy, RetryStrategy.DefaultExponential);
    
    return policy.ExecuteAction( () => {
    
       using (var context = new CaseDb())
       {
          context.SetModified(existingCase);
          context.SaveChangesWithRetry();
          return existingCase;
       }
    
    });
    Thanks in advance for any help.

    • Moved by Alan_chen Friday, April 27, 2012 2:20 AM (From:ADO.NET Entity Framework and LINQ to Entities)
    • Moved by Arwind - MSFT Friday, April 27, 2012 6:43 AM (From:Windows Azure Development)
    Thursday, April 26, 2012 11:22 AM

Answers

All replies

  • Just realized there is an error in the last code block in the original post, I meant to say

    var strategy = new SqlAzureTransientErrorDetectionStrategy();
    RetryPolicy policy = new RetryPolicy(strategy, RetryStrategy.DefaultExponential);
    
    return policy.ExecuteAction( () => {
    
       using (var context = new CaseDb())
       {
          context.SetModified(existingCase);
          context.SaveChanges();
          return existingCase;
       }
    
    });

    Thursday, April 26, 2012 7:01 PM
  • Hi Chris Keyser,

    Please pay attention to the Case #2 – Retry Policy With Transaction Scope section to enroll the process into a transaction  to retry policy.

    TechNet Subscriber Support
    If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.


    Stephanie Lv

    TechNet Community Support

    • Marked as answer by Challen Fu Monday, May 7, 2012 9:11 AM
    Monday, April 30, 2012 9:12 AM