I wonder how the DataContext handles concurrency violation.
For example -
Two users are fetching some data from the database, then some of them change some row and co开发者_运维知识库mmit changes, then other user trying commit their changes so a ChangeConflictException
should occur, but how does the DataContext know that the data has changed?
Fetching this data again and comparing? Or some database notification mechanism?
Concurrency control can be done by using either TimeStamp column in database or UpdateCheck attribute in LINQ-to-SQL.
MSDN Concurrency Overview (LINQ-to-SQL)
MSDN How to manage change conflicts (LINQ-to-SQL)
The previous values of all columns that aren't being changed and are marked UpdateCheck are included in the where clause of the generated update statement. If the update affected 1 row, all is well- if it affected 0 rows (eg, somebody else changed one of those values, hence it couldn't locate the row after filtering), you get the ChangeConflictException.
Yes it fetches the data again to validate concurrency.
The LINQ to SQL employs optimistic concurrency control, which means L2S checks the status of data as opposed to locking the data. You can specify which column L2S should use to determine if data is changed or not. By default it would compare every column.
See Understanding LINQ to SQL (9) Concurrent Conflict for in depth discussion.
精彩评论