The foundation is the top priority ~ Do you really know transactionscope?

Source: Internet
Author: User
ArticleDirectory
    • Understand transaction options and use it to control nested transactions
    • Second, when will it be upgraded to a distributed transaction (when MSDTC request is initiated)
    • Iii. reflected in LINQ to SQL. For a data context, due to improper operations, multiple connection connections will also be generated.

Back to directory

Do you really know transactionscope?

Related Articles

Change uncertainty to confirmation ~ When to upgrade transactionscope to a distributed transaction

Change uncertainty to confirmation ~ When is transactionscope upgraded to a distributed transaction ~ Continued

Change uncertainty to confirmation ~ When is transactionscope upgraded to a distributed transaction ~ Continue (avoid unnecessary MSDTC)

Understand transaction options and use it to control nested transactions

 

  Public   Enum   transactionscospontion { ///   If an environment transaction exists (I think it should be called a nested transaction, an atmosphere transaction, ambient transaction ), then join the transaction. 
If the transaction does not exist, create a new transaction. If a transaction is added, the transaction is committed only after the environment transaction is committed. required = 0 , // This transaction scope will create your own transactions and will not be added to the current external transaction requiresnew = 1 , /// if the transaction is within the scope of the currently active transaction, the transaction scope will not be added to the ambient transaction (ambient T ). Or create a transaction.
this option can be used when some Code needs to be left outside the transaction. suppress = 2 ,}

 

Second, when will it be upgraded to a distributed transaction (when MSDTC request is initiated)

When your web server and database server are on the same computer,When performing operations on the same database, it is not upgraded to a distributed transaction.

When your web server and database server are on the same computer,When multiple data contexts are created for the same database, they are not upgraded to distributed transactions.

When your web server and database server are on the same computer,When you operate tables in two databases, this will be promoted to distributed tasks.

When your web server and database server are not on the same computer,If more than one connection is established, MSDTC is initiated.

Iii. reflected in LINQ to SQL. For a data context, due to improper operations, multiple connection connections will also be generated.

 

There is no problem with this method. It is an event and has clear responsibilities. After the task is completed, it passes through base. when the submitchange () method is submitted to the database, a connection is established, an SQL statement is sent, and the communication is completed.

However, sometimes this Dal layer action needs to be combined by the businesses of the BLL layer. The bll layer can call multiple similar Dal methods within a transaction scope, at this time, they will regard this method as a separateIndependent Work UnitThis is what we don't want to see, because in our understanding, a BLL business should be a work order element, and it should be returned to the code, after multiple Dal-layer methods are interpreted as SQL statements, it is okay to submit them once. Therefore, the above Code may produce some bad taste after being combined!

Solution:

The submitchanges () method that opens the data context to the BLL layer and whether to submit the attribute issubmitchange. The bll layer submits the submitchanges () method in a unified manner () to determine whether issubmitchange is true, if yes, and then submit, of course, this issubmitchange is true by default, that is, each call to a separate method will complete this submission, which is normal, yesOperation integrity,

When BLL combines methods, it first sets the required issubmitchange to false. At this time, the Dal method will not be submitted, waiting for BLL submitchange ().

Core code:

The default submission attribute issubmitchange is true.

 

 

For The bll layer, we publish a unified operation object, which will overwrite the submitchanges method of the base class. Its method modifier is public and public to The bll layer.

Bllbase of the base class of The bll layer. You can assign values to the issumitchange attribute through the creation method.

 

OK. Here, our BLL can control when the code is submitted to the database whether it is to call other BLL business combinations or to call the Dal layer method combinations. For a business object, it should be a work unit.

Back to directory

 

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.