開放式並行存取:總覽Optimistic Concurrency: Overview

LINQ to SQLLINQ to SQL 支援開放式並行存取 (Optimistic Concurrency) 控制。supports optimistic concurrency control. 下表描述適用于檔中LINQ to SQLLINQ to SQL開放式平行存取的詞彙:The following table describes terms that apply to optimistic concurrency in LINQ to SQLLINQ to SQL documentation:

詞彙Terms 描述Description
並行concurrency 兩位以上的使用者同時嘗試更新相同資料庫資料列的情況。The situation in which two or more users at the same time try to update the same database row.
並行衝突concurrency conflict 兩位以上的使用者同時嘗試將衝突值送出給資料列的一個或多個資料行的情況。The situation in which two or more users at the same time try to submit conflicting values to one or more columns of a row.
並行控制concurrency control 用來解決並行衝突的技術。The technique used to resolve concurrency conflicts.
開放式並行存取控制項optimistic concurrency control 此種技術會先檢查資料列中的其他異動是否已變更值,才允許送出變更。The technique that first investigates whether other transactions have changed values in a row before permitting changes to be submitted.

與封閉式並行控制相反,這會鎖定記錄以避免發生並行衝突。Contrast with pessimistic concurrency control, which locks the record to avoid concurrency conflicts.

所謂的開放式控制項,因為它會將一個交易干擾另一個的機會視為不太可能。Optimistic control is so termed because it considers the chances of one transaction interfering with another to be unlikely.
衝突的解決方式conflict resolution 透過再次查詢資料庫,然後調整差異,以重新整理衝突項目的處理流程。The process of refreshing a conflicting item by querying the database again and then reconciling differences.

重新整理物件時,LINQ to SQLLINQ to SQL 變更 Tracker 會保留下列資料:When an object is refreshed, the LINQ to SQLLINQ to SQL change tracker holds the following data:

-原本從資料庫取得,並用於更新檢查的值。- The values originally taken from the database and used for the update check.
-來自後續查詢的新資料庫值。- The new database values from the subsequent query.

LINQ to SQLLINQ to SQL 接著會判斷物件是否發生衝突 (也就是,它的其中一個或多個成員值是否變更)。then determines whether the object is in conflict (that is, whether one or more of its member values has changed). 如果物件衝突, LINQ to SQLLINQ to SQL接下來會決定哪一個成員發生衝突。If the object is in conflict, LINQ to SQLLINQ to SQL next determines which of its members are in conflict.

LINQ to SQLLINQ to SQL 發現的所有成員衝突都會加入至衝突清單中。Any member conflict that LINQ to SQLLINQ to SQL discovers is added to a conflict list.

在物件模型中,當下列兩個條件都成立時,就會發生開放式平行存取衝突: LINQ to SQLLINQ to SQLIn the LINQ to SQLLINQ to SQL object model, an optimistic concurrency conflict occurs when both of the following conditions are true:

  • 用戶端嘗試將變更送出給資料庫。The client tries to submit changes to the database.

  • 資料庫中的一個或多個更新檢查值,自用戶端最後一次讀取之後已更新過。One or more update-check values have been updated in the database since the client last read them.

解決這項衝突包括探索哪些物件成員發生衝突,然後決定要採取的動作。Resolution of this conflict includes discovering which members of the object are in conflict, and then deciding what you want to do about it.

注意

只有對應成 AlwaysWhenChanged 的成員才會參與開放式並行存取檢查。Only members mapped as Always or WhenChanged participate in optimistic concurrency checks. 而不會檢查標記為 Never 的成員。No check is performed for members marked Never. 如需詳細資訊,請參閱 UpdateCheckFor more information, see UpdateCheck.

範例Example

例如,在下列案例中,User1 查詢資料庫中的資料列,開始準備更新。For example, in the following scenario, User1 starts to prepare an update by querying the database for a row. User1 會接收到值為 Alfreds、Maria 和 Sales 的一個資料列。User1 receives a row with values of Alfreds, Maria, and Sales.

User1 想將 Manager 資料行的值變更為 Alfred,並將 Department 資料行的值變更為 Marketing。User1 wants to change the value of the Manager column to Alfred and the value of the Department column to Marketing. 但是在 User1 送出那些變更之前,User2 就已經先送出變更給資料庫。Before User1 can submit those changes, User2 has submitted changes to the database. 因此,現在 Assistant 資料行的值已變更為 Mary,而 Department 資料行的值已變更為 Service。So now the value of the Assistant column has been changed to Mary and the value of the Department column to Service.

如果 User1 現在嘗試送出變更,則送出會失敗,而且會擲回 ChangeConflictException 例外狀況。When User1 now tries to submit changes, the submission fails and a ChangeConflictException exception is thrown. 因為 Assistant 資料行和 Department 資料行的資料庫值並不是所預期的值,所以會發生這種結果。This result occurs because the database values for the Assistant column and the Department column are not those that were expected. 因此表示 Assistant 和 Department 資料行的成員會產生衝突。Members representing the Assistant and Department columns are in conflict. 下表將摘要說明這種情況。The following table summarizes the situation.

主管Manager AssistantAssistant 部門Department
原始狀態Original state AlfredsAlfreds MariaMaria SalesSales
User1User1 AlfredAlfred 行銷Marketing
User2User2 MaryMary 服務Service

您可以使用不同方式來解決這類衝突。You can resolve conflicts such as this in different ways. 如需詳細資訊,請參閱如何:管理變更衝突For more information, see How to: Manage Change Conflicts.

衝突偵測和解決檢查清單Conflict Detection and Resolution Checklist

您可以偵測和解決任何精細度等級的衝突。You can detect and resolve conflicts at any level of detail. 其中一種極端的做法是,使用三種方式的其中一種方式來解決所有的衝突 (請參閱 RefreshMode),而不考慮其他事項。At one extreme, you can resolve all conflicts in one of three ways (see RefreshMode) without additional consideration. 另一種極端的做法是,指定每個衝突成員之每種衝突類型的特定動作。At the other extreme, you can designate a specific action for each type of conflict on every member in conflict.

支援衝突探索和解決的 LINQ to SQL 型別LINQ to SQL Types That Support Conflict Discovery and Resolution

LINQ to SQLLINQ to SQL 中支援開放式並行存取衝突解決的類別和功能包括:Classes and features to support the resolution of conflicts in optimistic concurrency in LINQ to SQLLINQ to SQL include the following:

另請參閱See also