Eu tenho uma aplicação que fica em um site único e na hora do usuário fazer o login ele escolhe o banco que deseja trabalhar ou que ele tenha acesso.
Gostaria de saber se alguém já fez ou tem algum modelo para me passar.
using System.Data;
using System.Runtime.Remoting.Messaging;
using System.Web;
using NHibernate;
using NHibernate.Cache;
using NHibernate.Cfg;
namespace Data.NhibernateBase
{
public sealed class NHibernateSessionManager
{
#region Thread-safe, lazy Singleton
/// <summary>
/// for more details about its implementation.
/// </summary>
static NHibernateSessionManager instance;
static readonly object padlock = new object();
public static NHibernateSessionManager Instance
{
get
{
lock (padlock)
{
//Alteramos aqui Henrique e Elton
return instance ?? (instance = new NHibernateSessionManager());
//return instance = new NHibernateSessionManager();
}
}
}
private NHibernateSessionManager()
{
InitSessionFactory();
}
#endregion
private void InitSessionFactory()
{
var cfg = new Configuration();
if (MappingByCodeConfiguration.Configured)
{
cfg.AddMapping(MappingByCodeConfiguration.GetCompiledMapping());
}
cfg.DataBaseIntegration(x => x.ConnectionStringName = DataContext.ConnectionStringName);
sessionFactory = cfg.Configure().BuildSessionFactory();
}
/// <summary>
/// Allows you to register an interceptor on a new session. This may not be called if there is already
/// an open session attached to the HttpContext. If you have an interceptor to be used, modify
/// the HttpModule to call this before calling BeginTransaction().
/// </summary>
public void RegisterInterceptor(IInterceptor interceptor)
{
ISession session = ThreadSession;
if (session != null && session.IsOpen)
{
throw new CacheException("You cannot register an interceptor once a session has already been opened");
}
GetSession(interceptor);
}
public ISession GetSession()
{
return GetSession(null);
}
/// <summary>
/// Gets a session with or without an interceptor. This method is not called directly; instead,
/// it gets invoked from other public methods.
/// </summary>
private ISession GetSession(IInterceptor interceptor)
{
ISession session = ThreadSession;
if (session == null)
{
session = interceptor != null ? sessionFactory.OpenSession(interceptor) : sessionFactory.OpenSession();
//session = sessionFactory.OpenSession();
ThreadSession = session;
}
session.FlushMode = FlushMode.Auto;
return session;
}
public ISession NewSession()
{
return sessionFactory.OpenSession();
}
public ISession NewSession(IDbConnection conn)
{
return sessionFactory.OpenSession(conn);
}
public void CloseSession()
{
ISession session = ThreadSession;
ThreadSession = null;
if (session != null && session.IsOpen)
{
session.Close();
}
}
private ISession ThreadSession
{
get
{
if (HttpContext.Current != null)
{
return (ISession)HttpContext.Current.Items[SESSION_KEY];
}
return (ISession)CallContext.GetData(SESSION_KEY);
}
set
{
if (HttpContext.Current != null)
{
HttpContext.Current.Items[SESSION_KEY] = value;
}
else CallContext.SetData(SESSION_KEY, value);
}
}
private ISessionFactory sessionFactory;
private const string SESSION_KEY = "THREAD_SESSION";
~NHibernateSessionManager()
{
CloseSession();
}
}
}