我正在利用 NuGet 包Polly来实现捕获故障转移 SQL 异常的重试逻辑。我在 Azure 中设置了 SQL Server Always On High Availability。
我不想捕获所有 SQL 异常(这将是不正确的),而是希望捕获发生故障转移时发生的特定 SQL 异常。
从 SSMS 中,我显示仪表板,然后我能够人为地触发故障转移来测试我的代码。最初,我让所有异常都冒泡(所以没有陷阱)。然后我会排队进行故障转移并查看我的日志以查看引发了什么 SQL 异常。随后,我能够捕获由于故障转移而发生的所有 SQL 异常。
我的问题是,这是一份完整的清单吗?在 SQL Server 故障转移上实现重试逻辑的其他人是否会捕获任何其他 SQL 异常?
我已经用逻辑尝试了将近 100 次故障转移,但没有出现任何问题。这当然并不意味着我已经捕获了全部的故障转移 SQL 异常。
重试逻辑有 Policy.Handle(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)
);
}