I’m using Session Per Request approach in ASP .NET Web Forms application.
This is the code that runs when request ends, and this is how I close session:
protected void Application_EndRequest(object sender, EventArgs e)
{
ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current, SessionFactoryProvider.SessionFactory);
if (session != null)
{
if (session.Transaction != null && session.Transaction.IsActive)
{
session.Transaction.Rollback();
}
if (session.IsOpen)
{
session.Close();
}
}
}
Pretty obvious stuff. However I’m constantly getting exception when calling session.Close. The exception is of type SessionException and the message is:
{“Session was already closed”}
The stack trace is:
at NHibernate.Impl.SessionImpl.Close() in d:\CSharp\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 343
The version of NHibernate I’m using is : 3.0.0.4000
I’ve already searched for code that could close the session, but haven’t found any. Also I’ve tested it with simple requests that do nothing with session object, and still the same. Any thoughts what could be the issue?
EDIT: After divide and conquer session, I’ve targeted a piece of code that is responsible for that exception:
var session = SessionFactoryProvider.GetCurrentSession();
using (ITransaction tx = session.BeginTransaction())
{
session.Update(instrument);
tx.Commit();
}
If I comment it out session is closed smoothly in EndRequest handler and no Session was already closed exception is thrown.
Issue was resolved by moving to NHibernate 3.3.1.
However, other issue surfaced:
Could not find the property – exception after switching from NHibernate 3 to 3.3.1