我正在使用NuGet软件包Polly来实现重试逻辑,以捕获故障转移SQL异常。我在Azure中设置了SQL Server Always On高可用性。
与其捕获所有SQL异常(这是不正确的),我想捕获发生故障转移时特定的SQL异常。
从SSMS开始,我显示仪表板,然后能够人为地触发故障转移以测试我的代码。最初,我让所有异常都冒泡(所以没有捕获)。然后我会排队一个故障转移,并查看我的日志以查看引发了哪个SQL异常。随后,我能够捕获由于故障转移而发生的所有SQL异常。
我的问题是,这是一个全面的列表吗?其他实施SQL Server故障转移重试逻辑的人是否还会捕获其他SQL异常?
我已经尝试了接近100次的故障转移逻辑,没有任何异常冒泡。当然,这并不意味着我已经捕获了所有故障转移SQL异常。
与其捕获所有SQL异常(这是不正确的),我想捕获发生故障转移时特定的SQL异常。
从SSMS开始,我显示仪表板,然后能够人为地触发故障转移以测试我的代码。最初,我让所有异常都冒泡(所以没有捕获)。然后我会排队一个故障转移,并查看我的日志以查看引发了哪个SQL异常。随后,我能够捕获由于故障转移而发生的所有SQL异常。
我的问题是,这是一个全面的列表吗?其他实施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)
);
}