Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - SQL Azure Retry Policy

Could anyone guide me how to implement a retry policy with EF to SQL Azure, please.

like image 301
Nil Pun Avatar asked Apr 18 '11 13:04

Nil Pun


2 Answers

I am using the Transiet Fault Handling Framework, provided in lue of a better solution by EF team.

  • Add the binary, or the project in the link above to your solution, and add the reference to your project.
  • Instantiate a retry policy with suitable parameters:

    var retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(
            10, 
            TimeSpan.FromSeconds(0.5), 
            TimeSpan.FromSeconds(2)
    ) { FastFirstRetry = true };
  • Use your retry policy object for any atomic work on the context.

    using(var context = new ... )
    {
        ...//Maybe you do something to the database...
        retryPolicy.ExecuteAction(() => context.SaveChanges());
    }
like image 126
Gleno Avatar answered Oct 18 '22 14:10

Gleno


Entity Framework 6 has added connection resiliency as a new feature to help address this, quoting from Microsoft: "enables automatic recovery from transient connection failures". Here is the Connection Resiliency Spec for EF6 if you want to learn more.

EF6 at NuGet

like image 23
Bern Avatar answered Oct 18 '22 15:10

Bern