由于现在的项目数据访问层使用Nhibernate管理,访问页面 数据加载时候特别慢,

原来是每次访问Session的时候 一开 一闭  很是消耗性能。

访问自然也就慢了,查了些资料,Session可以进行Application级的管理

具体代码:

 

 class SessionManager : IHttpModule
    {

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(Application_BeginRequest);
            context.EndRequest += new EventHandler(Application_EndRequest);
        }

        public void Dispose()
        {
        }

        private void Application_BeginRequest(object sender, EventArgs e)
        {
            ManagedWebSessionContext.Bind(HttpContext.Current,    YQKC_ERP.DAL.NHibernateHelper.GetCurrentSession());//开启Session
        }

        private void Application_EndRequest(object sender, EventArgs e)
        {
            YQKC_ERP.DAL.NHibernateHelper.CloseSessionFactory();//关闭Session
        }
    }

在web.config添加HttpModule

 <httpModules>
      <add name="SessinModel" type="YQKC_ERP.T.SessionManager" />
  </httpModules>

 

如此 效率大大提高   真是爽啊!!!

 

 

posted on 2009-04-30 19:06  冷傲残痕  阅读(1119)  评论(2编辑  收藏  举报