コード例 #1
0
        /// <summary>
        /// 根据配置信息找到或创建工厂,并提供一个会话对象
        /// </summary>
        /// <param name="dac">数据访问配置类</param>
        /// <returns>返回值为一个回话对象</returns>
        public static ISession GetSession(DataAccessorConfiguration dac)
        {
            lock (dict.SyncRoot)
            {
                if (dict.ContainsKey(dac))
                {
                    return((dict[dac] as HBData).Factory.OpenSession());
                }
                else
                {
                    Configuration cfg = new Configuration();
                    foreach (Assembly asse in dac.MappingsAssemblies)
                    {
                        cfg.Configure(dac.ConfigFile).AddAssembly(asse);
                    }
                    if (dac.NamingStrategy != null)
                    {
                        cfg.SetNamingStrategy(dac.NamingStrategy);
                    }
                    ISessionFactory factory = cfg.BuildSessionFactory();

                    NS.Component.NHibernate.NHibernateFactory.Initialize(factory);

                    HBData data = new HBData()
                    {
                        Configuration = cfg, Factory = factory
                    };
                    dict.Add(dac, data);
                    return(factory.OpenSession());
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// 清理某个数据库连接
 /// </summary>
 /// <param name="dac">传入的参数为构建数据库连接时使用的配置对象</param>
 public static void Clear(DataAccessorConfiguration dac)
 {
     lock (dict.SyncRoot)
     {
         if (dict.ContainsKey(dac))
         {
             HBData hbdata = dict[dac] as HBData;
             dict.Remove(dac);
             hbdata.Factory.Dispose();
             hbdata = null;
         }
     }
 }