Ejemplo n.º 1
0
    } // End of the UpdateLockIdAndFlags method

    #endregion

    #region Get methods

    /// <summary>
    /// Get one webshop session based on id
    /// </summary>
    /// <param name="id">The id</param>
    /// <param name="applicationName">The application name</param>
    /// <returns>A reference to a webshop session post</returns>
    public static WebshopSession GetOneById(string id, string applicationName)
    {
        // Create the post to return
        WebshopSession post = null;

        // Create the connection and the sql statement
        string connection = Tools.GetConnectionString();
        string sql = "SELECT * FROM dbo.webshop_sessions WHERE id = @id AND application_name = @application_name;";

        // The using block is used to call dispose automatically even if there is a exception.
        using (SqlConnection cn = new SqlConnection(connection))
        {
            // The using block is used to call dispose automatically even if there is a exception.
            using (SqlCommand cmd = new SqlCommand(sql, cn))
            {
                // Add a parameters
                cmd.Parameters.AddWithValue("@id", id);
                cmd.Parameters.AddWithValue("@application_name", applicationName);

                // Create a MySqlDataReader
                SqlDataReader reader = null;

                // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to
                // avoid having our application crash in such cases.
                try
                {
                    // Open the connection.
                    cn.Open();

                    // Fill the reader with one row of data.
                    reader = cmd.ExecuteReader();

                    // Loop through the reader as long as there is something to read and add values
                    while (reader.Read())
                    {
                        post = new WebshopSession(reader);
                    }
                }
                catch (Exception e)
                {
                    // We do not want to throw an exception
                    string exMessage = e.Message;
                }
                finally
                {
                    // Call Close when done reading to avoid memory leakage.
                    if (reader != null)
                        reader.Close();
                }
            }
        }

        // Return the post
        return post;

    } // End of the GetOneById method
Ejemplo n.º 2
0
    /// <summary>
    /// This method runs in the background
    /// </summary>
    public static void Run()
    {
        while(true)
        {
            // Sleep for 30 minutes 1800000
            Thread.Sleep(1800000);

            // Do the work
            WebshopSession.DeleteAllExpired();
        }

    } // End of the Run method
Ejemplo n.º 3
0
    } // End of the ReleaseItemExclusive method

    /// <summary>
    /// Reset the timeout for the session
    /// </summary>
    public override void ResetItemTimeout(HttpContext context, string id)
    {
        // Create a webshop session post
        WebshopSession webshopSession = new WebshopSession();
        webshopSession.id = id;
        webshopSession.application_name = this.applicationName;
        webshopSession.expires_date = DateTime.UtcNow.AddMinutes(this.sessionStateConfiguration.Timeout.TotalMinutes);

        // Update the expiration date
        WebshopSession.UpdateExpirationDate(webshopSession);

    } // End of the ResetItemTimeout method
Ejemplo n.º 4
0
    } // End of the SetAndReleaseItemExclusive method

    /// <summary>
    /// Release an session item
    /// </summary>
    public override void ReleaseItemExclusive(HttpContext context, string id, object lockId)
    {
        // Create a webshop session post
        WebshopSession webshopSession = new WebshopSession();
        webshopSession.id = id;
        webshopSession.application_name = this.applicationName;
        webshopSession.lock_id = (Int32)lockId;
        webshopSession.expires_date = DateTime.UtcNow.AddMinutes(this.sessionStateConfiguration.Timeout.TotalMinutes);
        webshopSession.locked = false;

        // Update the lock
        WebshopSession.Unlock(webshopSession);

    } // End of the ReleaseItemExclusive method
Ejemplo n.º 5
0
    } // End of the constructor

    #endregion

    #region Insert methods

    /// <summary>
    /// Add one webshop session post
    /// </summary>
    /// <param name="post">A reference to a webshop session post</param>
    public static void Add(WebshopSession post)
    {

        // Create the connection and the sql statement
        string connection = Tools.GetConnectionString();
        string sql = "INSERT INTO dbo.webshop_sessions (id, application_name, created_date, expires_date, " 
            + "lock_date, lock_id, timeout_limit, locked, session_items, flags) "
            + "VALUES (@id, @application_name, @created_date, @expires_date, @lock_date, @lock_id, "
            + "@timeout_limit, @locked, @session_items, @flags);";

        // The using block is used to call dispose automatically even if there are an exception.
        using (SqlConnection cn = new SqlConnection(connection))
        {
            // The using block is used to call dispose automatically even if there are an exception.
            using (SqlCommand cmd = new SqlCommand(sql, cn))
            {
                // Add parameters
                cmd.Parameters.AddWithValue("@id", post.id);
                cmd.Parameters.AddWithValue("@application_name", post.application_name);
                cmd.Parameters.AddWithValue("@created_date", post.created_date);
                cmd.Parameters.AddWithValue("@expires_date", post.expires_date);
                cmd.Parameters.AddWithValue("@lock_date", post.lock_date);
                cmd.Parameters.AddWithValue("@lock_id", post.lock_id);
                cmd.Parameters.AddWithValue("@timeout_limit", post.timeout_limit);
                cmd.Parameters.AddWithValue("@locked", post.locked);
                cmd.Parameters.AddWithValue("@session_items", post.session_items);
                cmd.Parameters.AddWithValue("@flags", post.flags);

                // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to
                // avoid having our application crash in such cases
                try
                {
                    // Open the connection
                    cn.Open();

                    // Execute the insert
                    cmd.ExecuteNonQuery();

                }
                catch (Exception e)
                {
                    // We do not want to throw an exception
                    string exMessage = e.Message;
                }
            }
        }

    } // End of the Add method
Ejemplo n.º 6
0
    } // End of the UpdateExpirationDate method

    /// <summary>
    /// Lock a webshop session
    /// </summary>
    /// <param name="post">A reference to a webshop session post</param>
    public static Int32 Lock(WebshopSession post)
    {
        // Create the int to return
        Int32 postsAffected = 0;

        // Create the connection and the sql statement
        string connection = Tools.GetConnectionString();
        string sql = "UPDATE dbo.webshop_sessions SET lock_date = @lock_date, locked = @locked "
            + "WHERE id = @id AND application_name = @application_name AND locked = 0 AND expires_date > @expires_date;";

        // The using block is used to call dispose automatically even if there are an exception.
        using (SqlConnection cn = new SqlConnection(connection))
        {
            // The using block is used to call dispose automatically even if there are an exception.
            using (SqlCommand cmd = new SqlCommand(sql, cn))
            {
                // Add parameters
                cmd.Parameters.AddWithValue("@id", post.id);
                cmd.Parameters.AddWithValue("@application_name", post.application_name);
                cmd.Parameters.AddWithValue("@expires_date", post.expires_date);
                cmd.Parameters.AddWithValue("@lock_date", post.lock_date);
                cmd.Parameters.AddWithValue("@locked", post.locked);

                // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to
                // avoid having our application crash in such cases.
                try
                {
                    // Open the connection.
                    cn.Open();

                    // Execute the update
                    postsAffected = cmd.ExecuteNonQuery();

                }
                catch (Exception e)
                {
                    // We do not want to throw an exception
                    string exMessage = e.Message;
                }
            }
        }

        // Return the int
        return postsAffected;

    } // End of the Lock method
Ejemplo n.º 7
0
    } // End of the Initialize method

    #endregion

    #region Add methods

    /// <summary>
    /// Create an uninitialized item
    /// </summary>
    public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
    {
        // Create a webshop session post
        WebshopSession webshopSession = new WebshopSession();
        webshopSession.id = id;
        webshopSession.application_name = this.applicationName;
        webshopSession.created_date = DateTime.UtcNow;
        webshopSession.expires_date = DateTime.UtcNow.AddMinutes((Double)timeout);
        webshopSession.lock_date = DateTime.UtcNow;
        webshopSession.lock_id = 0;
        webshopSession.timeout_limit = timeout;
        webshopSession.locked = false;
        webshopSession.session_items = "";
        webshopSession.flags = 1;

        // Add the session
        WebshopSession.Add(webshopSession);

    } // End of the CreateUninitializedItem method
Ejemplo n.º 8
0
    } // End of the SetItemExpireCallback method

    /// <summary>
    /// Set and realease a session post
    /// </summary>
    public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
    {
        // Serialize the SessionStateItemCollection as a string.
        string sessItems = Serialize((SessionStateItemCollection)item.Items);

        // Create a webshop session
        WebshopSession webshopSession = new WebshopSession();
        webshopSession.id = id;
        webshopSession.application_name = this.applicationName;
        webshopSession.created_date = DateTime.UtcNow;
        webshopSession.expires_date = DateTime.UtcNow.AddMinutes((Double)item.Timeout);
        webshopSession.lock_date = DateTime.UtcNow;
        webshopSession.lock_id = 0;
        webshopSession.timeout_limit = item.Timeout;
        webshopSession.locked = false;
        webshopSession.session_items = sessItems;
        webshopSession.flags = 0;

        if (newItem == true)
        {
            // Delete the session if it exists
            WebshopSession.DeleteOnId(id, this.applicationName);

            // Add the session
            WebshopSession.Add(webshopSession);

        }
        else
        {
            // Update session values
            webshopSession.lock_id = (Int32)lockId;

            // Update the session
            WebshopSession.UpdateWithLockId(webshopSession);
        }

    } // End of the SetAndReleaseItemExclusive method
Ejemplo n.º 9
0
    } // End of the Deserialize method

    #endregion

    #region Delete methods

    /// <summary>
    /// Remove a session item
    /// </summary>
    public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item)
    {
        // Delete the session post
        WebshopSession.DeleteOnId(id, this.applicationName, (int)lockId);
        
    } // End of the RemoveItem method
Ejemplo n.º 10
0
    } // End of the GetItemExclusive method


    /// <summary>
    /// Get a session store item
    /// </summary>
    private SessionStateStoreData GetSessionStoreItem(bool lockRecord, HttpContext context, string id, out bool locked, out TimeSpan lockAge, 
        out object lockId, out SessionStateActions actionFlags)
    {
        // Initial values for return value and out parameters
        SessionStateStoreData item = null;
        lockAge = TimeSpan.Zero;
        lockId = null;
        locked = false;
        actionFlags = 0;

        // String to hold serialized SessionStateItemCollection.
        string serializedItems = "";
        // True if a record is found in the database.
        bool foundRecord = false;
        // True if the returned session item is expired and needs to be deleted.
        bool deleteData = false;
        // Timeout value from the data store.
        int timeout = 0;

        // Create a webshop session
        WebshopSession webshopSession = new WebshopSession();

        if (lockRecord == true)
        {
            // Update the webshop session
            webshopSession.id = id;
            webshopSession.application_name = this.applicationName;
            webshopSession.expires_date = DateTime.UtcNow;
            webshopSession.lock_date = DateTime.UtcNow;
            webshopSession.locked = true;

            // Lock the webshop session
            Int32 postsAffected = WebshopSession.Lock(webshopSession);

            // Set the locked variable
            locked = postsAffected == 0 ? true : false;
        }

        // Get the current session
        webshopSession = WebshopSession.GetOneById(id, this.applicationName);

        if(webshopSession != null)
        {
            if (webshopSession.expires_date < DateTime.UtcNow)
            {
                locked = false;
                deleteData = true;
            }
            else
            {
                foundRecord = true;
            }

            // Set data
            serializedItems = webshopSession.session_items;
            lockId = webshopSession.lock_id;
            lockAge = DateTime.UtcNow.Subtract(webshopSession.lock_date);
            actionFlags = (SessionStateActions)webshopSession.flags;
            timeout = webshopSession.timeout_limit;
        }

       
        // If the returned session item is expired, 
        // delete the record from the data source.
        if (deleteData)
        {
            WebshopSession.DeleteOnId(id, this.applicationName);
        }

        // The record was not found. Ensure that locked is false.
        if (foundRecord == false)
        {
            locked = false;
        }
            
        // If the record was found and you obtained a lock, then set 
        // the lockId, clear the actionFlags,
        // and create the SessionStateStoreItem to return.
        if (foundRecord && !locked)
        {
            lockId = (int)lockId + 1;

            // Update the lock id and flags
            WebshopSession.UpdateLockIdAndFlags(id, this.applicationName, (Int32)lockId);

            // If the actionFlags parameter is not InitializeItem, deserialize the stored SessionStateItemCollection.
            if (actionFlags == SessionStateActions.InitializeItem)
                item = CreateNewStoreData(context, (Int32)this.sessionStateConfiguration.Timeout.TotalMinutes);
            else
                item = Deserialize(context, serializedItems, timeout);
        }

        // Return the session item
        return item;

    } // End of the GetSessionStoreItem method
Ejemplo n.º 11
0
    } // End of the Lock method

    /// <summary>
    /// Unlock a webshop session
    /// </summary>
    /// <param name="post">A reference to a webshop session post</param>
    public static void Unlock(WebshopSession post)
    {
        // Create the connection and the sql statement
        string connection = Tools.GetConnectionString();
        string sql = "UPDATE dbo.webshop_sessions SET expires_date = @expires_date, locked = @locked "
            + "WHERE id = @id AND application_name = @application_name AND lock_id = @lock_id;";

        // The using block is used to call dispose automatically even if there are an exception.
        using (SqlConnection cn = new SqlConnection(connection))
        {
            // The using block is used to call dispose automatically even if there are an exception.
            using (SqlCommand cmd = new SqlCommand(sql, cn))
            {
                // Add parameters
                cmd.Parameters.AddWithValue("@id", post.id);
                cmd.Parameters.AddWithValue("@application_name", post.application_name);
                cmd.Parameters.AddWithValue("@lock_id", post.lock_id);
                cmd.Parameters.AddWithValue("@expires_date", post.expires_date);
                cmd.Parameters.AddWithValue("@locked", post.locked);

                // The Try/Catch/Finally statement is used to handle unusual exceptions in the code to
                // avoid having our application crash in such cases.
                try
                {
                    // Open the connection.
                    cn.Open();

                    // Execute the update
                    cmd.ExecuteNonQuery();

                }
                catch (Exception e)
                {
                    // We do not want to throw an exception
                    string exMessage = e.Message;
                }
            }
        }

    } // End of the Unlock method