What is good C# coding style for catching SQLException and retrying

后端 未结 8 665
慢半拍i
慢半拍i 2020-12-05 02:55

I have a method that calls a SQLServer function to perform a free text search against a table. That function will occasionally on the first call result in a SQLException: \"

8条回答
  •  一生所求
    2020-12-05 03:44

    It's not good style, but sometimes you have to do it, because you simply can't change existing code and have to deal with it.

    I am using the following generic method for this scenario. Note the PreserveStackTrace() method, which can sometimes be very helpful in a re-throw scenario.

    public static void RetryBeforeThrow(Action action, int retries, int timeout) where T : Exception
    {
        if (action == null)
            throw new ArgumentNullException("action", string.Format("Argument '{0}' cannot be null.", "action"));
    
        int tries = 1;
    
        do
        {
            try
            {
                action();
                return;
            }
            catch (T ex)
            {
                if (retries <= 0)
                {
                    PreserveStackTrace(ex);
                    throw;
                }
    
                Thread.Sleep(timeout);
            }
        }
        while (tries++ < retries);
    }
    
    /// 
    /// Sets a flag on an  so that all the stack trace information is preserved 
    /// when the exception is re-thrown.
    /// 
    /// This is useful because "throw" removes information, such as the original stack frame.
    /// 
    public static void PreserveStackTrace(Exception ex)
    {
        MethodInfo preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic);
        preserveStackTrace.Invoke(ex, null);
    }
    

    You would call it like that:

    RetryBeforeThrow(() => MethodWhichFails(), 3, 100);
    

提交回复
热议问题