Entity Framework - SQL Azure Retry Policy

前端 未结 5 1398
春和景丽
春和景丽 2020-12-31 05:20

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

相关标签:
5条回答
  • 2020-12-31 05:49

    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

    0 讨论(0)
  • 2020-12-31 05:57

    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

    0 讨论(0)
  • 2020-12-31 06:00

    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.

    0 讨论(0)
  • 2020-12-31 06:02

    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());
        }
    
    0 讨论(0)
  • 2020-12-31 06:05

    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.

    0 讨论(0)
提交回复
热议问题