zoukankan      html  css  js  c++  java
  • 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;
            }
    
        }
    }
  • 相关阅读:
    求一列的和,awk和perl哪个快?
    转:使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
    使用apt-get autoremove造成的系统无法开机
    因不公对待,技术销毁删除代码数据,谁对谁错?负能量文章,老板慎入。
    我曾经做过的插件
    宝石TD迷宫设计器
    VSX-5 VSXMusic 编码听音乐
    耐得住寂寞,才能守得住繁华
    VSX-4 VSXTra
    VSX-3 VSCT文件
  • 原文地址:https://www.cnblogs.com/chenrui7/p/3396829.html
Copyright © 2011-2022 走看看