A misconception about distributed transactions: Does using TransactionScope always turn on distributed transactions?

Source: Internet
Author: User

Background:

Transactions are a basic concept of a database management system, and transactions have four basic characteristics, namely, acid: atomicity (atomicity), consistency (consistency), isolation (isolation), and persistence (durability), The consistency and integrity of the database can be ensured through the transaction mechanism.

However, database transactions can only be transacted at the same session level of the DB instance. Distributed transactions can coordinate operations between multiple sessions of a DB instance, even database operations between multiple DB instances, and maintain transactional characteristics. In principle, however, we do not recommend the use of distributed transactions because distributed transactions consume more resources and perform poorly.

However, we have always misunderstood the code usage and effects of distributed transactions: will it be possible to open distributed transactions with TransactionScope?

Verify:

Let's do a simple demo: Two connection strings are exactly the same, ADO. NET will reuse connections in the connection pool, what will the result be?

using(TransactionScope ts =NewTransactionScope ())        {SqlConnection conn; Conn=NewSqlConnection ("server=.; UID=TKK123;PWD=AAAAAA"); Conn.        Open (); SqlCommand cmd=Conn.        CreateCommand (); Cmd.commandtext="Select 1 as TKK"; Cmd.        ExecuteNonQuery (); Conn.        Close (); Conn=NewSqlConnection ("server=.; UID=TKK123;PWD=AAAAAA"); Conn.        Open (); CMD=Conn.        CreateCommand (); Cmd.commandtext="Select 2 as TKK"; Cmd.        ExecuteNonQuery (); Conn.        Close ();    Ts.complete (); } Console.WriteLine ("OK"); Console.readkey ();

Strange thing happened, and did not see what we thought of the Distributed transaction!!!

We changed one of the connection strings so that ADO was a two data source, so that it would really turn on distributed transactions.

using(TransactionScope ts =NewTransactionScope ())        {SqlConnection conn; Conn=NewSqlConnection ("server=.; UID=TKK123;PWD=AAAAAA"); Conn.        Open (); SqlCommand cmd=Conn.        CreateCommand (); Cmd.commandtext="Select 1 as TKK"; Cmd.        ExecuteNonQuery (); Conn.        Close (); Conn=NewSqlConnection ( "server=.; UID=TKK123;PWD=AAAAAA; "); --Adds a semicolon, does not share the connection conn.        Open (); CMD=Conn.        CreateCommand (); Cmd.commandtext="Select 2 as TKK"; Cmd.        ExecuteNonQuery (); Conn.        Close ();    Ts.complete (); } Console.WriteLine ("OK"); Console.readkey ();

Let's look at how distributed transactions coordinate each database connection, the current case we are using the same database, so although two database connections are established, the same transaction ID is still the same at the database level.

If we open two different db instances, what will we see? Try it ...

A misconception about distributed transactions: Does using TransactionScope always turn on distributed transactions?

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.