nhibernate系列之一 NHSessionManager 支持多数据库

nhibernate系列之一    NHSessionManager 支持多数据库


啥也不说了  源代码。。。


using System;
using System.Collections.Generic;
using System.IO;
using NHibernate;
using NHibernate.Cfg;
using System.Web;
using System.Collections;
using System.Runtime.Remoting.Messaging;
using Com.Zfrong.Arch.Common.Data.NH.Session.Storage;
namespace Com.Zfrong.Arch.Common.Data.NH.Session
{
    /// <summary>
    /// 支持多数据库连接串
    /// </summary>
    internal sealed class NHSessionManager
    {
        #region Thread-safe唯一实例
        private NHSessionManager() {}
        private static object locker = new object();
        static NHSessionManager instance;
        public static NHSessionManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new NHSessionManager();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion
       /// <summary>
        /// key为appsttring的key,支持多数据库
       /// </summary>
       /// <param name="key"></param>
       /// <returns></returns>
       public ISessionFactory GetSessionFactory(string key) {

           ISessionFactory sessionFactory = SessionStorageManager.GetSessionStorage().GetSessionFactory(key);
     

             if (sessionFactory == null) {
                 NHibernate.Cfg.Configuration cfg = NH.Configuration.NHConfigurationManager.Instance.GetConfiguration(key);
                sessionFactory = cfg.BuildSessionFactory();
                if (sessionFactory == null) {
                    throw new InvalidOperationException(
                      "cfg.BuildSessionFactory() return null.");
                }
                SessionStorageManager.GetSessionStorage().SetSessionFactory(key, sessionFactory);
            }

            return sessionFactory;
        }
      
       /// <summary>
       /// key为appsetting的key,支持多数据库
       /// </summary>
       /// <param name="key"></param>
       /// <returns></returns>
        public ISession GetSession(string key) {
            return GetSession(key, null);
        }

        private ISession GetSession(string key,IInterceptor interceptor) {
            ISession session = SessionStorageManager.GetSessionStorage().GetSession(key);
            if (session == null) {
                ISessionFactory  sessionFactory = GetSessionFactory(key);
                if (interceptor== null){
                  session = sessionFactory.OpenSession();
                }
                else {
                    session = sessionFactory.OpenSession(interceptor);
                 }
                SessionStorageManager.GetSessionStorage().SetSession(key, session);
            }
            else if (session != null && session.IsOpen && interceptor!=null)
            {
                throw new Exception("You cannot register an interceptor once a session has already been opened");
            }
            if (session == null)
                throw new ApplicationException("session was null");
            
            return session;
        }

        public bool RemoveSession(string key)
        {
            return SessionStorageManager.GetSessionStorage().RemoveSession(key);
        }
        public bool RemoveSessionFactory(string key)
        {
             bool rtn= SessionStorageManager.GetSessionStorage().RemoveSessionFactory(key);
             RemoveSession(key);
             return rtn;
        }
        
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值