Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of LINQ TO SQL distributed transaction

Shulou Source: shulou.com Published: 2022-06-02 09:03:12 10月12日 Update

This article introduces the knowledge of "LINQ TO SQL distributed transaction example Analysis". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

LINQ TO SQL, as its name implies, involves database operations. Then there is the concept of a transaction, for example, if we need to insert two entities at once, but want to ensure the integrity of the two operations.

1. Implicit transaction

In fact, even if we don't write code, LINQ TO SQL will automatically create a transaction. The default transaction isolation level is ReadCommitted

two。 Explicit transaction

We can also write our own code to explicitly control the transaction. The default transaction isolation level is ReadCommitted.

3. LINQ TO SQL distributed transaction

We can also use TransactionScope objects to define LINQ TO SQL distributed transactions.

Note: the default isolation level of TransactionScope is serializable, which may cause concurrent lock conflicts.

* probably talk about concurrency control: the problem is that at some point, a client reads the data, then it disconnects and modifies locally; at the same time, another client also reads the data and modifies and submits it. At this point, if the * * clients update again, there should be a problem. This is called the concurrency problem.

LINQ TO SQL defines concurrency control behavior by specifying ConflictMode in the submitchanges method:

◆ FailOnFirstConflict (default)

◆ ContinueOnConflict

Here is an example of continuing to operate in case of concurrency conflicts and resolving conflicts

Var query = from p in ctx.Products where p.CategoryID = = 1 select p; foreach (var p in query) p.UnitsInStock = Convert.ToInt16 (p.UnitsInStock-1); try {ctx.SubmitChanges (ConflictMode.ContinueOnConflict);} catch (ChangeConflictException) {foreach (ObjectChangeConflict cc in ctx.ChangeConflicts) {Product p = (Product) cc.Object; Reponse.Write (p.ProductID + "); cc.Resolve (RefreshMode.OverwriteCurrentValues) / / abandon the current update, all updates shall prevail}} the content of "LINQ TO SQL distributed transaction example Analysis" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Transaction problem distributed update client client data level conflict control isolation analysis two code content time more knowledge utility as the name implies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux Xiaomi vpn Microsoft