简体   繁体   中英

Entity Framework - SQL Azure Retry Policy

有人可以指导我如何使用EF到SQL Azure实现重试策略。

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

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

The problem with using the Transient Fault Handling library according to most of the documentation out there is that it forces you to wrap every database call in your application.

If you use Entity Framework 6 (currently in alpha) then there is some new in-built support for transient retries with Azure SQL Database (with a little bit of configuration): here is the link

I've created a library which allows you to configure Entity Framework to retry using the Fault Handling block without needing to change every database call - generally you will only need to change your config file and possibly one or two lines of code.

This allows you to use it for Entity Framework or Linq To Sql, here is the link

This Azure Forum thread has some links to good resources that cover this topic. There doesn't seem to be anything 'official' quite yet. But there are some open source projects that give you a pretty good start.

http://social.msdn.microsoft.com/Forums/en-US/ssdsgetstarted/thread/3a9ed384-5374-438e-a8a4-ff4bd8000738/#27b5251a-bff5-4282-980c-ad43fdd85591

From the answer:

http://blogs.msdn.com/b/appfabriccat/archive/2010/10/28/best-practices-for-handling-transient-conditions-in-sql-azure-client-applications.aspx

I personally didn't use the library the blog mentions. Instead I was able to get away with a simple WHILE LOOP with a TRY/CATCH that watched for the specific SQL EXCEPTION Error Numbers which were safe to retry. There is also a counter which basically prevents it from 'retrying' forever.

The Windows Server AppFabric Customer Advisory Team have provided some fairly detailed guidance around retries in this blog post .

Basically, they've got a number of different ways of using the Transient Fault Handling Framework (which has been since superseded by the Transient Fault Handling Application Block , which is similar) in order to provide retries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM