title: Application_EndRequest Doesn't Fire on a 404
body: <p>I am using ASP MVC 2 and Nhibernate. I have created an HTTP Module as demonstrated in Summer of NHibernate 13 that looks like so:</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>web.config</p>
  
  <pre><code>&lt;add name="UnitOfWork" type="HttpModules.UnitOfWork"/&gt;
  </code></pre>
  
  <p>My problem is that Application_EndRequest never gets called on a 404 error so if my view does not render I completely block database access until my flush takes place. I am fairly new to NHibernate so I am not sure if I am missing something.</p>