6

我是一个 vb.net 人,阅读 C# 有困难。我将 C# Dapper 编译为 DLL 并在我的应用程序中使用它。我主要担心的是我认为我需要修改源以在每个 SQL 查询中默认集成 SQL Azure 的瞬态故障处理框架。

我可以在连接级别添加重试逻辑,因为它位于 dapper 之上,但不是在嵌入在 drapper 类中的执行查询级别。

有人做过吗?

* 更新 *

在 Dapper 调用之上仅使用 ReliableSqlConnection 会处理执行非查询的重试逻辑吗?

这是使用瞬态故障处理从 MS 重试的示例代码

using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.AzureStorage;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;
using System.Data;

...

using (ReliableSqlConnection conn = new ReliableSqlConnection(connString, retryPolicy))
{
conn.Open();

IDbCommand selectCommand = conn.CreateCommand();
selectCommand.CommandText = 
  "UPDATE Application SET [DateUpdated] = getdate()";

// Execute the above query using a retry-aware ExecuteCommand method which 
// will automatically retry if the query has failed (or connection was 
// dropped).
int recordsAffected = conn.ExecuteCommand(selectCommand, retryPolicy);

}

这是 Dapper 代码的执行部分,使用了相同的名称,但我猜它是自定义执行函数

    private static int ExecuteCommand(IDbConnection cnn, IDbTransaction transaction, string sql, Action<IDbCommand, object> paramReader, object obj, int? commandTimeout, CommandType? commandType)
    {
        IDbCommand cmd = null;
        bool wasClosed = cnn.State == ConnectionState.Closed;
        try
        {
            cmd = SetupCommand(cnn, transaction, sql, paramReader, obj, commandTimeout, commandType);
            if (wasClosed) cnn.Open();
            return cmd.ExecuteNonQuery();
        }
        finally
        {
            if (wasClosed) cnn.Close();
            if (cmd != null) cmd.Dispose();
        }
    }
4

1 回答 1

3

我建议将重试包装在 Dapper周围,最好使用 RetryPolicy.ExecuteAction 方法。这样,对连接的 OPEN 调用和命令本身都将使用 TFH 重试策略进行重试:

例如:

        SqlRetryPolicy.ExecuteAction(() =>
        {
            // Place Dapper ExecuteCommand here: e.g.
            ExecuteCommand(conn,  trans, ... )
        });
于 2014-05-21T22:39:34.510 回答