-->
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.  [ 4 posts ] 
Author Message
 Post subject: Server Error in Application
PostPosted: Tue Sep 06, 2005 3:55 am 
Contributor
Contributor

Joined: Sun Jun 26, 2005 5:03 am
Posts: 51
Location: London, UK
Hi, I have an ASP.NET project using the standard session handling pattern.

After it's been running for a while we get

Quote:
Server Error in '/' Application.
------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
NHibernate.Cfg.Configuration.SecondPassCompile() in C:\Devel\Sourceforge\NHibernate\nhibernate\src\NHibernate\Cfg\Configuration.cs:776
NHibernate.Cfg.Configuration.BuildSessionFactory() in C:\Devel\Sourceforge\NHibernate\nhibernate\src\NHibernate\Cfg\Configuration.cs:896
ComponentSoftware.Persistence.NHibernate.SessionFactory.get_NHibernateFactory() in c:\devel\library\net\nhibernate\sessionfactory.cs:60
ComponentSoftware.Persistence.NHibernate.BaseRepository.get_Factory() in c:\devel\library\net\nhibernate\baserepository.cs:36
ComponentSoftware.Persistence.NHibernate.BaseRepository.OpenSession() in c:\devel\library\net\nhibernate\baserepository.cs:135
WorldTravelOnline.Domain.Service.Repository..ctor(Boolean open) in C:\Devel\WorldTravelOnline\Code\Domain\Service\Repository.cs:21
WorldTravelOnline.Web.Util.NSessionModule.Context_BeginRequest(Object sender, EventArgs e) in C:\Devel\WorldTravelOnline\Code\web\util\NSessionModule.cs:22
System.Web.SyncEventExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute() +60
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +87


Anyone else experienced the same thing - I've looked at the code and it's difficult to work out why it might be happening.

_________________
Paul Hatcher
NHibernate Team


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 5:45 am 
Newbie

Joined: Thu May 19, 2005 6:04 am
Posts: 14
Location: Glasgow, UK
(assuming you're using the httpmodule)
I've been using a the pattern, and was getting the same problem.

I made the following change (and I've not had any problems since):

the module only hooks up context.EndRequest (although there is a context_BeginRequest method), added the hook into Init.


Hope this helps

Mark


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 1:18 pm 
Contributor
Contributor

Joined: Sun Jun 26, 2005 5:03 am
Posts: 51
Location: London, UK
Here's the code I have in the HttpModule
Code:
      private void Context_BeginRequest(object sender, EventArgs e)
      {
         Repository repository = new Repository(true);
         HttpContext.Current.Items.Add("Repository", repository);
      }

      private void Context_EndRequest(object sender, EventArgs e)
      {
         Repository repository = (Repository) HttpContext.Current.Items["Repository"];
         if (repository != null)
            repository.CloseSession();
      }


If I don't hook up the BeginRequest I get an error on first access to my repository instance - which isn't surprising as it hasn't been created. :)

How are you getting around this?

_________________
Paul Hatcher
NHibernate Team


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 5:57 am 
Newbie

Joined: Thu May 19, 2005 6:04 am
Posts: 14
Location: Glasgow, UK
OK, here's what I have. I have two classess, the module and a session factory class

first the module:

Code:
using System;
using System.Web;
using NHibernate;

namespace Web.Handlers
{

public class SessionHandler : IHttpModule
{
   private static readonly string KEY = "NHibernateSession";

   SessionFactory sf;

   public void Init(HttpApplication context)
   {
      context.BeginRequest += new EventHandler(context_BeginRequest);
      context.EndRequest += new EventHandler(context_EndRequest);
      sf = new SessionFactory();
   }

   public void Dispose()
   {
      sf = null;
   }

   private void context_BeginRequest(object sender, EventArgs e)
   {


      HttpApplication application = (HttpApplication)sender;
      HttpContext context = application.Context;
      
      context.Items[KEY] = null;
      
      if(!sf.Session.IsConnected)
      {
         sf.OpenSession();

      }

      context.Items[KEY] = sf.Session;
   }

   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.Close();
         }
         catch {}
      }

      context.Items[KEY] = null;
   }

   public static ISession CurrentSession
   {
      get
      {
         HttpContext currentContext = HttpContext.Current;
         ISession session = currentContext.Items[KEY] as ISession;
         SessionFactory lsf;

         if (session == null)
         {
            lsf = new SessionFactory();
            session = lsf.Session;

            
            currentContext.Items[KEY] = session;
         }

         if(!session.IsConnected)
         {
            session.Reconnect();

         }

         return session;
      }
   }

}

}


now the sessionfactory

Code:
using System;
using NHibernate;
using NHibernate.Cfg;
using System.Collections;
using NHibernate.Expression;


namespace Web.Handlers
{
   /// <summary>
   /// Summary description for SessionFactory.
   /// </summary>
   public class SessionFactory
   {

      Configuration config;
      ISessionFactory factory;
      private ISession session;




      public SessionFactory()
      {
         config = new Configuration();

         config.AddAssembly("MyAssembly");
         factory = config.BuildSessionFactory();
         OpenSession();
      }

      ~SessionFactory()
      {
         if (null != session)
         {
            session.Dispose();      
         }
         if (null != factory)
         {
            factory.Close();
         }

      }
      public void OpenSession()
      {
         session = factory.OpenSession(new MyInterceptor());

      }

      public ISession Session
      {
         get
         {
            return session;
         }
      }

   }
}



I didn't have the context.BeginRequest in the handler originally (based on the code on the blog) and it would work most of the time, sometimes the first access of objects via Nhibernate would fail, then work fine on second call, or an action (say navigating to a page calling the handler) would work 4 times, then die on the fifth (either the Object reference not set error, or sometimes a session is closed error).

It now works 100%, I can even stop on a page, leave it overnight, and resume the next morning without error (so I know the session is long killed).

Hope this helps

Mark


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.