Exploring logical transactions TransactionScope and transactionscope

Source: Internet
Author: User

Exploring logical transactions TransactionScope and transactionscope
1. What is TransactionScope?

TransactionScope is a range transaction (similar to a transaction in a database). It ensures that all data modification operations within the scope of the transaction statement are consistent, either all succeeded or all failed rollback.

MSDN: If no exception occurs within the transaction scope (that is, the TransactionScope object is initialized and Its Dispose method is called), the transaction involved in the range can continue, otherwise, the transactions involved will be rolled back.
When the application wants to execute all the work in the transaction, it should call the Complete method once to notify the transaction manager that the transaction is acceptable (at this time the transaction is not committed) to commit the transaction, failed to call this method to stop the transaction.
Calling the Dispose method marks the end of the transaction range. The exception that occurs after this method is called does not affect the transaction.

Ii. What is the use of TransactionScope?

Assume that there is a demand: to implement the order function, the main business includes deducting goods inventory, deducting user account balance, generating order records and recording logs. To ensure data consistency, we usually create an order transaction in the database, and then the program calls the transaction to pass in the corresponding parameters. The problem arises. If the user's account data and order data are in different databases, they will not be able to complete all the tasks in the same database transaction, and they will not be able to guarantee data consistency.
Recently, as the business change company switched to the MySQL database, it was customary to write transactions first when processing data changes. When writing, it found that there was no transaction in the existing database, so I asked the java group, how can I ensure data consistency without transactions? The answer is: what is the transaction? spring helps us solve all the problems .... Immediately ,. net has not heard of Spring (it is said that there is a similar framework), although you can consider using warehousing and work units to solve the problem, it feels very troublesome, later, we found TransactionScope when looking for a solution.

Iii. How to Use TransactionScope?
1 try 2 {3 using (TransactionScope scope = new TransactionScope () 4 {5 // TODO: Data Processing Service 6 scope. complete (); 7} 8} 9 catch (Exception ex) 10 {11 throw ex; 12}
Iv. Questions 1. Preparations
1 // Database Access Object 2 public class Studuent 3 {4 public static IList <StudentModel> GetList () 5 {6 // dirty read 7 string SQL = "SELECT Id, name, CreateTime FROM Student order by Id DESC; "; 8 DataTable dt = SQLHelper. executeDataTable (CommandType. text, SQL); 9 return dt. toModel <StudentModel> (); 10} 11 12 public static bool Add (string name) 13 {14 SqlParameter [] parms = {new SqlParameter ("@ Name", SqlDbType. NVarChar, 32) {Value = name }}; 15 string SQL = "INSERT INTO Student (Name) VALUES (@ Name)"; 16 return SQLHelper. executeNonQuery (CommandType. text, SQL, parms)> 0; 17} 18 19 public static void Clear () 20 {21 SQLHelper. executeNonQuery ("DELETE Student"); 22} 23}
1 // public method: outputs the student list 2 static void PrintStudent () 3 {4 IList <StudentModel> list = Studuent. getList (); 5 foreach (var item in list) 6 {7 Console. writeLine ("{0} \ t {1} \ t {2}", Thread. currentThread. managedThreadId, item. name, item. createTime); 8} 9 Console. writeLine (); 10}
2. When will a transaction be committed?

Initially I thought that the transaction was submitted immediately after the Complete was executed, but according to the output, the transaction was still not committed two seconds after the Complete method was executed. Because dirty reading is not allowed, the primary thread can query the Student table after the Transaction Completes the operations, but the Student list is output after the scope calls the dispose method, MSDN introduces that dispose determines the end of the transaction range, so it is assumed that the transaction is committed when dispose is called.

3. How does an exception cause data not to be submitted?

Comparing the two figures, we can see that exceptions that occur after the Complete do not affect the commit of the transaction. The transaction is not committed because the Complete is not executed due to exceptions. I have read an article saying that if the TransactionScope scope does not call Complete, it will cause a program exception. I think he must be joking...

4. nested 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.