如何重用code,它重新打开连接? [英] How to reuse code that reopens connection?

查看:106
本文介绍了如何重用code,它重新打开连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的生产服务器杀死活动的连接,所以我们的API需要在需要的时候恢复。下面code的作品,但它是非常重复的:

 私人const int的MaxRetryCount = 3;

    公共静态SqlDataReader的RestoreConnectionAndExecuteReader(SqlCommand的命令)
    {
        INT retryCount = 0;

        而(retryCount ++< MaxRetryCount)
        {
            尝试
            {
                如果(command.Connection.State == ConnectionState.Closed)
                    command.Connection.Open();
                返回Command.ExecuteReader却();
            }
            赶上(例外五)
            {
                如果(!e.Message.ToLower()。包含(发生传输级错误))
                {
                    扔;
                }
            }
        }
        抛出新的异常(无法还原命令连接:+ command.CommandText);
    }

    公共静态无效RestoreConnectionAndExecuteNonQuery(SqlCommand的命令)
    {
        变种retryCount = 0;
        而(retryCount ++< MaxRetryCount)
        {
            尝试
            {
                如果(command.Connection.State == ConnectionState.Closed)
                    command.Connection.Open();
                command.ExecuteNonQuery();
                返回;
            }
            赶上(例外五)
            {
                如果(!e.Message.ToLower()。包含(发生传输级错误))
                {
                    扔;
                }
            }
        }
        抛出新的异常(无法还原命令连接:+ command.CommandText);
    }
 

我怎么能重构我的code和消除重复?我需要保持这些方法的签名,因为它们都用遍了该系统。

解决方案

 私人const int的MaxRetryCount = 3;

公共静态牛逼RestoreConnectionAndExecute< T>(SqlCommand的命令,函数功能:LT; SqlCommand的,T> FUNC)
{
    INT retryCount = 0;

    而(retryCount ++< MaxRetryCount)
    {
        尝试
        {
            如果(command.Connection.State == ConnectionState.Closed)
                command.Connection.Open();
            返回FUNC(命令);
        }
        赶上(例外五)
        {
            如果(!e.Message.ToLower()。包含(发生传输级错误))
            {
                扔;
            }
        }
    }
    抛出新的异常(无法还原命令连接:+ command.CommandText);

}

公共静态SqlDataReader的RestoreConnectionAndExecuteReader(SqlCommand的命令)
{
    返回RestoreConnectionAndExecute(命令,C => c.ExecuteReader());
}

公共静态INT RestoreConnectionAndExecuteNonQuery(SqlCommand的命令)
{
    返回RestoreConnectionAndExecute(命令,C => c.ExecuteNonQuery());
}
 

Our production server kills inactive connections, so our API needs to restore them when they are needed. The following code works, but it is very repetitive:

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

How could I refactor my code and eliminate duplication? I need to keep the signatures of these methods, as they are used all over the system.

解决方案

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

这篇关于如何重用code,它重新打开连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆