NHibernate 对 BeginRequest 和 EndRequest 的会话处理
我正在 BeginRequest 上打开一个会话,并尝试在 EndRequest 上关闭它。我偶尔会看到如下错误:
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)
根据这个问题 ASP.NET 请求总是在同一线程上 BeginRequest 和 EndRequest 吗?,我无法确定 BeginRequest 和 EndRequest 是否会在同一线程上触发。我应该如何处理打开和关闭 NHibernate 会话以避免出现这些错误?
谢谢!
克里斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用任何 IoC 容器,您可以在控制器构造函数上接收 ISession。
我通常在方法上使用属性,例如 这个
If you use any IoC container, you can receive an ISession on controller constructor.
I usually use attribute on method, like this