2014-06-10 35 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分貝完全把戲的好主意 –

相關問題