简体   繁体   English

SQL Azure数据库重试逻辑

[英]SQL Azure Database retry logic

I have implemented the following code for handling INSERT/UPDATE retry logic with exponential backoff when writing to an Azure Database. 我已经实现了以下代码,用于在写入Azure数据库时使用指数退避处理INSERT / UPDATE重试逻辑。

static SqlConnection TryOpen(this SqlConnection connection)
{
  int attempts = 0;
  while (attempts < 5)
  {
    try
    {
      if (attempts > 0)
       System.Threading.Thread.Sleep(((int)Math.Pow(3, attempts)) * 1000);
      connection.Open();
      return connection;
    }
    catch { }
    attempts++;
  }
  throw new Exception("Unable to obtain a connection to SQL Server or SQL Azure.");
}

However should I consider applying retry logic for my database reads as well? 但是,我是否应该考虑为我的数据库读取应用重试逻辑? Or would the SqlCommand.CommandTimeout() method suffice? 或者SqlCommand.CommandTimeout()方法是否足够? Most of my reads are instituted using the following code: 我的大多数读取都是使用以下代码创建的:

Dim myDateAdapter As New SqlDataAdapter(mySqlCommand)
Dim ds As New DataSet
myDateAdapter.Fill(ds, "dtName")

It's hard to know what sort of transient errors will occur in a production environment with Azure so I am trying to do as much mitigation as possible now. 很难知道在Azure的生产环境中会发生什么样的瞬态错误,所以我现在尝试尽可能多地进行缓解。

I think retries are going to be part of your Windows Azure SQL Database operations in general. 我认为重试将成为您的Windows Azure SQL数据库操作的一部分。

Rather than implementing a custom solution, have you looked at the transient fault handling application block published by Microsoft Patterns and Practices, specifically for SQL Database? 您是否考虑 Microsoft模式和实践发布的瞬态故障处理应用程序块 ,而不是实现自定义解决方案,特别是针对SQL数据库?

Connection failures in SQL Azure are common. SQL Azure中的连接失败很常见。 This is because your application will create a connection pool but while your side thinks these connections are over, Azure could terminate them at their end and you will never know about it. 这是因为您的应用程序将创建一个连接池,但是当您的一方认为这些连接已经结束时,Azure可能会终止它们并且您永远不会知道它。

They do this for valid reasons such as a particular instance has become overloaded and they are transferring connections to another one. 他们这样做是出于正当理由,例如特定实例已经过载并且他们正在将连接转移到另一个实例。 With in-house SQL servers you generally never get this problem because your SQL Servers are always available and dedicated for your use. 使用内部SQL服务器,您通常不会遇到此问题,因为您的SQL Server始终可用并专用于您的使用。

As an example, I get about 5 connection failures with SQL Azure on about 100,000 database queries in a day. 例如,我在一天内就大约100,000个数据库查询与SQL Azure发生了大约5次连接失败。

It's going to happen with SQL Azure. 这将发生在SQL Azure中。 If you are using ADO.NET then David's suggestion of transient fault handling is the way to go. 如果您正在使用ADO.NET,那么David建议的瞬态故障处理是可行的方法。

If you are going to use Entity Framework, there is good news and bad news: Transient Fault Handling with SQL Azure using Entity Framework 如果您打算使用Entity Framework,那么有好消息和坏消息: 使用Entity Framework进行SQL Azure的瞬态故障处理

I have implemented SqlConnection and SqlCommand extension methods providing retry logic. 我已经实现了提供重试逻辑的SqlConnectionSqlCommand扩展方法。 It is available on NuGet . 它可以在NuGet上获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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