gpt4 book ai didi

azure-sql-database - 如何为 Dapper 实现 SQL Azure transient 故障处理框架?

转载 作者:行者123 更新时间:2023-12-03 23:41:13 26 4
gpt4 key购买 nike

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

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

有人做过吗?

* 更新 *

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

这里是 MS 重试的示例代码,带有 transietn 故障处理

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

最佳答案

我建议围绕 Dapper 进行重试,最好使用 RetryPolicy.ExecuteAction 方法。这样既对连接的 OPEN 调用
并且命令本身将使用 TFH 重试策略重试:

例如:

        SqlRetryPolicy.ExecuteAction(() =>
{
// Place Dapper ExecuteCommand here: e.g.
ExecuteCommand(conn, trans, ... )
});

关于azure-sql-database - 如何为 Dapper 实现 SQL Azure transient 故障处理框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15372188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com