問題描述
我們正在嘗試構建大批量訂單記錄系統.共有三個主要表: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模板網!