加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQL Azure数据库重试逻辑

发布时间:2020-12-12 16:22:56 所属栏目:MsSql教程 来源:网络整理
导读:我已经实现了以下代码,用于在写入Azure数据库时使用指数退避处理INSERT / UPDATE重试逻辑. static SqlConnection TryOpen(this SqlConnection connection){ int attempts = 0; while (attempts 5) { try { if (attempts 0) System.Threading.Thread.Sleep(((i
我已经实现了以下代码,用于在写入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数据库?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读