2014-06-10 24 views
1

我有网站托管在Azure(作为网络服务),因为它是后端使用SQL Azure。在SQL Azure的瞬态错误处理期间RetryPolicy.Retrying事件不会触发?

我的错误日志已被填充很多看起来是瞬态网络和sql连接错误。

因此,我实施了企业库瞬态错误处理块。在测试中,它似乎可以正常运行。

我遇到的问题是,我想记录此重试逻辑发生的情况。从文档RetryPolicy.Retrying看起来是我后来的事件,但在测试中它不会触发。有大量的在C#示例遵循下面的模式来触发此事件:

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"); 
}; 

我想我正确地适应这一点,但总之,有什么不对下面的代码?

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 

我完全新在这个代码块是怎么回事,至少有一半的人,但我thows一个RTFM前 - 我试过!

+0

我注意到事件在瞬态错误发生时没有触发,你有没有解决它? –

回答

1

很难判断代码中是否有错误;可能是因为根本没有发现瞬态错误。你如何确定存在瞬态错误?我会做的第一件事是确保你有一个可重复的方法来创建一个瞬态错误。

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

有两两件事要记住使用SQL Azure的瞬态错误:

1)他们是很难测试,因为其中许多取决于是你无法控制的变量;最容易发生的瞬态错误之一是空间不足(上面的建议)

2)还有其他几种类型的错误可以触发,例如Azure中的路由器swicthing条件,这些错误不被视为瞬态;例如IOException错误不会被SQL瞬态策略捕获。因此,您需要分别考虑这些类型的错误,或者自定义策略以包含这些错误。你的catch块应该在你当前的实现中捕获这些错误。

+0

与1GB分贝完全把戏的好主意 –

相关问题