We recently started using Nhibernate in an existing application, and are running into memory issues. The IIS 6.0 worker process w3wp.exe keeps consuming a lot of memory, till the server just stops responding.
I really do not want to do worker process recycling as it kills the sessions and forces users to log off.
I am using a session per request model (an httpmodule). I am noting the source code of the httpmodule below. Please help.
Code:
using System;
using System.Web;
using NHibernate;
namespace CID.Infrastructure.BaseClasses
{
   /// <summary>
   /// Summary description for NHSessionHandler.
   /// </summary>
   public class SessionHandler : IHttpModule 
   { 
      // this is only used if not running in HttpModule mode
      private static ISession m_session;
      private static readonly string KEY = "NHibernateSession"; 
 
      public void Init(HttpApplication context) 
      { 
         context.BeginRequest += new EventHandler(context_BeginRequest);
         context.EndRequest += new EventHandler(context_EndRequest); 
      } 
 
      public void Dispose() 
      { 
      } 
 
      private void context_BeginRequest(object sender, EventArgs e) 
      { 
         HttpApplication application = (HttpApplication)sender; 
         HttpContext context = application.Context; 
 
         context.Items[KEY] = DataAccessLayer.OpenSession(); 
      } 
 
      private void context_EndRequest(object sender, EventArgs e) 
      { 
         HttpApplication application = (HttpApplication)sender; 
         HttpContext context = application.Context; 
 
         ISession session = context.Items[KEY] as ISession; 
         if (session != null) 
         { 
            try 
            { 
               session.Flush(); 
               session.Disconnect();
               session.Close(); 
               
            } 
            catch {} 
            
            finally
            {
               session.Dispose();
            }
         } 
 
         context.Items[KEY] = null; 
      } 
 
      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 = DataAccessLayer.OpenSession(); 
                  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] as ISession; 
 
               if (session == null) 
               { 
                  session = DataAccessLayer.OpenSession(); 
                  currentContext.Items[KEY] = session; 
               } 
 
               return session; 
            }
         } 
      } 
 
   } 
}