Transaction: Distributed Transaction TransactionScope, atomicity

Source: Internet
Author: User

Transaction is a program execution unit that accesses and may update various data items in the database ). Transactions are usually caused by the execution of user programs written in advanced database manipulation languages or programming languages (such as SQL, C ++, or Java), and are formatted as begin transaction and commit transaction.

Or rollback transaction statement (or callback function. A transaction is composed of all operations performed between the beginning (transin transaction) and the end (committransaction or rollback transaction) of the transaction. In SQL Server, transaction can be abbreviated as tran at the beginning or end of a transaction statement.

 

Things should have four attributes: atomicity, consistency, isolation, and continuity. These four attributes are generally called ACID properties.

 

Atomicity: a transaction is an inseparable unit of work. All operations in a transaction are either done or not done.

 
Consistency: A transaction must change the database from one consistent state to another. Consistency is closely related to atomicity.
 
Isolation: the execution of a transaction cannot be disturbed by other transactions. That is to say, operations and data used within a transaction are isolated from other concurrent transactions, and each transaction executed concurrently cannot interfere with each other.
 
Durability: permanence refers to a transaction that changes data in the database once committed. Other subsequent operations or faults should not have any impact on them.


 

 

Start the service
Distributed Transaction Coordinator (TrkWks)

Add reference
System. Transactions

Introduce namespace
Using System. Transactions;


Write code

 

Static void Main (string [] args) {// distributed transaction (atomicity, either all succeeded or all failed (rollback → SQL completed )) using (TransactionScope ts = new TransactionScope () {// Step 1 string SQL = "select top 1 loginId from dbo. sys_user where pwd = @ pwd; "; SqlParameter [] param = {new SqlParameter (" @ pwd ", System. data. sqlDbType. varChar, 16) {Value = "12" }}; Console. writeLine (SqlHelper. executeScalar (SQL, param ). toString (); // Step 2: string _ SQL = "select top 1 loginId from dbo. sys_user where pwd = @ pwd; "; SqlParameter [] _ param = {new SqlParameter (" @ pwd ", System. data. sqlDbType. varChar, 16) {Value = "1"}; Console. writeLine (SqlHelper. executeScalar (_ SQL, _ param ). toString (); // complete ts. complete ();} Console. readKey ();} static void Main (string [] args) {// distributed transaction (atomicity, either all succeeded or all failed (rollback → SQL completed )) using (TransactionScope ts = new TransactionScope () {// Step 1 string SQL = "select top 1 loginId from dbo. sys_user where pwd = @ pwd; "; SqlParameter [] param = {new SqlParameter (" @ pwd ", System. data. sqlDbType. varChar, 16) {Value = "12" }}; Console. writeLine (SqlHelper. executeScalar (SQL, param ). toString (); // Step 2: string _ SQL = "select top 1 loginId from dbo. sys_user where pwd = @ pwd; "; SqlParameter [] _ param = {new SqlParameter (" @ pwd ", System. data. sqlDbType. varChar, 16) {Value = "1"}; Console. writeLine (SqlHelper. executeScalar (_ SQL, _ param ). toString (); // complete ts. complete ();} Console. readKey ();}

 

Ts. Complete (); Transaction completion ts. Complete (); Transaction completion

 

Related Article

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.