Distributed Transaction transactionscope

Source: Internet
Author: User

How to Apply

Start a transaction

// ------------------------------------------------ Transactionscope -------------------------------------------------------------------

Using (transactionscope scope =

New transactionscope (transactionscopeoption. required, new transactionoptions {isolationlevel = isolationlevel. repeatableread }))

{

// Todo something

// Todo something

// Todo something

Submit the transaction scope. Complete ();

}

The transaction has no rollback. If an exception occurs in the transaction, the transaction will be automatically rolled back, provided that the transaction must be referenced using the using statement.

Transactionscospontion. required transaction type. If a transaction exists in the current environment, it is automatically appended to the current transaction and becomes the subtransaction of the transaction.

The details are as follows:

Required

If a transaction already exists, the transaction scope will be added to the existing transaction. Otherwise, it will create its own transaction.

Requiresnew

This transaction scope will create your own transactions.

Suppress

If the transaction is within the scope of the current active transaction, the transaction scope will neither join the ambient transaction nor create your own transaction. This option can be used when some code needs to be left outside the transaction.

Note:

A connection to multiple different connection strings cannot be enabled in a scope. Otherwise, an exception occurs. solution: to enable a new connection string connection, you can place the sub-connection in a nested scope, as follows:

Using (transactionscope fatherscope =

New transactionscope (transactionscopeoption. required, new transactionoptions {isolationlevel = isolationlevel. repeatableread }))

{

Using (transactionscope chidlscope =

New transactionscope (transactionscopeoption. required, new transactionoptions {isolationlevel = isolationlevel. repeatableread }))

{

// Add a new string connection and perform cross-database operations.

Chidlscope. Complete ();

}

Fatherscope. Complete ();

}

// ------------------------------------------------ Sqltransaction -------------------------------------------------------------------

using (SqlConnection conn = new SqlConnection("conn"))            {                conn.Open();                 using (var tran = conn.BeginTransaction())                {                    //to do something                     tran.Commit();                }                 conn.Close();            }

Compared with sqlconnection. Transaction

1. the scope does not show that the transaction is enabled, so it can only be used once.

2. When the scope transaction reaches a certain level, the transaction will rise to the distributed level.

3. sqltransaction is significantly more efficient at the data layer, and it is convenient to enable and disable transactions when you need to query, improving the overall performance.

4. sqltransaction transactions can only be completed within one connection. scope can be connected across multiple databases (must be the same connection string)

In general

Determine the transaction to be applied based on business needs to ensure data integrity

Tips

Reference: http://www.cnblogs.com/blsong/archive/2010/08/13/1798987.html

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.