Exemplo n.º 1
0
 public void CreateSessionStateTable()
 {
     using (var connection = new SqlConnection(_connectString))
     {
         try
         {
             var cmd  = _commandHelper.CreateNewSessionTableCmd(CreateSessionTableSql);
             var task = SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, cmd, CanRetry).ConfigureAwait(false);
             task.GetAwaiter().GetResult();
         }
         catch (Exception ex)
         {
             // Indicate that the DB doesn't support InMemoryTable
             var sqlException = ex as SqlException;
             if (sqlException != null &&
                 // 40536 error code from SQL Azure
                 // 41337 error code from SQL server
                 (sqlException.Number == 40536 || sqlException.Number == 41337))
             {
                 throw sqlException;
             }
             else
             {
                 throw new HttpException(SR.Cant_connect_sql_session_database, ex);
             }
         }
     }
 }
        public async Task <SessionItem> GetSessionStateItemAsync(string id, bool exclusive)
        {
            var    locked  = false;
            var    lockAge = TimeSpan.Zero;
            var    now     = DateTime.UtcNow;
            object lockId  = null;

            byte[] buf = null;
            SessionStateActions actions = SessionStateActions.None;
            SqlCommand          cmd     = null;

            if (exclusive)
            {
                cmd = _commandHelper.CreateGetStateItemExclusiveCmd(GetStateItemExclusiveSql, id);
            }
            else
            {
                cmd = _commandHelper.CreateGetStateItemCmd(GetStateItemSql, id);
            }

            using (var connection = new SqlConnection(_connectString))
            {
                using (var reader = await SqlSessionStateRepositoryUtil.SqlExecuteReaderWithRetryAsync(connection, cmd, CanRetry))
                {
                    if (await reader.ReadAsync())
                    {
                        buf = await reader.GetFieldValueAsync <byte[]>(0);
                    }
                }

                var outParameterLocked = cmd.GetOutPutParameterValue(SqlParameterName.Locked);
                if (outParameterLocked == null || Convert.IsDBNull(outParameterLocked.Value))
                {
                    return(null);
                }
                locked = (bool)outParameterLocked.Value;
                lockId = (int)cmd.GetOutPutParameterValue(SqlParameterName.LockCookie).Value;

                if (locked)
                {
                    lockAge = new TimeSpan(0, 0, (int)cmd.GetOutPutParameterValue(SqlParameterName.LockAge).Value);

                    if (lockAge > new TimeSpan(0, 0, Sec.ONE_YEAR))
                    {
                        lockAge = TimeSpan.Zero;
                    }
                    return(new SessionItem(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.º 3
0
        public async Task CreateUninitializedSessionItemAsync(string id, int length, byte[] buf, int timeout)
        {
            var cmd = _commandHelper.CreateTempInsertUninitializedItemCmd(TempInsertUninitializedItemSql, id, length, buf, timeout);

            using (var connection = new SqlConnection(_connectString))
            {
                await SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, cmd, CanRetry, true);
            }
        }
Exemplo n.º 4
0
        public async Task ReleaseSessionItemAsync(string id, object lockId)
        {
            var cmd = _commandHelper.CreateReleaseItemExclusiveCmd(ReleaseItemExclusiveSql, id, lockId);

            using (var connection = new SqlConnection(_connectString))
            {
                await SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, cmd, CanRetry);
            }
        }
Exemplo n.º 5
0
        public async Task ResetSessionItemTimeoutAsync(string id)
        {
            var cmd = _commandHelper.CreateResetItemTimeoutCmd(ResetItemTimeoutSql, id);

            using (var connection = new SqlConnection(_connectString))
            {
                await SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, cmd, CanRetry);
            }
        }
Exemplo n.º 6
0
 public void DeleteExpiredSessions()
 {
     using (var connection = new SqlConnection(_connectString))
     {
         var cmd  = _commandHelper.CreateDeleteExpiredSessionsCmd(DeleteExpiredSessionsSql);
         var task = SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, cmd, CanRetry).ConfigureAwait(false);
         task.GetAwaiter().GetResult();
     }
 }
        private bool CanRetry(RetryCheckParameter parameter)
        {
            if (_retryIntervalMilSec <= 0 ||
                !SqlSessionStateRepositoryUtil.IsFatalSqlException(parameter.Exception) ||
                parameter.RetryCount >= _maxRetryNum)
            {
                return(false);
            }

            // sleep the specified time and allow retry
            Thread.Sleep(_retryIntervalMilSec);
            parameter.RetryCount++;

            return(true);
        }
 public void CreateSessionStateTable()
 {
     using (var connection = new SqlConnection(_connectString))
     {
         try
         {
             var cmd  = _commandHelper.CreateNewSessionTableCmd(CreateSessionTableSql);
             var task = SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, cmd, CanRetry).ConfigureAwait(false);
             task.GetAwaiter().GetResult();
         }
         catch (Exception ex)
         {
             throw new HttpException(SR.Cant_connect_sql_session_database, ex);
         }
     }
 }
        public async Task CreateOrUpdateSessionStateItemAsync(bool newItem, string id, byte[] buf, int length, int timeout, int lockCookie, int orginalStreamLen)
        {
            SqlCommand cmd;

            if (!newItem)
            {
                cmd = _commandHelper.CreateUpdateStateItemLongCmd(UpdateStateItemLongSql, id, buf, length, timeout, lockCookie);
            }
            else
            {
                cmd = _commandHelper.CreateInsertStateItemLongCmd(InsertStateItemLongSql, id, buf, length, timeout);
            }

            using (var connection = new SqlConnection(_connectString))
            {
                await SqlSessionStateRepositoryUtil.SqlExecuteNonQueryWithRetryAsync(connection, cmd, CanRetry, newItem);
            }
        }