我想为我的数据库调用编写一些包装代码(使用C#和Microsoft技术访问数据库),自动重试"瞬态"异常.通过瞬态,我的意思是有一个很好的机会最终会解决(对于逻辑错误,永远不会工作).我能想到的例子包括:
我曾计划使用SqlException的错误号来发现这些.例如:
List<RunStoredProcedureResultType> resultSet = null;
int limit = 3;
for (int i = 0; i < limit; ++i)
{
bool isLast = i == limit - 1;
try
{
using (var db = /* ... */)
{
resultSet = db.RunStoredProcedure(param1, param2).ToList();
}
//if it gets here it was successful
break;
}
catch (SqlException ex)
{
if (isLast)
{
//3 transient errors in a row. So just kill it
throw;
}
switch (ex.Number)
{
case 1205: //deadlock …Run Code Online (Sandbox Code Playgroud)