Hello
I´m using the following class for the Hibernate Scope. Each request I reconnect to the session. The problem is that i want the user to edit a set and in case of press the botton ok, the changes will be committed and if he presses botton cancel, changes are not done. How can i do that the object has more scope?
using System;
using System.Web;
using System.Runtime.Remoting.Messaging;
using NHibernate;
using NHibernate.Cfg;
namespace com.utils.nhibernate
{
/// <summary>
/// Summary description for NHibernateHttpModule.
/// </summary>
public class NHibernateHttpModule : IHttpModule
{
// this is only used if not running in HttpModule mode
protected static ISessionFactory m_factory;
// this is only used if not running in HttpModule mode
private static ISession m_session;
private static readonly string KEY_NHIBERNATE_FACTORY = "NHibernateSessionFactory";
private static readonly string KEY_NHIBERNATE_SESSION = "NHibernateSession";
private const string TRANSACTION_KEY = "CONTEXT_TRANSACTION";
public void Init(HttpApplication context)
{
log4net.Config.XmlConfigurator.Configure();
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(context_EndRequest);
}
public void Dispose()
{
if (m_session != null)
{
m_session.Close();
m_session.Dispose();
}
if (m_factory != null)
{
m_factory.Close();
}
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
context.Items[KEY_NHIBERNATE_SESSION] = CreateSession();
}
private void context_EndRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
ISession session = context.Items[KEY_NHIBERNATE_SESSION] as ISession;
if (session != null)
{
try
{
session.Flush();
session.Close();
}
catch (Exception ex) { session.Close(); }
}
context.Items[KEY_NHIBERNATE_SESSION] = null;
}
protected static ISessionFactory CreateSessionFactory()
{
Configuration config;
ISessionFactory factory = null;
config = new Configuration();
try
{
if (config == null)
{
throw new InvalidOperationException("NHibernate configuration is null.");
}
//config = config.Configure("hibernate.cfg.xml");
config.Configure();
factory = config.BuildSessionFactory();
foreach (NHibernate.Mapping.PersistentClass pClass in config.ClassMappings)
{
string schema = System.Configuration.ConfigurationManager.AppSettings[pClass.MappedClass.Assembly.ManifestModule.Name];
if (schema != null) //cambiamos el schema de la Base de datos si no se queda tal cual
pClass.RootTable.Schema = schema;
}
if (factory == null)
{
throw new InvalidOperationException("Call to Configuration.BuildSessionFactory() returned null.");
}
else
{
return factory;
}
}
catch (Exception e)
{
foreach (NHibernate.Mapping.PersistentClass pClass in config.ClassMappings)
{
string schema = System.Configuration.ConfigurationManager.AppSettings[pClass.MappedClass.Assembly.ManifestModule.Name];
if (schema != null) //cambiamos el schema de la Base de datos si no se queda tal cual
pClass.RootTable.Schema = schema;
}
factory = config.BuildSessionFactory();
if (factory == null)
{
throw new InvalidOperationException("Call to Configuration.BuildSessionFactory() returned null.");
}
else
{
return factory;
}
}
}
public static ISessionFactory CurrentFactory
{
get
{
if (HttpContext.Current == null)
{
// running without an HttpContext (non-web mode)
// the nhibernate session is a singleton in the app domain
if (m_factory != null)
{
return m_factory;
}
else
{
m_factory = CreateSessionFactory();
return m_factory;
}
}
else
{
// running inside of an HttpContext (web mode)
// the nhibernate session is a singleton to the http request
HttpContext currentContext = HttpContext.Current;
ISessionFactory factory = currentContext.Application[KEY_NHIBERNATE_FACTORY] as ISessionFactory;
if (factory == null)
{
factory = CreateSessionFactory();
currentContext.Application[KEY_NHIBERNATE_FACTORY] = factory;
}
return factory;
}
}
}
public static ISession CreateSession()
{
ISessionFactory factory;
ISession session;
factory = NHibernateHttpModule.CurrentFactory;
if (factory == null)
{
throw new InvalidOperationException("Call to Configuration.BuildSessionFactory() returned null.");
}
session = factory.OpenSession();
if (session == null)
{
throw new InvalidOperationException("Call to factory.OpenSession() returned null.");
}
return session;
}
public static ISession CurrentSession
{
get
{
if (HttpContext.Current == null)
{
// running without an HttpContext (non-web mode)
// the nhibernate session is a singleton in the app domain
if (m_session != null)
{
return m_session;
}
else
{
m_session = CreateSession();
return m_session;
}
}
else
{
// running inside of an HttpContext (web mode)
// the nhibernate session is a singleton to the http request
HttpContext currentContext = HttpContext.Current;
ISession session = currentContext.Items[KEY_NHIBERNATE_SESSION] as ISession;
if (session == null)
{
session = CreateSession();
currentContext.Items[KEY_NHIBERNATE_SESSION] = session;
}
return session;
}
}
}
public void BeginTransaction()
{
ITransaction transaction = ContextTransaction;
if (transaction == null)
{
transaction = CurrentSession.BeginTransaction();
ContextTransaction = transaction;
}
}
public void CommitTransaction()
{
ITransaction transaction = ContextTransaction;
try
{
if (HasOpenTransaction())
{
transaction.Commit();
ContextTransaction = null;
}
}
catch (HibernateException)
{
RollbackTransaction();
throw;
}
}
public bool HasOpenTransaction()
{
ITransaction transaction = ContextTransaction;
return transaction != null && !transaction.WasCommitted && !transaction.WasRolledBack;
}
public void RollbackTransaction()
{
ITransaction transaction = ContextTransaction;
try
{
if (HasOpenTransaction())
{
transaction.Rollback();
}
ContextTransaction = null;
}catch(Exception e){
}
}
/// <summary>
/// If within a web context, this uses <see cref="HttpContext" /> instead of the WinForms
/// specific <see cref="CallContext" />. Discussion concerning this found at
///
http://forum.springframework.net/showthread.php?t=572.
/// </summary>
private ITransaction ContextTransaction
{
get
{
if (IsInWebContext())
{
return (ITransaction)HttpContext.Current.Items[TRANSACTION_KEY];
}
else
{
return (ITransaction)CallContext.GetData(TRANSACTION_KEY);
}
}
set
{
if (IsInWebContext())
{
HttpContext.Current.Items[TRANSACTION_KEY] = value;
}
else
{
CallContext.SetData(TRANSACTION_KEY, value);
}
}
}
private bool IsInWebContext()
{
return HttpContext.Current != null;
}
public static void CaducarSesion()
{
if (m_session!=null)
m_session.Clear(); //se limpian los datos de la sesión
}
}
}