Nhibernate的Session管理

参考:http://www.cnblogs.com/renrenqq/archive/2006/08/04/467688.html

但这个方法还不能解决Session缓存问题,由于创建Session需要消耗大量的内存,参考上面的方法,当请求并发的时候,内存不断的增加。

在网上找了很多种方法都没有解决。下面贴上主要代码:

 

1、接口请参考上面文章

using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace XiaoRui.Data.NhibernateProvider
{

    /// <summary>
    /// Http Session管理
    /// </summary>
    class HttpSessionSource : ISessionStorage
    {
        private string name;
        private ISession session;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name">session名称</param>
        public HttpSessionSource(string name)
        {
            this.name = name;
        }

        /// <summary>
        /// 获得ISession 
        /// </summary>
        /// <returns>获得的ISession</returns>
        public ISession Get()
        {
            // 很多人使用下面的方法,但内存占用很大
            // session = HttpContext.Current.Items[name] as ISession;

            //使用Cache缓存
            session = HttpContext.Current.Cache[name] as ISession;

            if (session != null)
            {
                // 这里很重要
                return session.SessionFactory.OpenSession();
            }

            return session;
        }

        /// <summary>
        /// 保存ISession
        /// </summary>
        /// <param name="value">需要保存的ISession</param>
        public void Set(ISession value)
        {
            if (value != null && !HttpContext.Current.Items.Contains(name))
            {
                //HttpContext.Current.Items.Add(name, value);
                HttpContext.Current.Cache.Insert(name, value);
            }
            else
            {
                HttpContext.Current.Items.Remove(name);
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

    }
}

 

2、主要从Session管理者获取Session

 

using NHibernate;
using NHibernate.Cfg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XiaoRui.Data.NhibernateProvider
{
    /// <summary>
    /// session管理者
    /// </summary>
    class SessionManager
    {
        private static ISessionStorage storage;

        //Session工厂
        public static ISessionStorage SessionStorage
        {
            get { return storage; }
        }

        public SessionManager(string name)
        {
            storage = ISessionStorageFactory.GetSessionStorage(name);
        }

        /// <summary>
        /// 获取Session
        /// </summary>
        /// <returns></returns>
        public ISession GetSession()
        {
            ISession session = storage.Get();
            if (session == null)
            {
                session = CreateSession();
                storage.Set(session);
            }

            return session;
        }

        /// <summary>
        /// 创建Session
        /// </summary>
        /// <returns></returns>
        private ISession CreateSession()
        {
            Configuration config = new Configuration();
            config.Configure(storage.Name);
            ISession result = config.BuildSessionFactory().OpenSession();
            result.FlushMode = FlushMode.Always;    
            return result;
        }

    }
}

 

 

 

posted @ 2013-10-30 15:23  rae  阅读(453)  评论(0编辑  收藏  举报