2013-03-14 61 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