gpt4 book ai didi

c# - 陷阱 SQL 故障转移异常

转载 作者:行者123 更新时间:2023-12-05 08:09:14 33 4
gpt4 key购买 nike

我正在使用 NuGet 包 Polly实现捕获故障转移 SQL 异常的重试逻辑。我在 Azure 中设置了 SQL Server Always On 高可用性。

我不想捕获所有 SQL 异常(这是不正确的),而是想捕获发生故障转移时发生的特定 SQL 异常。

在 SSMS 中,我显示了仪表板,然后我能够人为地触发故障转移来测试我的代码。最初,我让所有异常冒泡(所以没有陷阱)。然后我将故障转移排队并查看我的日志以查看引发了什么 SQL 异常。随后,我能够捕获所有因故障转移而发生的 SQL 异常。

我的问题是,这是一份全面的 list 吗?在 SQL Server 故障转移上实现重试逻辑的其他人是否会捕获任何其他 SQL 异常?

我已经用逻辑尝试了将近 100 次故障转移,但没有任何问题发生。这当然并不意味着我已经捕获了全部故障转移 SQL 异常。

重试逻辑有Policy.Handle<SqlException>(se => IsFailoverSqlException(se)) .根据故障转移排队时我在代码中的位置,我看到了下面捕获的三个 SQL 异常。

    private static bool IsFailoverSqlException(SqlException se)
{
return (
/*
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
*/
(se.Class == 20 && se.State == 0 && se.Number == 53) ||
/*
Failed while logging Fatal to MT Database: Unable to access availability database 'MedicusMT' because the database replica is not in the PRIMARY or SECONDARY role.
Connections to an availability database is permitted only when the database replica is in the PRIMARY or SECONDARY role.
Try the operation again later.
*/
(se.Class == 14 && se.State == 1 && se.Number == 983) ||
// A transport-level error has occurred when receiving results from the server. (provider: Session Provider, error: 19 - Physical connection is not usable)
(se.Class == 20 && se.State == 0 && se.Number == -1)
);
}

最佳答案

使用 NET 4.5 及更高版本时行为发生了变化。 SqlException Number may be a Win32 API number What I found was error number depends on the network library used in the SQL Server connection.

命名管道库的值很好。与 Azure 库通信时,有一个 transient 错误需要处理。不要在 Azure 案例上重复一个好的 analysis。Windows 主机上的 TCP 网络库将出现 Win32 内部异常 1225。错误编号:1225,状态:0,类:20

我不喜欢 SqlException Net45 更改的是 SqlException.Number 是值的 mixed domain。它可以是来自底层网络库的 SQL Server ErrorAzure Transient Error 或 Win32 API。

如果您在 Unit of work 级别应用策略,请考虑重试死锁受害者 SQL Server 错误 1205

如果您进行实时升级,您可能会发现 DBA 会终止 PSID 错误编号:596,状态:1,类:21

关于c# - 陷阱 SQL 故障转移异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40029034/

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