单轨、nhibernate 和每个请求会话模式
我需要一些关于我即将对我们的网络应用程序进行重构的见解和想法。
我们最初通过使用 HttpApplication 中的 On_BeginRequest / On_EndRequest 来创建和处置会话,从而将每个请求会话模式与 NHibernate 和 ActiveRecord 一起使用。后来,我们意识到任何与数据库相关的异常都被抛出到我们的单轨上下文之外,这意味着我们的救援没有启动。作为另一个副作用,我们没有选择在任何情况下完全跳过 NHibernate 会话的创建。行动,在某些情况下这是可取的。
因此,我们重写了它,在基本控制器的 Initialize() / Contextualize() 中创建会话,并将它们处理在基本控制器的 Dispose() 中。我们还在救援控制器中回滚会话,以防止对数据库进行任何半写入更改。到目前为止,一切都很好。在 Dispose() 中执行此操作的原因是因为我们希望它能够完成视图渲染,因为延迟加载原因以及需要获取会话的视图组件(我们可以切换到工作单元) viewcomponents,但它们似乎没有 Dispose()...)
但是,我遇到了一些死锁问题,我们已经在数据库中启动了事务,但该事务没有回滚也没有提交,并且我无法获取我的绕过它,主要是因为我们用这种方法造成的混乱......
所以我找到了这篇文章:http://hackingon.net/post/NHibernate-Session-Per-Request-with-ASPNET-MVC.aspx
我想,“过滤器,我们可以在 MonoRail 中也可以使用它!”,因为它可以在 BeforeAction 和 AfterRendering 中发挥作用。
我的问题是:
- 如果过滤器中发生异常会发生什么?
- 即使操作或渲染中发生异常,AfterRendering 也会触发吗?
- 您会推荐这种方法吗?如果不推荐,您的建议是什么?
非常感谢任何指点!
I need some insights and thoughts about a refactoring I'm about to do to our web-app.
We initially used the session-per-request pattern with NHibernate and ActiveRecord by using the On_BeginRequest / On_EndRequest in the HttpApplication to create and dispose the session. Later on, we realized that any DB-related exceptions got thrown outside of our monorail-context, meaning that our rescues didn't kick in. As another sideeffect, we didn't have the option to fully skip creation of NHibernate sessions in any action, which in some cases would be desirable.
So we rewrote it to create sessions in Initialize() / Contextualize() in our base controller, and disposed them in the Dispose() of our base controller. We also Rollback the session in our rescue controller to prevent any half written changes to the DB. So far, so good. The reason for doing it in the Dispose() is because we want it to live through the view-rendering, because of lazy-loading reasons aswell as viewcomponents that needs to get a session (we could switch to units-of-work for the viewcomponents, but they don't seem to have a Dispose()...)
However, I'm experiencing some deadlock issues where we have started transacations in the DB that isn't getting rollbacked nor committed and I can't get my head around it, mostly because of the mess we've made with this approach...
So I found this article: http://hackingon.net/post/NHibernate-Session-Per-Request-with-ASPNET-MVC.aspx
And I thought, "Filters, we can use that in MonoRail too!", because it can kick in on BeforeAction and AfterRendering.
My questions then are:
- What happens if an Exception occurs in the filter?
- Will AfterRendering fire even if an Exception occurs in the action or the rendering?
- Would you recommend this approach, if not, what are your suggestion instead?
Any pointers are very much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个应用程序错误处理程序来处理异常。
附加一个调试器并找出答案。
可能不是(尽管这是我的文章)。它不适用于 RenderAction。最好使用 IoC 容器来控制连接的生命周期。
You need an application error handler to care of exception handling.
Attach a debugger and find out.
Probably not (even though it is my article). It doesn't work with RenderAction. Best to use an IoC container to control the lifetime of connections.