Exemple #1
0
        public virtual void ReleaseItemExclusive(IAspEnvironmentContext context, string id, object lockID)
        {
            ///We are using current context items to hold some values. So we have to delete them before
            ///current request completes or is forced to complete.
            context.RemoveItem(SESSION_LOCK_COUNT);

            if (_lockSessions)
            {
                ReleaseSessionItemLock(context, id, lockID);
            }
        }
Exemple #2
0
        private object getSessionStoreItem(bool acquireLock, IAspEnvironmentContext context, string id, out bool locked, out TimeSpan lockAge, out object lockID, out SessionInitializationActions action)
        {
            //lock age is set to zero, so SessionStateModule will,
            //after 0.5 sec, calls this GetItemExclusive function until a value or
            //null reference is returned. If no data was found, 'locked' is set to false and
            //null reference is returned which tells SessionStateModule to call CreateDataStore
            //function to create new data store

            locked  = false;
            lockAge = TimeSpan.Zero;
            lockID  = null;
            action  = SessionInitializationActions.InitializeItem;
            DateTime lockDate = DateTime.UtcNow;

            LockHandle lockHandle   = new LockHandle(null, lockDate);
            object     items        = null;
            Hashtable  table        = null;
            bool       lockTimedOut = false;
            string     locationID   = GetLocationID(context, id);


            if (_cache == null)
            {
                LogError(new OperationFailedException("The specified cache is not running. "), id);
                return(null);
            }

            try
            {
                _cache.IsSessionCookieless = IsSessionCookieless(context);
                byte[] buffer = null;
                lock (s_dataLock)
                {
                    if (s_cacheNeedInit)
                    {
                        InitializeCache();
                    }
                }
                if (_lockSessions)
                {
                    try
                    {
                        buffer = (byte[])_cache.Get(locationID, GetUniqueSessionId(id), ref lockHandle, acquireLock, true);
                    }
                    catch (Exception)
                    {
                        buffer = (byte[])_cache.Get(locationID, GetUniqueSessionId(id), ref lockHandle, acquireLock, false);
                    }


                    lockID   = lockHandle.LockId == null ? null : lockHandle;
                    lockDate = lockHandle == null ? DateTime.UtcNow : lockHandle.LockDate;
                }
                else
                {
                    try
                    {
                        buffer = (byte[])_cache.Get(locationID, GetUniqueSessionId(id), true);
                    }
                    catch (Exception)
                    {
                        buffer = (byte[])_cache.Get(locationID, GetUniqueSessionId(id), false);
                    }
                }

                if (buffer != null)
                {
                    using (MemoryStream stream = new MemoryStream(buffer))
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        table = formatter.Deserialize(stream) as Hashtable;
                        stream.Close();
                    }
                }

                if (_lockSessions && !String.IsNullOrEmpty(lockHandle.LockId))
                {
                    DateTime now = DateTime.UtcNow;
                    if ((0L < lockDate.Ticks) && (lockDate.Ticks < now.Ticks))
                    {
                        lockAge = (TimeSpan)(now - lockDate);
                    }
                    else
                    {
                        lockAge = TimeSpan.Zero;
                    }

                    /// Following 'if' block is executed if item is locked.
                    /// i.e NCache API returns null and out variables are populated with lockId and lockDate
                    /// Note: Item exists in cache but is locked.
                    if (table == null)
                    {
                        if (this._sessionLockingRetries >= 0)
                        {
                            if (!context.ContainsItem(SESSION_LOCK_COUNT))
                            {
                                context[SESSION_LOCK_COUNT] = 0;
                            }

                            int retriesCompleted = (int)context[SESSION_LOCK_COUNT];

                            if (retriesCompleted < this._sessionLockingRetries)
                            {
                                context[SESSION_LOCK_COUNT] = retriesCompleted + 1;
                            }
                            else ///this will construct and send a dummy session to application
                            {
                                if (this._emptySessionWhenLocked)
                                {
                                    locked       = false;///a dummy session is going to be returned
                                    lockID       = null;
                                    lockTimedOut = true;

                                    return(CreateEmptySession(context, _defaultTimeout));
                                }
                                else
                                {
                                    throw new ProviderException("Cannot acquire session lock. Session is already locked.");
                                }
                            }
                        }

                        locked = true;
                        return(null);
                    }
                }

                /// If item exists in cahce and lock acqusition was successfull or locking was disabled.
                if (table != null)
                {
                    items = GetContents(context, table, ref action);
                    if (action == SessionInitializationActions.InitializeItem)
                    {
                        int timeout = (int)table[TIMEOUT_KEY];
                        items = CreateNewStoreData(context, timeout);
                    }
                }
                else
                {
                    if (NCacheLog != null && NCacheLog.IsInfoEnabled)
                    {
                        NCacheLog.Warn("SessionStoreBase.GetSessionItem", "Session not found in cache. " + id);
                    }
                }
            }
            catch (Exception exc)
            {
                /// If item is not found in Remote cache.
                /// Update cookies to do any further operations for this session request on current primary cache;
                /// and do not raise exception.

                LogError(exc, id);
            }
            finally
            {
                if ((lockTimedOut || table != null) && context.ContainsItem(SESSION_LOCK_COUNT))
                {
                    context.RemoveItem(SESSION_LOCK_COUNT);
                }
                //    throw new ProviderException("Cannot acquire session lock. Session is already locked.");
            }
            return(items);
        }