NHibernate 3、HttpModule 和错误
背景:
使用 NH3 创建 Web 应用程序的启动,使用 HttpModule 来管理事务和会话的创建/提交/回滚。 在发生错误的情况下,我想向用户返回一条简单的消息,但回滚事务。 挂钩 context.Error 事件,允许在异常时轻松回滚,但是,由于捕获了异常,因此永远不会调用该事件,从而向用户显示友好的消息。
由于上述问题,我将创建一个自定义工作单元场景,并将其存储在请求的 Http 上下文中。如果发生错误,请设置 RollbackRequired 标志,并让工作单元基于此执行回滚。 将会话存储在 HttpContext 中 - 这与使用 CurrentSessionContext 相同吗?
如果有人有其他想法我会很棒。
谢谢。
BACKGROUND:
Creating the start of a web app with NH3 using HttpModule to manage the creation/commit/rollback of the transaction and session.
In a scenario where an error occurs, i would like to return a simple message back to the user, but rollback the transaction.
Hooking into the context.Error event, allows easy rollback upon an exception, however, this will never be called since exceptions are being caught, displaying a friendly message to the user.
Due to the issue above i am going to create a custom unit of work scenario, and store it in the Http Context for the request. If an error occurs, set a RollbackRequired flag, and have the unit of work perform rollback based on this.
Storing in the session in the HttpContext - Is this the same as using CurrentSessionContext?
If anyone has other thoughts i'd be great.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这几乎就是 WebSessionContext 的作用。
如果您捕获了异常,为什么不将其也回滚呢?
ISession.Transaction.WasCommited
和ISession.ITransaction.WasRolledback
指示事务是否已回滚/提交。如果它没有回滚并且没有提交,您可以在 HttpModule 中提交它。Yes, it is pretty much what the WebSessionContext thing does.
If you're catching the exception, why don't you roll it back as well?
ISession.Transaction.WasCommitted
andISession.ITransaction.WasRolledback
give you an indication of whether the transaction was rolled back / committed. You can commit it in your HttpModule if it's not rolled back and not committed.