This article is a mirror article of machine translation, please click here to jump to the original article.

View: 25346|Reply: 2

[Source] Use of TransactionScope in C#

[Copy link]
Posted on 4/10/2017 10:16:25 AM | | | |
TransactionScope

TransactionScope is a class in System.Transactions, which is a class that comes with the .NET framework. If the TransactionScope is released without calling the Complete method (TransactionScope implements the IDisposible interface, using block is recommended), then it will consider the operation to fail and will roll back the execution.
TransactionScope is a useful API that manages transactions around ("around" means that databases that support TransactionScope can automatically manage transactions), and most mainstream databases support this API, including Microsoft's own MSSQL, of course.
If you are using a database or some transaction-related system that does not support TransactionScope, you can still use the interceptor, but you will have to modify your code to use a suitable API that supports transactions (e.g., using the BeginTransaction API to get the database provider's implementation of IDbTransactions).



The above is a custom exception that I deliberately threw to see if it can be rolled back normally, and the picture below is normal execution, without exceptions.




Calling the TransactionScope's Complete method indicates that the transaction is successfully executed. Of course, if the transaction is always executed, then the transaction is not needed. The reason why there are transactions is to solve the problem of failure in multiple operations, and if there is a failure, it will be rolled back.
Because the . .NET's TransactionScope, there is no explicit rollback call, and the closest equivalent is to use the Dispose method. If the TransactionScope is released before the Complete method is called, then the TransactionScope performs a rollback. Therefore, a Dispose call needs to be added to the transaction interceptor face to perform the rollback.


Finally, attach the code!


Attached is the source code:
Tourists, if you want to see the hidden content of this post, pleaseReply






Previous:The CREATE DATABASE statement is not allowed within a multi-statement transaction
Next:Make Linq's OrderBy support dynamic fields
 Landlord| Posted on 4/10/2017 10:20:30 AM |
The TransactionScope class can be used nested, and if you want to use it nested, you need to specify the TransactionScopeOption parameter in the nested transaction block. The default parameter is Required.

When the TransactionScopeOption of the nested class TransactionScope is Required, you can see the following result, their transactions have the same ID. Moreover, it is only true success when both TransactionScopes are complete.

If you set TransactionScopeOption to RequiresNew, the nested transaction blocks and the outer transaction blocks are independent and do not affect each other.

TransactionScopeOption is set to Suppress to cancel the transaction of the current block, which is generally rarely used.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com