我已经实现了以下代码,用于在写入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()方法是否足够?我的大多数读取都是使用以下代码创建的:
很难知道在Azure的生产环境中会发生什么样的瞬态错误,所以我现在尝试尽可能多地进行缓解.