TransactionScope 需要多个隔离级别?

发布于 2024-09-15 23:52:51 字数 746 浏览 10 评论 0原文

我在应用程序中遇到需要使用表锁提示或将事务隔离级别设置为默认已提交读以外的级别的情况,以解决死锁问题。我使用面向服务的体系结构,每个服务调用都作为原子操作运行,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 技术交流群。

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

发布评论

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

评论(1

萝莉病 2024-09-22 23:52:51

我认为这里最好的答案是使用多个事务,并批处理在一批中仅读取“脏”的事务,以及在另一批中提交需要读取的更新。如果任何信息需要跨批次,请为该数据设置一个临时内存缓存。

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.

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