ChallengeFind the bug fixes

time to read 1 min | 116 words

In the following code there are two very subtle bug fixes that result from painful experience. Can you spot them?

public class Global: System.Web.HttpApplication
{
	public static ISessionFactory SessionFactory = CreateSessionFactory();
	
	protected static ISessionFactory CreateSessionFactory()
	{
		return new Configuration()
			.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"))
			.BuildSessionFactory();
	}
	
	public static ISession CurrentSession
	{
		get{ return (ISession)HttpContext.Current.Items["current.session"]; }
		set { HttpContext.Current.Items["current.session"] = value; }
	}
	
	protected Global()
	{
		BeginRequest += delegate
		{
			CurrentSession = SessionFactory.OpenSession();
		};
		EndRequest += delegate
		{
			if(CurrentSession != null)
				CurrentSession.Dispose();
		};
	}
}

Oh, and it isn’t a Null Reference Exception in the EndRequest event handler.

More posts in "Challenge" series:

  1. (03 Jan 2020) Spot the bug in the stream–answer
  2. (15 Feb 2010) Where is the optimization?