TransactionScope distributed transactions, placing two database operations in one transaction __ database

Source: Internet
Author: User
Tags volatile

First, the concept

1. Reference the using System.Transactions namespace in the project (first to the reference to the Add net component);

There are three modes of TransactionScope:

Transactionscopeoptions

Describe

Required

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

RequiresNew

This transaction scope will create its own transaction.

Suppress

If it is within the scope of the current active transaction, the transaction scope neither joins the ambient transaction (ambient transaction) nor creates its own transaction. This option is available when part of the code needs to be left outside the transaction.

It is important to enter and exit transactions quickly, because transactions lock valuable resources. Best practices require that we create it before we need to use the transaction, open the connection quickly before executing the command, execute the action query, and complete and release the transaction as soon as possible. During the execution of a transaction, you should also avoid any unnecessary database-independent code, which prevents resources from being locked for a long time.


2, code example:

Transactionoptions transactionoption = new Transactionoptions ();

Set TRANSACTION ISOLATION LEVEL
Transactionoption.isolationlevel = System.Transactions.IsolationLevel.ReadCommitted;

Set a transaction timeout of 60 seconds
Transactionoption.timeout = new TimeSpan (0, 0, 60);

using (TransactionScope scope = new TransactionScope (transactionscopeoption.required, TransactionOption))
{
Try
{
To implement transactional work here
Send a message
Insertmessage (Senduserid, Touser, Content, sendedstatus);

Inserting records in the Receive information table
Receivecount + + insertreceivemessage (userids[0], Senduserid, content, "0");

No error, COMMIT transaction
Scope.complete ();
}
catch (Exception ex) {
throw new Exception ("Send an information exception, Reason:" +ex. message);
}finally{
Releasing resources
Scope. Dispose ();
}
}

3, in the TransactionScope the default transaction level is serializable, that is, in the transaction process, the complete lock table. Other processes cannot be queried, modified, added, deleted. This can lead to a significant reduction in efficiency, although data integrity is high. Usually we don't need that high data integrity. So the default transaction level needs to be modified:

All transaction levels are as follows:
Description of Member name
Chaos cannot overwrite pending changes in a higher isolation level transaction.
ReadCommitted cannot read volatile data during a transaction, but you can modify it.
ReadUncommitted can read and modify variable data during a transaction.
RepeatableRead can read variable data during a transaction, but it cannot be modified. You can add new data during a transaction.
Serializable can read volatile data during a transaction, but it cannot be modified, and no new data can be added.
Snapshot can read variable data. Before a transaction modifies data, it verifies whether another transaction has changed the data after it was originally read. If the data has been updated, an error is raised. This enables the transaction to obtain the previously committed data value.
When an attempt is made to promote a transaction created with this isolation level, a InvalidOperationException is thrown and an error message "Transactions with IsolationLevel Snapshot cannot is Promoted (cannot promote a transaction with a IsolationLevel snapshot).

Unspecified is using a different isolation level than the specified isolation level, but the level cannot be determined. If this value is set, an exception is thrown.

II. Integration with Database services

//Create TransactionScope
using (TransactionScope tscope= new TransactionScope ())
{
 using ( SqlConnection cn2005= New SqlConnection (someSql2005))
 {
 sqlcommand cmd= new SqlCommand (Sqlupdate, CN2005);
 cn2005. Open ();
 cmd. ExecuteNonQuery ();
 }

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.