C # Distributed Transaction Solution-transactionscope

Source: Internet
Author: User

cite someone else's guide:
In the actual development work, an event is executed, then another interface is invoked to insert the data, and if the processing logic is abnormal, the previously inserted data will be garbage data.
What we want is to be able to define the entire method as a transaction, and the TransactionScope class provides a simple method through which you do not have to interact with the transaction itself,
You can mark a block of code as participating in a transaction. The TransactionScope object creates a transaction and sets the transaction to the current property of the transaction class. First, the advantages of TransactionScope1, easy to use. TransactionScope can implement implicit transactions, allowing you to write data access layer code without taking into account the transaction, while at the business level of the control transaction. 2, you can implement distributed transactions, such as cross-Library or MSMQ. Second, TransactionScope shortcomings1, the price is not high. For example, you're just"Scope"The transaction that controls a library."TransactionScope"It's a little wasted.2, in general, as long as you use the"TransactionScope", you need to configure MSDTC, to be equipped with a firewall, to open 139 port. This port cannot be changed. If you have to use distributed transactions, you have to wonder1. Does this have to be done in the business? Is it worth rolling back the entire transaction if it has not been completed or failed? Are there no elegant compensation measures or fault-tolerant measures? 2. Distributed transactions involve a point that must be so much? Must be in real-time operation of this large string? Unable to refine some points by notifying class operations? 3after you initiate a distributed transaction, do you do transaction-independent operations, even though these operations are not related to transactions? (for example, reading data, computing, returning messages to other modules, and so on, etc.), knowing that the transaction should end as soon as possible. 4. You didn't put some reading into the business, did you? This is a very easy mistake, and you enlist a select operation in the transaction. 5. Your actions, some of the steps can be performed after all the operations have been completed. This type of operation has obvious notification class characteristics. The notification class action is said, I give you a notice, and I promise to inform you;
You have to take this notice and make sure the process is successful, but you don't have to deal with it when I tell you. Such an operation is obviously possible with another task. Iv. use of Distributed transactions note the following points1: Ensure that the machine participating in the transaction opens the distributed transaction support;2: If machine turns on the firewall, the MSDTC process needs to be set as an exception;3: The machine participating in the transaction cannot cross the domain (if the cross-domain, currently Microsoft does not have the exact solution);4:multi-database uses distributed transactions, and if it is the same database, it is best to use SqlTransaction.

1. Add the System.Transactions reference first (you need to add the net assembly)

The code for C # calls is as follows:

A clear explanation of this distributed transaction class blogs address: http://www.cnblogs.com/artech/archive/2010/01/31/1660356.html

usingSystem.Transactions;using(TransactionScope scope =NewTransactionScope ()) {    //trancation one do something ...    varDbo1 =Dbfactory.getdbfacade (Dbconnectionstringname.ysl_businesstraveltrade_readwrite); stringSQL1 ="UPDATE dbo. TestUser SET name= ' CCC ' WHERE id = 2"; Dbo1.    ExecuteNonQuery (CommandType.Text, SQL1); //trancation-something ...    vardbo =Dbfactory.getdbfacade (Dbconnectionstringname.ysl_businesstravel_readwrite); stringsql ="UPDATE dbo. TestUser SET name= ' CCC ' WHERE id = 2"; Dbo.    ExecuteNonQuery (CommandType.Text, SQL); //Submitscope.complete ();}

2. Setting up the Web server and SQL Server environment configuration

Component Services, management tools, Control Panel, system and security
(both the Web server and SQL Server need to be configured)

Services, management tools, control Panel, systems and security
(both the Web server and SQL Server need to be configured)

Control Panel, System and security->windows firewall
(both the Web server and SQL Server need to be configured)

3. MSSQL Configuration

For server connections, right-click Properties

4, configure the hosts (only configure the Web server, if not, on the SQL Server is also configured)

C:\Windows\System32\drivers\etchosts file, open with Notepad
(You need to configure SQL Server information in the Hosts file on the Web server)
(If the test does not pass, configure information on the Web server on the Hosts file on the SQL Server)

C # Distributed Transaction Solution-transactionscope

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.