TransactionScope 需要多个隔离级别?
我在应用程序中遇到需要使用表锁提示或将事务隔离级别设置为默认已提交读以外的级别的情况,以解决死锁问题。我使用面向服务的体系结构,每个服务调用都作为原子操作运行,Linq To Sql 充当轻量级 DAL。每个服务调用都会调用我的业务层并声明一个新事务,如下所示:
using (var scope = new TransactionScope())
{
// Get datacontext
// do Business Logic stuff, including database operations via Linq To Sql
// Save transaction
scope.Complete();
}
问题是有时我有复杂的业务逻辑,需要许多数据库操作。一些读取、一些写入、一些更新读取等,都在同一个服务调用中,因此是同一个事务。
我读过有关 Linq To Sql 无法向 linq 查询添加表锁定提示的信息,建议使用 TransactionScope 隔离级别的解决方案。这很好,但在我的情况下,每个事务都是为了原子服务调用的目的,我不知道这会在哪里起作用。例如,如果我需要在没有锁定的情况下读取一个表,脏读可能没问题,然后转而进行另一次读取以进行更新,然后进行更新。我不想为整个事务设置“未提交读取”,而只想为一次特定读取设置,那么我该怎么办?
是否没有我可以实现的扩展,它允许我添加表锁定提示,而不使用视图或存储过程,或使用 datacontext.ExecuteQuery("my raw sql string here")
I am running into situations in my application where I need to use table lock hints or set the transaction isolation level to something other than the default Read Committed, in order to resolve deadlock issues. I am using a service oriented architecture, with each service call operating as an atomic operation, and Linq To Sql is serving as a lightweight DAL. Each service call calls my Business Layer and declares a new transaction like this:
using (var scope = new TransactionScope())
{
// Get datacontext
// do Business Logic stuff, including database operations via Linq To Sql
// Save transaction
scope.Complete();
}
The problem is sometimes I have complicated business logic that requires many database operations. Some reads, some writes, some reads for updating, etc, all within the same service call, and thus the same transaction.
I have read about the inability of Linq To Sql to add table lock hints to your linq query, with the suggested solution of using TransactionScope isolation levels instead. That's great and all, but in my situation, where each Transaction is for the purpose of an atomic service call, I don't see where this would work. For example, if I need to read one table without locking and dirty reads may be OK, and turn around and do another read for the purpose of updating, and do an update. I don't want to set Read Uncommitted for the entire transaction, only one particular read, so what do I do?
Is there not an extension I can implement that will allow me to add table lock hints, without using views or stored procedures, or using datacontext.ExecuteQuery("my raw sql string here")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这里最好的答案是使用多个事务,并批处理在一批中仅读取“脏”的事务,以及在另一批中提交需要读取的更新。如果任何信息需要跨批次,请为该数据设置一个临时内存缓存。
I think the best answer here is to use multiple Transactions, and batch the transactions that only read "dirty" in one batch, and the updates that require read committed in another batch. If any information needs to cross batches, setup a temporary in memory cache for that data.