/// <summary> /// Save the provided session state into the database /// </summary> /// <param name="sessionId">the session id of the session state to save</param> /// <param name="session">the session state to save</param> /// <param name="sessionTimeOut">Timeout of the session</param> public void SaveSession(string sessionId, AspSessionContents session, int sessionTimeOut) { string cnx_str; var resolver = new PartitionResolver(); cnx_str = resolver.ResolvePartition(sessionId); using (SqlConnection conn = new SqlConnection(cnx_str)) { using (SqlCommand saveCmd = new SqlCommand()) { saveCmd.Connection = conn; saveCmd.CommandText = "BEGIN TRAN;"; saveCmd.CommandText += DeleteStatement; saveCmd.CommandText += InsertStatement; saveCmd.CommandText += "COMMIT"; saveCmd.Parameters.AddWithValue("@SessionTimeOut", sessionTimeOut); saveCmd.Parameters.AddWithValue("@Data", this.Serialize(session)); saveCmd.Parameters.AddWithValue("@ID", new Guid(sessionId)); try { conn.Open(); saveCmd.ExecuteNonQuery(); } catch (Exception e) { Loggers.Logger.Error("Save Session failed : " + e.Message); resolver.ResetConf(); throw; } } } }
/// <summary> /// Refresh the session into the storage /// </summary> /// <param name="sessionId">The Id of the session to refresh</param> public void RefreshSession(string sessionId) { string cnx_str; var resolver = new PartitionResolver(); cnx_str = resolver.ResolvePartition(sessionId); using (SqlConnection conn = new SqlConnection(cnx_str)) { using (SqlCommand saveCmd = new SqlCommand()) { saveCmd.Connection = conn; saveCmd.CommandText = UpdateLastAccessedStatement; saveCmd.Parameters.AddWithValue("@ID", new Guid(sessionId)); try { conn.Open(); saveCmd.ExecuteNonQuery(); } catch (Exception e) { Loggers.Logger.Error("Update Session failed : " + e.Message); resolver.ResetConf(); throw; } } } }
/// <summary> /// Load the session state corresponding to a session id from the database /// </summary> /// <param name="sessionId">the session id</param> /// <returns>the requested session state or a new one if it does not exist in the database or it is expired</returns> public AspSessionContents LoadSession(string sessionId) { string cnx_str; var resolver = new PartitionResolver(); cnx_str = resolver.ResolvePartition(sessionId); AspSessionContents session = null; using (SqlConnection conn = new SqlConnection(cnx_str)) { using (SqlCommand loadCmd = new SqlCommand()) { loadCmd.CommandText = SelectStatement; loadCmd.Connection = conn; loadCmd.Parameters.AddWithValue("@Id", new Guid(sessionId)); try { conn.Open(); using (SqlDataReader reader = loadCmd.ExecuteReader()) { if (reader.Read()) { // Session has not expired => deserialize it session = this.Deserialize(reader["Data"].ToString()); session.TimeOut = int.Parse(reader["SessionTimeOut"].ToString()); } } } catch (Exception e) { Loggers.Logger.Error("Load session failed : " + e.Message); resolver.ResetConf(); throw; } } } return(session); }