LINQ to SQL 和并发问题

时间:2022-12-03
本文介绍了LINQ to SQL 和并发问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试构建大批量订单记录系统.共有三个主要表:1. 订单2. 订单详情3. 订单发货

We are trying to build a High-Volume Orders Record System. There are three primary tables: 1. Orders 2. OrderDetails 3. OrderShipment

发货表包含每个订单的 n 条记录,任何记录发货条目都可以在客户接受订单之前更改,之后订单将被冻结.(业务需求)

The Shipment table contains n record per order and any record shipment entry can be changed before the Customer accepts th order, after which it is frozen. (A business requirement)

虽然这在现实世界中可能不会发生......在我们的负载测试期间,我们收到 System.Data.Linq.ChangeConflictException 异常.在事务中完成提交也无济于事.我们不能强制 LINQ 在更新操作的整个持续时间内锁定该行吗?

Although this may not happen in real world scenarios... during our load tests, we are getting System.Data.Linq.ChangeConflictException exceptions. Wrapping up the submit inside a transacion is not helping either. Can't we force LINQ to get a lock on the row for the entire duration of the update operation?

有没有其他方法可以解决这个问题?

Is there any other way to get over this?

推荐答案

如果您对同一数据的并发更新有真正的问题,那么您可以考虑在事务中执行整个操作 - 即获取数据 提交它.只要您将 get/update/commit 视为短期的原子操作(即您不会在中间暂停用户输入),它应该没问题.

If you are having genuine issues with concurrent updates on the same data, then you might consider performing the entire operation in a transaction - i.e. getting the data and committing it. As long as you treat the get/update/commit as a short-lived, atomic operation (i.e. you don't pause for user-input in the middle) it should be OK.

特别是,使用可序列化的隔离级别,没有人可以更新您拥有读锁的数据(即您查询的任何内容).唯一的问题是,如果不同的查询以不同的顺序读取数据,这可能会导致死锁场景.AFAIK,没有办法让 LINQ-to-SQL 发出 (UPDLOCK) 提示,这是一种耻辱.

In particular, with a serializable isolation level, nobody can update data that you have a read lock on (i.e. anything you have queried). The only problem is that this might lead to deadlock scenarios if different queries are reading data in different orders. AFAIK, there is no way to get LINQ-to-SQL to issue the (UPDLOCK) hint, which is a shame.

TransactionScope 或 SqlTransaction 都可以,只要它们被设置为可序列化隔离(这是 TransactionScope 的默认设置).

Either a TransactionScope or a SqlTransaction would do, as long as they are set as serializable isolation (which is the default for TransactionScope).

这篇关于LINQ to SQL 和并发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:SQL Server 2005 事务级和存储过程 下一篇:没有了

相关文章

最新文章