我们的生产服务器会终止非活动连接,因此我们的API需要在需要时恢复它们。以下代码有效,但它非常重复:
private const int MaxRetryCount = 3;
public static SqlDataReader RestoreConnectionAndExecuteReader(SqlCommand command)
{
int retryCount = 0;
while (retryCount++ < MaxRetryCount)
{
try
{
if (command.Connection.State == ConnectionState.Closed)
command.Connection.Open();
return command.ExecuteReader();
}
catch(Exception e)
{
if(!e.Message.ToLower().Contains("transport-level error has occurred"))
{
throw;
}
}
}
throw new Exception("Failed to restore connection for command:"+command.CommandText);
}
public static void RestoreConnectionAndExecuteNonQuery(SqlCommand command)
{
var retryCount = 0;
while(retryCount++ < MaxRetryCount)
{
try
{
if (command.Connection.State == ConnectionState.Closed)
command.Connection.Open();
command.ExecuteNonQuery();
return;
}
catch(Exception e)
{
if (!e.Message.ToLower().Contains("transport-level error has occurred"))
{
throw;
}
}
}
throw new Exception("Failed to restore connection for command:" + command.CommandText);
}
我如何重构代码并消除重复?我需要保留这些方法的签名,因为它们在整个系统中使用。
答案 0 :(得分:5)
private const int MaxRetryCount = 3;
public static T RestoreConnectionAndExecute<T>(SqlCommand command, Func<SqlCommand, T> func)
{
int retryCount = 0;
while (retryCount++ < MaxRetryCount)
{
try
{
if (command.Connection.State == ConnectionState.Closed)
command.Connection.Open();
return func(command);
}
catch(Exception e)
{
if(!e.Message.ToLower().Contains("transport-level error has occurred"))
{
throw;
}
}
}
throw new Exception("Failed to restore connection for command:"+command.CommandText);
}
public static SqlDataReader RestoreConnectionAndExecuteReader(SqlCommand command)
{
return RestoreConnectionAndExecute(command, c => c.ExecuteReader());
}
public static int RestoreConnectionAndExecuteNonQuery(SqlCommand command)
{
return RestoreConnectionAndExecute(command, c => c.ExecuteNonQuery());
}
答案 1 :(得分:1)
private const int MaxRetryCount = 3;
public static SqlDataReader RestoreConnectionAndExecuteReader(SqlCommand command)
{
return RestoreConnectionAndExecuteQueryHelper(command, true);
}
public static void RestoreConnectionAndExecuteNonQuery(SqlCommand command)
{
RestoreConnectionAndExecuteQueryHelper(command, false);
}
private static SqlDataReader RestoreConnectionAndExecuteQueryHelper(SqlCommand command, bool returnReader)
{
var retryCount = 0;
while (retryCount++ < MaxRetryCount)
{
try
{
if (command.Connection.State == ConnectionState.Closed)
command.Connection.Open();
if (returnReader)
{
return command.ExecuteReader();
}
else
{
command.ExecuteNonQuery();
return null;
}
}
catch (Exception e)
{
if (!e.Message.ToLower().Contains("transport-level error has occurred"))
{
throw;
}
}
}
throw new Exception("Failed to restore connection for command:" + command.CommandText);
}
答案 2 :(得分:0)
这些方法的共同部分是连接检索过程。您可以创建一个新的静态方法,该方法负责调用例如以命令作为参数的检索连接,如果在其他情况下建立连接则返回它,则抛出异常。
答案 3 :(得分:0)
在代码示例中有一些我不喜欢的东西。但是,要明确回答关于删除重复的问题 - 将公共代码提取到需要委托的方法中。
private TReturn RestoreConnectionAndExecute<T>(SqlCommand command, Func<SqlCommand, TReturn> execute)
{
int retryCount = 0;
while (retryCount++ < MaxRetryCount)
{
try
{
if (command.Connection.State == ConnectionState.Close)
command.Connection.Open();
return execute(command);
}
catch(Exception e)
{
...
}
}
public SqlDataReader RestoreConnectionAndExecuteReader(SqlCommand command)
{
return this.RestoreConnectionAndExecute(command, c => c.ExecuteReader());
}
public void RestoreConnectionAndExecuteNonQuery(SqlCommand command)
{
// Ignore return
this.RestoreConnectionAndExecute(command, c => c.ExecuteNonQuery());
}
但你应该重新考虑一些事情。包括: