NHibernate 对 BeginRequest 和 EndRequest 的会话处理

发布于 2024-12-14 03:31:30 字数 1253 浏览 1 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2024-12-21 03:31:30

如果您使用任何 IoC 容器,您可以在控制器构造函数上接收 ISession。

我通常在方法上使用属性,例如 这个

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

I usually use attribute on method, like this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文