首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RetryPolicy.Retrying事件在SQL的瞬态错误处理过程中不触发?

RetryPolicy.Retrying事件在SQL的瞬态错误处理过程中不触发?
EN

Stack Overflow用户
提问于 2014-06-10 12:14:12
回答 1查看 690关注 0票数 1

我有托管在azure中的网站(作为一个web服务),它使用sql azure作为后端。

我的错误日志中填充了大量的临时网络和sql连接错误。

因此,我实现了企业库临时错误处理块。在测试中,它的操作似乎是正确的。

我遇到的问题是,我想记录发生重试逻辑的实例。从文档中看,RetryPolicy.Retrying似乎是我所追求的事件,但是在测试中它不会触发。在C#中有许多示例遵循以下模式触发此事件:

代码语言:javascript
复制
var retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(retryStrategy);
// Receive notifications about retries.
retryPolicy.Retrying += (sender, args) =>
{
    // Log details of the retry.
    var msg = String.Format("Retry - Count:{0}, Delay:{1}, Exception:{2}",
        args.CurrentRetryCount, args.Delay, args.LastException);
    Trace.WriteLine(msg, "Information");
};

我以为我已经正确地适应了这一点,但简而言之,下面的代码有什么问题吗?!

代码语言:javascript
复制
Private RetryManager As RetryManager
Private WithEvents RetryPolicy As RetryPolicy

Private Sub RetryPolicy_Retrying(ByVal sender As Object, ByVal args As RetryingEventArgs)
    ' Log details of the retry.
    Dim msg = String.Format("Retry - Count:{0}, Delay:{1}, Exception:{2}", args.CurrentRetryCount, args.Delay, args.LastException)
    Trace.TraceInformation(msg)
End Sub

Private Sub SetupRetryPolicy()
    'If its already set then lets not do it again
    If RetryPolicy Is Nothing Then
        RetryManager = EnterpriseLibraryContainer.Current.GetInstance(Of RetryManager)()
        RetryPolicy = RetryManager.GetRetryPolicy(Of SqlAzureTransientErrorDetectionStrategy)("Exponential Backoff Retry Strategy")
        ' connect sub as handler to event when retry occurs
        AddHandler RetryPolicy.Retrying, AddressOf RetryPolicy_Retrying
    End If
End Sub


Public Sub ExecuteAndDoStuff(ByVal connString As String, ByVal cmdText As String)

    SetupRetryPolicy()

    'get a connection with retry
    Using conn As New ReliableSqlConnection(connString, RetryPolicy, RetryPolicy)
        conn.Open()
        Using cmd As SqlCommand = conn.CreateCommand
            Try
                cmd.CommandText = cmdText
                ' this might be overkill, do I need to pass the retry policy in again for the command?
                Dim dr As SqlDataReader = cmd.ExecuteReaderWithRetry(RetryPolicy, RetryPolicy)
                '... do something with this datareader

            Catch ex As Exception
                'log error
                Trace.TraceError("Query failed to execute despite retry logic: " & ex.ToString)
                'continue to throw the error (picked up higher up the chain)
                Throw ex
            End Try

        End Using

    End Using

End Sub

我对这段代码中至少有一半是陌生的,但在任何人向我发出rtfm之前--我试过了!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-10 12:52:30

很难判断您的代码中是否有任何错误;可能根本没有检测到任何瞬态错误。您是如何确定是否存在短暂错误的?我要做的第一件事是确保您有一种可重复的方法来创建一个瞬态错误。

我设置测试的方式是在Azure SQL数据库中有一个1GB的数据库,填充数据直到达到其存储极限,然后尝试添加更多的数据(这每次都会产生一个短暂的错误)。

对于Azure SQL瞬态错误,需要记住两件事:

1)它们很难测试,因为它们中的许多依赖于超出您控制范围的变量;最容易复制的瞬态错误之一是空间(上面的建议)。

2)还有一些其他类型的错误可以触发,例如Azure中的路由器阻塞条件,这些错误不被认为是瞬态的;例如,SQL瞬态策略没有捕获IOException错误。因此,您要么需要单独解释这些错误,要么需要自定义策略以包含这些错误。catch块应该在当前实现中捕获这些错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24140714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档