I am using ASP MVC 2 and Nhibernate. I created an HTTP module, as shown in the summer of summer of NHibernate 13, which looks like this:
public void Init(HttpApplication context) { context.PreRequestHandlerExecute += new EventHandler(Application_BeginRequest); context.PostRequestHandlerExecute += new EventHandler(Application_EndRequest); } private void Application_BeginRequest(object sender, EventArgs e) { ISession session = StaticSessionManager.OpenSession(); session.BeginTransaction(); CurrentSessionContext.Bind(session); } private void Application_EndRequest(object sender, EventArgs e) { ISession session = CurrentSessionContext.Unbind(StaticSessionManager.SessionFactory); if (session != null) try { session.Transaction.Commit(); } catch (Exception) { session.Transaction.Rollback(); } finally { session.Flush(); session.Close(); } }
web.config
<add name="UnitOfWork" type="HttpModules.UnitOfWork"/>
My problem is that Application_EndRequest is never called on error 404, so if my view does not display, I completely block access to the database until my flash occurs. I am new to NHibernate, so I'm not sure if I have something missing.
Shane source share