实体框架 - SQL Azure 重试策略

发布于 2024-11-01 17:52:23 字数 41 浏览 3 评论 0原文

请任何人指导我如何使用 EF 到 SQL Azure 实施重试策略。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抱着落日 2024-11-08 17:52:23

我正在使用 Transiet 故障处理框架,EF 团队提供了更好的解决方案。

  • 将二进制文件或上面链接中的项目添加到您的解决方案中,并将引用添加到您的项目中。
  • 使用合适的参数实例化重试策略:
    var retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(
            10, 
            TimeSpan.FromSeconds(0.5), 
            TimeSpan.FromSeconds(2)
    ) { FastFirstRetry = true };
  • 使用重试策略对象来执行上下文上的任何原子工作。
    using(var context = new ... )
    {
        ...//Maybe you do something to the database...
        retryPolicy.ExecuteAction(() => context.SaveChanges());
    }

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());
    }
甜心小果奶 2024-11-08 17:52:23

Entity Framework 6 添加了连接弹性作为一项新功能功能可以帮助解决这个问题,引用 Microsoft 的话:“启用暂时连接故障的自动恢复”。如果您想了解更多信息,请参阅EF6 连接弹性规范

NuGet 上的 EF6

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

汐鸠 2024-11-08 17:52:23

根据大多数文档,使用瞬态故障处理库的问题是它迫使您在应用程序中包装每个数据库调用。

如果您使用 Entity Framework 6(目前处于 alpha 版本),则有一些新的内置支持对 Azure SQL 数据库的瞬时重试(需要一些配置):这里是 link

我创建了一个库,允许您配置实体框架以重试使用故障处理块,而无需更改每个数据库调用 - 通常您只需要更改配置文件,可能还需要更改一两行代码。

这允许您将其用于 Entity Framework 或 Linq To Sql,这里是链接

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

一桥轻雨一伞开 2024-11-08 17:52:23

此 Azure 论坛主题包含一些涵盖该主题的优质资源的链接。似乎还没有任何“官方”消息。但有一些开源项目可以为您提供一个良好的开端。

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

从答案:

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

我个人没有使用博客提到的库。相反,我能够使用带有 TRY/CATCH 的简单 WHILE 循环来监视可以安全重试的特定 SQL EXCEPTION 错误号。还有一个计数器基本上可以防止它永远“重试”。

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.

春庭雪 2024-11-08 17:52:23

Windows Server AppFabric 客户咨询团队在 这篇博文

基本上,他们有多种不同的方法来使用瞬态故障处理框架(该框架已被 瞬态故障处理应用程序块,这是类似的)以便提供重试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文