問(wèn)題描述
我們正在嘗試構(gòu)建大批量訂單記錄系統(tǒng).共有三個(gè)主要表:1. 訂單2. 訂單詳情3. 訂單發(fā)貨
We are trying to build a High-Volume Orders Record System. There are three primary tables: 1. Orders 2. OrderDetails 3. OrderShipment
發(fā)貨表包含每個(gè)訂單的 n 條記錄,任何記錄發(fā)貨條目都可以在客戶(hù)接受訂單之前更改,之后訂單將被凍結(jié).(業(yè)務(wù)需求)
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)
雖然這在現(xiàn)實(shí)世界中可能不會(huì)發(fā)生......在我們的負(fù)載測(cè)試期間,我們收到 System.Data.Linq.ChangeConflictException 異常.在事務(wù)中完成提交也無(wú)濟(jì)于事.我們不能強(qiáng)制 LINQ 在更新操作的整個(gè)持續(xù)時(shí)間內(nèi)鎖定該行嗎?
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?
有沒(méi)有其他方法可以解決這個(gè)問(wèn)題?
Is there any other way to get over this?
推薦答案
如果您對(duì)同一數(shù)據(jù)的并發(fā)更新有真正的問(wèn)題,那么您可以考慮在事務(wù)中執(zhí)行整個(gè)操作 - 即獲取數(shù)據(jù) 并 提交它.只要您將 get/update/commit 視為短期的原子操作(即您不會(huì)在中間暫停用戶(hù)輸入),它應(yīng)該沒(méi)問(wèn)題.
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.
特別是,使用可序列化的隔離級(jí)別,沒(méi)有人可以更新您擁有讀鎖的數(shù)據(jù)(即您查詢(xún)的任何內(nèi)容).唯一的問(wèn)題是,如果不同的查詢(xún)以不同的順序讀取數(shù)據(jù),這可能會(huì)導(dǎo)致死鎖場(chǎng)景.AFAIK,沒(méi)有辦法讓 LINQ-to-SQL 發(fā)出 (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 都可以,只要它們被設(shè)置為可序列化隔離(這是 TransactionScope 的默認(rèn)設(shè)置).
Either a TransactionScope or a SqlTransaction would do, as long as they are set as serializable isolation (which is the default for TransactionScope).
這篇關(guān)于LINQ to SQL 和并發(fā)問(wèn)題的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!