Active Record/Nhibernate TransactionScopes 问题
这个问题纯粹与AR相关。
假设我的 web/wcf 应用程序中有 2 个层 - DataLayer 和 BusinessLayer。 DataLayer 包含用于映射的几乎原子操作(查找、获取、保存、特殊查找、交叉查找等:))。有很多 HQL 或标准。该层没有使用 SessionScopes。我的一些映射包含惰性集合。
现在,BL 层的方法包含与 DL 调用混合的业务逻辑 - 这是正常的。对于内部具有只读操作的每个公共方法,我指定了 SessionScope(FlushAction.Never) 。对于所有具有保存或更新操作的方法,我使用 TransactionScope(),其中包含 try catch,在方法末尾使用直接 VoteCommit,并在 catch 子句中使用 VoteRollback。我已将 ReadCommited 设置为隔离级别。
对于基本情况,这是有效的。当我编写使用其他方法且内部有自己的事务的方法时,我开始遇到真正的问题 - 我说的是锁。例如,我有递归事务的方法。 似乎某些“读取”方法(具有自己的 SessionScope(FlushAction.Never))由于其他事务而被阻止。
我无法真正找出到底出了什么问题,但我开始认为我有一些重大的设计问题。
城堡网站上的帮助对我来说没什么用,还有 AR 源的测试 - 它们对我来说太基础了。
任何人都可以真正解释我应该如何使用 TransactionScopes 或者我的错误是什么?
This question is purely AR related.
Let's say I have 2 layers in my web/wcf application - DataLayer and BusinessLayer.
DataLayer contains almost atomic operations that work on mappings (find, get, save, special find, cross find , etc. :) ). There is a lot of HQL or Criterias. There's no SessionScopes used in this layer. Some of my mappings contain lazy collections.
Now, the BL layer has methods that contain business logic included mixed with DL calls - that's normal. For each public method that have readonly operations inside I have SessionScope(FlushAction.Never) specified. For all those methods that have some Save or Update operations, I'm using TransactionScope(), with try catch inside, with direct VoteCommit at the end of the method and VoteRollback inside catch clause. I have ReadCommitted set as isolationLevel.
For basic situations this works. I started to have real problems when I wrote methods that uses other methods with their own transactions inside - I'm talking about locks. For example I have method with transaction that is recursive.
It seems that some 'read' methods (that have their own SessionScope(FlushAction.Never)) are blocked because of other transactions.
I can't really track down what exactly is wrong, but I'm starting to think that I have some major design problem.
Help on castle site doesn't do much for me, also tests in AR sources - they are simply too basic for me.
Can anyone really explain how should I use the TransactionScopes or what's my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 Castle.Service.Transaction,它允许您以声明方式进行交易而不是明确地。这通常通过自动事务管理工具使用,但是它需要温莎。
I recommend using Castle.Service.Transaction, it allows you to do transactions declaratively instead of explicitly. This is typically used through the Automatic Transaction Management facility, but it requires Windsor.