/// <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>
        /// Initialize the Session State
        /// </summary>
        private void InitializeSession()
        {
            var session_id = this.SessionId;
            AspSessionContents session;

            session = this.sessionPersistence.LoadSession(session_id);
            if (session == null)
            {
                session = new AspSessionContents();
#if HOMELIDAYS_SESSION_ONSTART
#endif
            }

            this.contents = session;
        }
        /// <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);
        }
        /// <summary>
        /// Deserialize un xml en objet sessionstate
        /// </summary>
        /// <param name="xml">xml a déserializer</param>
        /// <returns>retourne un objet sessionstate</returns>
        public AspSessionContents Deserialize(string xml)
        {
            if (xml == null)
            {
                return(null);
            }

            AspSessionContents session = new AspSessionContents();

            XDocument xdoc         = XDocument.Parse(xml);
            var       queryforItem = from item in xdoc.Element("Session").Elements()
                                     select new
            {
                key   = item.Attribute("Key").Value,
                value = this.DeserializeItem(item)
            };

            foreach (var item in queryforItem)
            {
                session.Add(item.key, item.value);
            }

            return(session);
        }
        /// <summary>
        /// Serialize the SessionState Collection in order to save it in the database
        /// </summary>
        /// <param name="session">the session state to serailize to a string</param>
        /// <returns>a string of an formatted xml</returns>
        public string Serialize(AspSessionContents session)
        {
            if (session == null)
            {
                return(null);
            }

            var xquery = from sess in session
                         select this.SerializeItemOrTable(sess.Key, sess.Value);

            StringBuilder     sb  = new StringBuilder();
            XmlWriterSettings xws = new XmlWriterSettings();

            xws.OmitXmlDeclaration = false;
            xws.Indent             = false;

            using (XmlWriter xw = XmlWriter.Create(sb, xws))
            {
                XDocument doc = new XDocument(new XElement("Session", xquery));
                doc.WriteTo(xw);
            }

            return(sb.ToString());
        }