2013-03-14 27 views
0

在SQL Azure中使用瞬態故障處理應用程序塊。 在這個示例中,oCmd.ExecuteReader()的特定重試邏輯是必需的,還是ReliableSqlConnection負責處理?瞬態故障處理 - 打開的ReliableSqlConnection是否會在其管理下自動處理SQL命令重試?

Using oCmd As New SqlCommand() 
     strSQL = "SELECT xxxxxx.* FROM xxxxxx" 
     oCmd.CommandText = strSQL 
     Using oConn As New ReliableSqlConnection(Cs, retryPolicy) 
      oConn.Open() 
      oCmd.Connection = oConn 
      Using oDR As SqlDataReader = oCmd.ExecuteReader() 
       If oDR.Read() Then 
        sb1.Append(oDR("xxxxxx").ToString) 
       End If 
      End Using 
     End Using 
    End Using 

* UPDATE *

從下面的反應,如果我從ReliableSqlConnect對象的上下文創建SqlCommand對象,我可以期望有重試行爲擴展到命令也,如在規定此頁 http://geekswithblogs.net/ScottKlein/archive/2012/01/27/understanding-sql-azure-throttling-and-implementing-retry-logic.aspx

「下面的代碼示例說明了如何使用RetryPolicy類創建重試策略,指定重試嘗試和重試之間的固定時間,然後將此策略應用於Relia blSqlConnection作爲連接的策略以及策略到命令。「

RetryPolicy myretrypolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(3, TimeSpan.FromSeconds(30)); 

using (ReliableSqlConnection cnn = new ReliableSqlConnection(connString, myretrypolicy, myretrypolicy)) 
{ 
    try 
    { 
    cnn.Open(); 

    using (var cmd = cnn.CreateCommand()) 
    { 
     cmd.CommandText = "SELECT * FROM HumanResources.Employee"; 

     using (var rdr = cnn.ExecuteCommand<IDataReader>(cmd)) 
     { 
     // 
     } 
    } 
    } 
    catch (Exception ex) 
    { 
    MessageBox.Show(ex.Message.ToString()); 
    } 
} 

回答

0

ReliableSqlConnection僅將重試邏輯應用於建立連接的過程。嘗試使用這樣的事情:

Using oDR As SqlDataReader = oCmd.ExecuteReaderWithRetry(retryPolicy) 
      If oDR.Read() Then 
        sb1.Append(oDR("xxxxxx").ToString) 
      End If 
End Using 

有關使用情況的詳細信息您可以參考MSDN或有其他一些examples here

+0

好的,我讀了「這個策略然後被應用到ReliablSqlConnection作爲連接的策略以及策略到命令。」當編碼「使用(var cmd = cnn.CreateCommand())」意味着從sqlreliable連接對象創建命令將重試延長到命令。不是我想的,在sqlreliableconnection外部創建命令對象,所以我必須更改我的代碼模式。 – 2013-03-14 22:09:48