Exemple #1
0
        public static async Task <int> SqlExecuteNonQueryWithRetryAsync(NpgsqlConnection connection, NpgsqlCommand sqlCmd, Func <RetryCheckParameter, bool> canRetry, bool ignoreInsertPKException = false)
        {
            RetryCheckParameter retryParamenter = new RetryCheckParameter()
            {
                EndRetryTime = DateTime.UtcNow,
                RetryCount   = 0
            };

            sqlCmd.Connection = connection;
label_1:
            try
            {
                await SqlSessionStateRepositoryUtil.OpenConnectionAsync(connection);

                return((int)await sqlCmd.ExecuteNonQueryAsync());
            }
            catch (NpgsqlException ex)
            {
                //if (SqlSessionStateRepositoryUtil.IsInsertPKException(ex, ignoreInsertPKException))
                //    return -1;
                retryParamenter.Exception = ex;
                if (!canRetry(retryParamenter))
                {
                    throw;
                }
                else
                {
                    goto label_1;
                }
            }
        }
Exemple #2
0
        public static async Task <NpgsqlDataReader> SqlExecuteReaderWithRetryAsync(NpgsqlConnection connection,
                                                                                   NpgsqlCommand sqlCmd,
                                                                                   Func <RetryCheckParameter, bool> canRetry, CommandBehavior cmdBehavior = CommandBehavior.Default)
        {
            RetryCheckParameter retryParamenter = new RetryCheckParameter()
            {
                EndRetryTime = DateTime.UtcNow,
                RetryCount   = 0
            };

            sqlCmd.Connection = connection;
label_1:
            NpgsqlDataReader sqlDataReader;

            try
            {
                await SqlSessionStateRepositoryUtil.OpenConnectionAsync(connection);

                sqlDataReader = (NpgsqlDataReader)await sqlCmd.ExecuteReaderAsync(cmdBehavior);
            }
            catch (NpgsqlException ex)
            {
                retryParamenter.Exception = ex;
                if (!canRetry(retryParamenter))
                {
                    throw;
                }
                else
                {
                    goto label_1;
                }
            }
            return(sqlDataReader);
        }
Exemple #3
0
 private bool CanRetry(RetryCheckParameter parameter)
 {
     if (this._retryIntervalMilSec <= 0 || !SqlSessionStateRepositoryUtil.IsFatalSqlException(parameter.Exception) || parameter.RetryCount >= this._maxRetryNum)
     {
         return(false);
     }
     Thread.Sleep(this._retryIntervalMilSec);
     ++parameter.RetryCount;
     return(true);
 }
 private bool CanRetry(RetryCheckParameter parameter)
 {
     if (parameter.RetryCount >= this._maxRetryNum || !this.ShouldUseInMemoryTableRetry(parameter.Exception))
     {
         return(false);
     }
     Thread.Sleep(this._retryIntervalMilSec);
     ++parameter.RetryCount;
     return(true);
 }