-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: IIS 6.0 worker process hogging too much memory w/ NHibernate
PostPosted: Wed Aug 24, 2005 2:29 pm 
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;
            }
         }
      }

   }
}


Top
  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 5:28 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:38 am
Posts: 28
Didn't see anything obvious in your code. However, there are a whole bunch of memory profilers out there that will show you where and how the memory is wasted (of course I never used one so far ;)

Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 1:07 pm 
Beginner
Beginner

Joined: Tue Jul 19, 2005 11:21 am
Posts: 23
Location: erie, pa
i've had a similar issue where best i can tell is related to:

context.EndRequest += MyMethod

where MyMethod never fires. (Set debugging breakpoints and stepped through.) What i had to do was leak my abstraction and put a call in the global.asax to call MyMethod directly. I dunno if there is a bug in HttpApplication related to the EndRequest or if there's some gotcha to using it that is neither obvious or clearly documented.


PS: I'm using asp.net 2.0 beta 2.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.