0

I'm opening a session on a BeginRequest and attempting to close it on an EndRequest. I'm sporadically seeing an error that looks like this:

Possible nonthreadsafe access to session
at NHibernate.Action.EntityDeleteAction.Execute() 
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) 
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) 
at NHibernate.Engine.ActionQueue.ExecuteActions() 
at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) 
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) 
at NHibernate.Impl.SessionImpl.Flush() 
at ACC.Web.Modules.NHibernateSessionModule.CommitSession(ISession session) 
at ACC.Web.Modules.NHibernateSessionModule.ApplicationEndRequest(Object sender, EventArgs e) 
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

According to this question Do ASP.NET Requests always BeginRequest and EndRequest on the same thread?, I can't be sure that BeginRequest and EndRequest will fire on the same thread. How should I handle opening and closing NHibernate sessions so that I don't get these errors?

Thanks!
Chris

Community
  • 1
  • 1
Chris Harrington
  • 1,593
  • 3
  • 17
  • 26

1 Answers1

0

If you use any IoC container, you can receive an ISession on controller constructor.

I usually use attribute on method, like this

Rafael Mueller
  • 6,028
  • 3
  • 24
  • 28