SQL Azure数据库重试逻辑

前端之家收集整理的这篇文章主要介绍了SQL Azure数据库重试逻辑前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经实现了以下代码,用于在写入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.");
}

但是我应该考虑为我的数据库读取应用重试逻辑吗?或者sqlCommand.CommandTimeout()方法是否足够?我的大多数读取都是使用以下代码创建的:

Dim myDateAdapter As New sqlDataAdapter(MysqLCommand)
Dim ds As New DataSet
myDateAdapter.Fill(ds,"dtName")

很难知道在Azure的生产环境中会发生什么样的瞬态错误,所以我现在尝试尽可能多地进行缓解.

解决方法

我认为重试将成为您的Windows Azure sql数据库操作的一部分.

您是否看过Microsoft模式和实践发布的transient fault handling application block,而不是实现自定义解决方案,特别是sql数据库

原文链接:https://www.f2er.com/mssql/83272.html

猜你在找的MsSQL相关文章