Exemplo n.º 1
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);
        }
Exemplo n.º 2
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;
                }
            }
        }
Exemplo n.º 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);
 }
Exemplo n.º 4
0
        public async Task <SessionItem> GetSessionStateItemAsync(string id, bool exclusive)
        {
            TimeSpan lockAge = TimeSpan.Zero;
            DateTime utcNow  = DateTime.UtcNow;

            byte[] buf = (byte[])null;
            SessionStateActions actions = SessionStateActions.None;
            NpgsqlCommand       cmd     = (NpgsqlCommand)null;

            cmd = !exclusive?this._commandHelper.CreateGetStateItemCmd(PostgresSessionStateRepository.GetStateItemSql, id) : this._commandHelper.CreateGetStateItemExclusiveCmd(PostgresSessionStateRepository.GetStateItemExclusiveSql, id);

            using (NpgsqlConnection connection = new NpgsqlConnection(this._connectString))
            {
                NpgsqlDataReader reader = (NpgsqlDataReader)await SqlSessionStateRepositoryUtil.SqlExecuteReaderWithRetryAsync(connection, cmd,
                                                                                                                               new Func <RetryCheckParameter, bool>(this.CanRetry), CommandBehavior.Default);

                int lockId = 0;
                try
                {
                    if (reader.ReadAsync().Result)
                    {
                        buf     = (byte[])reader["sessionitemlong"];
                        actions = (SessionStateActions)reader["flags"];
                        lockId  = (int)reader["lockcookie"];
                        //lockAge = 0;
                    }
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                }
                reader = (NpgsqlDataReader)null;
                //  NpgsqlParameter putParameterValue = cmd.GetOutPutParameterValue(SqlParameterName.Locked);
                //  if (putParameterValue == null || Convert.IsDBNull(putParameterValue.Value))
                //      return (SessionItem)null;
                //  int num = (bool)putParameterValue.Value ? 1 : 0;
                ////  object lockId = (object)(int)cmd.GetOutPutParameterValue(SqlParameterName.LockCookie).Value;
                //  if (num != 0)
                //  {
                //      lockAge = new TimeSpan(0, 0, (int)cmd.GetOutPutParameterValue(SqlParameterName.LockAge).Value);
                //      if (lockAge > new TimeSpan(0, 0, 31536000))
                //          lockAge = TimeSpan.Zero;
                //      return new SessionItem((byte[])null, true, lockAge, lockId, actions);
                //  }
                //  actions = (SessionStateActions)cmd.GetOutPutParameterValue(SqlParameterName.ActionFlags).Value;
                //  if (buf == null)
                //      buf = (byte[])cmd.GetOutPutParameterValue(SqlParameterName.SessionItemLong).Value;
                return(new SessionItem(buf, true, lockAge, lockId, actions));
            }
        }
Exemplo n.º 5
0
 public void DeleteExpiredSessions()
 {
     using (NpgsqlConnection connection = new NpgsqlConnection(this._connectString))
     {
         NpgsqlCommand expiredSessionsCmd = this._commandHelper.CreateDeleteExpiredSessionsCmd(PostgresSessionStateRepository.DeleteExpiredSessionsSql);
         ConfiguredTaskAwaitable <int> configuredTaskAwaitable = SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, expiredSessionsCmd, new Func <RetryCheckParameter, bool>(this.CanRetry), false).ConfigureAwait(false);
         // ISSUE: explicit reference operation
         ConfiguredTaskAwaitable <int> .ConfiguredTaskAwaiter awaiter = ((ConfiguredTaskAwaitable <int>)@configuredTaskAwaitable).GetAwaiter();
         // ISSUE: explicit reference operation
         ((ConfiguredTaskAwaitable <int> .ConfiguredTaskAwaiter)@awaiter).GetResult();
     }
 }
        public async Task <SessionItem> GetSessionStateItemAsync(string id, bool exclusive)
        {
            TimeSpan lockAge = TimeSpan.Zero;
            DateTime utcNow  = DateTime.UtcNow;

            byte[] buf = (byte[])null;
            SessionStateActions actions = SessionStateActions.None;
            NpgsqlCommand       cmd     = (NpgsqlCommand)null;

            cmd = !exclusive?this._commandHelper.CreateGetStateItemCmd(SqlInMemoryTableSessionStateRepository.GetStateItemSql, id) : this._commandHelper.CreateGetStateItemExclusiveCmd(SqlInMemoryTableSessionStateRepository.GetStateItemExclusiveSql, id);

            using (NpgsqlConnection connection = new NpgsqlConnection(this._connectString))
            {
                SqlDataReader reader = (SqlDataReader)await SqlSessionStateRepositoryUtil.SqlExecuteReaderWithRetryAsync(connection, cmd, new Func <RetryCheckParameter, bool>(this.CanRetry), CommandBehavior.Default);

                try
                {
                    if (reader.ReadAsync().Result)
                    {
                        buf = (byte[])await(Task <byte[]>) reader.GetFieldValueAsync <byte[]>(0);
                    }
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                }
                reader = (SqlDataReader)null;
                NpgsqlParameter putParameterValue = cmd.GetOutPutParameterValue(SqlParameterName.Locked);
                if (putParameterValue == null || Convert.IsDBNull(putParameterValue.Value))
                {
                    return((SessionItem)null);
                }
                int    num    = (bool)putParameterValue.Value ? 1 : 0;
                object lockId = (object)(int)cmd.GetOutPutParameterValue(SqlParameterName.LockCookie).Value;
                if (num != 0)
                {
                    lockAge = new TimeSpan(0, 0, (int)cmd.GetOutPutParameterValue(SqlParameterName.LockAge).Value);
                    if (lockAge > new TimeSpan(0, 0, 31536000))
                    {
                        lockAge = TimeSpan.Zero;
                    }
                    return(new SessionItem((byte[])null, true, lockAge, lockId, actions));
                }
                actions = (SessionStateActions)cmd.GetOutPutParameterValue(SqlParameterName.ActionFlags).Value;
                return(new SessionItem(buf, true, lockAge, lockId, actions));
            }
        }
Exemplo n.º 7
0
        public async Task CreateUninitializedSessionItemAsync(string id, int length, byte[] buf, int timeout)
        {
            NpgsqlCommand    uninitializedItemCmd = this._commandHelper.CreateTempInsertUninitializedItemCmd(PostgresSessionStateRepository.TempInsertUninitializedItemSql, id, length, buf, timeout);
            NpgsqlConnection connection           = new NpgsqlConnection(this._connectString);

            try
            {
                int num = await SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, uninitializedItemCmd, new Func <RetryCheckParameter, bool>(this.CanRetry), true);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
            connection = (NpgsqlConnection)null;
        }
Exemplo n.º 8
0
        public async Task ReleaseSessionItemAsync(string id, object lockId)
        {
            NpgsqlCommand    itemExclusiveCmd = this._commandHelper.CreateReleaseItemExclusiveCmd(PostgresSessionStateRepository.ReleaseItemExclusiveSql, id, lockId);
            NpgsqlConnection connection       = new NpgsqlConnection(this._connectString);

            try
            {
                int num = await SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, itemExclusiveCmd, new Func <RetryCheckParameter, bool>(this.CanRetry), false);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
            connection = (NpgsqlConnection)null;
        }
Exemplo n.º 9
0
        public async Task CreateOrUpdateSessionStateItemAsync(bool newItem, string id, byte[] buf, int length, int timeout, int lockCookie, int orginalStreamLen)
        {
            NpgsqlCommand    sqlCmd     = newItem ? this._commandHelper.CreateInsertStateItemLongCmd(PostgresSessionStateRepository.InsertStateItemLongSql, id, buf, length, timeout) : this._commandHelper.CreateUpdateStateItemLongCmd(PostgresSessionStateRepository.UpdateStateItemLongSql, id, buf, length, timeout, lockCookie);
            NpgsqlConnection connection = new NpgsqlConnection(this._connectString);

            try
            {
                int num = await SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, sqlCmd, new Func <RetryCheckParameter, bool>(this.CanRetry), newItem);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
            connection = (NpgsqlConnection)null;
        }
        public async Task ResetSessionItemTimeoutAsync(string id)
        {
            NpgsqlCommand    resetItemTimeoutCmd = this._commandHelper.CreateResetItemTimeoutCmd(SqlInMemoryTableSessionStateRepository.ResetItemTimeoutSql, id);
            NpgsqlConnection connection          = new NpgsqlConnection(this._connectString);

            try
            {
                int num = await SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, resetItemTimeoutCmd, new Func <RetryCheckParameter, bool>(this.CanRetry), false);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
            connection = (NpgsqlConnection)null;
        }
Exemplo n.º 11
0
 public void CreateSessionStateTable()
 {
     using (NpgsqlConnection connection = new NpgsqlConnection(this._connectString))
     {
         try
         {
             NpgsqlCommand newSessionTableCmd = this._commandHelper.CreateNewSessionTableCmd(
                 PostgresSessionStateRepository.CreateSessionTableSql);
             ConfiguredTaskAwaitable <int> configuredTaskAwaitable = SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, newSessionTableCmd, new Func <RetryCheckParameter, bool>(this.CanRetry), false).ConfigureAwait(false);
             // ISSUE: explicit reference operation
             ConfiguredTaskAwaitable <int> .ConfiguredTaskAwaiter awaiter = ((ConfiguredTaskAwaitable <int>)@configuredTaskAwaitable).GetAwaiter();
             // ISSUE: explicit reference operation
             ((ConfiguredTaskAwaitable <int> .ConfiguredTaskAwaiter)@awaiter).GetResult();
         }
         catch (Exception ex)
         {
             throw new HttpException(Resource1.Cant_connect_sql_session_database, ex);
         }
     }
 }