LINQ those Things (3)-Transaction and concurrency conflict handling

Source: Internet
Author: User

Linq2sql adopts an open concurrency mechanism to deal with data inconsistency caused by database concurrent modification or time difference between reading and modifying. To implement this mechanism, LINQ2SQL will first open conflict detection when submitting data changes, and provides a series of methods for resolving conflicts when they are found.

The Concurrency conflict processing of Linq2sql adopts the optimistic concurrency (optimistic concurrency) mechanism. When a data update occurs, Linq2sql checks that the current database value and the original value returned by the query are equal, and if equal indicates that the database value has not changed, that no conflicts have occurred, that the update can be committed, or that when the database value changes, Throws Changeconflictexception, waiting for the user to decide how to handle the update.

Resources:

Optimistic Concurrency Overview http://msdn.microsoft.com/zh-cn/library/bb399373.aspx

This article will use a number of nouns, in order to avoid misunderstanding, first of all, the following, for a entity object properties:

1 database value: When concurrency conflicts occur, the database saved field values

2 Original value: The field value obtained by DataContext query. Because of the time interval from the DataContext query to the data update, the original value may be different from the database value.

Current value: through entity. property to get the value of the attribute. When you first query from DataContext, the current value = original value, but the current value may be modified in some operations, so the current and original values may be different.
Open conflict Detection

Only actions involving update and delete that can cause optimistic concurrency are detected by LINQ to detect conflicts by executing the action return value (@ @ROW_COUNT) of the SQL statement for UPDATE or delete.

Let's execute the following code (using the Northwnd database):

1 var context = GenerateContext();
2 context.Log = Console.Out;
3 Customer customer = context.Customers.First();
4 customer.CompanyName = "Updating customer";
5 context.SubmitChanges();

The resulting SQL statement is as follows, looking at the WHERE clause and the parameters passed

UPDATE [dbo].[ Customers]
"SET [CompanyName] = @p10
" WHERE ([CustomerID] = @p0) and ([CompanyName] = @p1) and ([ContactName] = @p2) A
ND ([ContactTitle] = @p3) and ([address] = @p4) and ([City] = @p5) and ([Region]
to NULL) and ([Po  Stalcode] = @p6) and ([Country] = @p7) and ([Phone] = @p8) and
([Fax] = @p9)
--@p0: Input NChar (Size = 5; Prec = 0; Scale = 0 [ALFKI]
--@p1: Input NVarChar (Size = 19; Prec = 0; Scale = 0) [Alfreds Futterkiste]
--@p2: Input NVarChar (Size = 12; Prec = 0; Scale = 0) [Maria Anders]
--@p3: Input NVarChar (Size = 20; Prec = 0; Scale = 0) [Sales Representative]
one--@p4: Input NVarChar (Size = 13; Prec = 0; Scale = 0 (obere Str.)
--@p5: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [Berlin]
--@p6: Input NVarChar (Size = 5; Prec = 0; Scale = 0) [12209]
--@p7: Input NVarChar (Size = 7; Prec = 0; Scale = 0) [Germany]
--@p8: Input NVarChar (Size = 11; Prec = 0; Scale = 0) [030-0074321]
--@p9: Input NVarChar (Size = 11; Prec = 0; Scale = 0 [030-0076545]
--@p10: Input NVarChar (Size = 17; Prec = 0; Scale = 0) [Updating customer]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.