The foundation is the top priority ~. Explicit transactions and implicit transactions in. net

Source: Internet
Author: User

Back to directory

. Net transactions can be divided into explicit and stable transactions. explicit means that we need to manually specify the transaction commit and rollback, while stable is. net helps us manage your transactions.CodeWhen there is no exception in the segment, it will help us to commit, and vice versa, it is reasonable to perform transaction rollback.

The description in msdn is as follows:

CommittabletransactionClass is applicationProgramTransactions provide an explicit method instead of implicitly using the transactionscope class. AndTransactionscopeDifferent classes, the application writer needs to explicitly call the commit and rollback methods to commit or stop transactions. However, only the creator of the transaction can commit the transaction. Therefore, the replica of the transaction that can be committed obtained through the clone method is not recommendable.

Explicit transaction:

CreateCommittabletransactionEnvironment transactions are not automatically set (Environment transactions are the transactions in which your code executes ). You can obtain or set environmental transactions by calling the static current attribute of the global transaction object. For more information about environment transactions, see the section "Managing transaction flow using transactionscotion tion" (using transactionscopeoption to manage transaction flows) that uses transaction scopes to implement implicit transaction topics. If no environment transaction is set, any operations on the resource manager do not belong to this transaction. You need to explicitly set or reset the environment transaction to ensure that the resource manager operates in the correct transaction context.

Before submittingCommittabletransactionPreviously, all the resources involved by the firm were still locked.

CommittabletransactionObjects cannot be reused. Once committed or rolled back, you cannot use it again in the transaction or set it as the transaction context of the current environment.

Stable transactions:

InNewStatement instantiationTransactionscopeThe transaction Manager determines which transaction to participate in. Once determined, this range will always be involved in this transaction. This decision is based on two factors: whether the transaction exists in the environment and the constructor.TransactionscospontionParameter value. Environment transactions are the transactions in which your code is executed. By callingTransactionThe current static attribute of the class can be used to obtain references to environment transactions. For more information about how to use this parameter, see the "transaction flow management" section for implementing implicit transaction themes using transaction scopes.

If it is in the transaction scope (that is, from InitializationTransactionscopeObject To call its dispose method)If no exception occurs, allow the transaction involved in this range to continue.. If an exception does occur in the transaction scope, the transactions it participates in will be rolled back.

After the application completes all the work it needs to execute in a transaction,You should only callCompleteMethod once to notify the Transaction Manager to accept the commit transaction. Failed to call this method will abort the transaction.

The call to the dispose method indicates the end of the transaction range. The exception that occurs after this method is called does not affect the transaction.

If it is modified in the rangeCurrentWill be calledDisposeException. However, when the range ends, the previous value is restored. In addition, ifCurrentCallDispose, The transaction is aborted at the end of the corresponding range.

The transaction format is as follows:

 1   Using (Transactionscope scope = New  Transactionscope ())  2   {  3    Try  4   {  5       //  Code segment 6   Scope. Complete ();  7   }  8    Catch  (Exception)  9   {  10      Throw  ;  11   }  12     Finally  13   { 14 Scope. Dispose (); //  Manually release transactions  15   }  16   }  17 }

Thank you for reading this article.

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: 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.