Csharp/C#教程:如何重用重新打开连接的代码?分享


如何重用重新打开连接的代码?

我们的生产服务器会终止非活动连接,因此我们的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); } 

我怎么能重构我的代码并消除重复? 我需要保留这些方法的签名,因为它们在整个系统中使用。

 private const int MaxRetryCount = 3; public static T RestoreConnectionAndExecute(SqlCommand command, Func 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()); } 

 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); } 

这些方法的共同部分是连接检索过程。 您可以创建一个新的静态方法,该方法负责调用例如以命令作为参数的检索连接,如果在其他情况下建立连接则返回它会引发exception。

在代码示例中有一些我不喜欢的东西。 但是,要明确回答有关删除重复的问题 – 将公共代码提取到接受委托的方法中。

 private TReturn RestoreConnectionAndExecute(SqlCommand command, Func 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()); } 

不过,你应该重新思考一些事情。 包含:

上述就是C#学习教程:如何重用重新打开连接的代码?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1017161.html

(0)
上一篇 2022年1月1日
下一篇 2022年1月1日

精彩推荐