啥也不说了 源代码。。。
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;
}
}
}