.NET distributed transaction processing

  Performing data persistence, we will be frequently used transactions. In general, ADO.NET Transaction processing will be able to meet our needs, however, ADO.NET colleagues in the transaction can not operate on multiple databases atomic connection; if there are multiple databases in your business environment , file write and other operations, and the need to ensure data integrity and consistency, you may consider using a distributed transaction processing provided by .NET.

  The use of distributed transaction processing, the need to support Windows systems, so we need a system of MSDTC service is turned on. Step: Administrative Tools> Component Services; expand Console Root> Component Services> Computers> My Computer; on the "My Computer," right-node open the "Properties"; check "Use local coordinator" in the tab , then click on the "OK" button, as shown below:

 

 

After the setup is complete, we can easily use a distributed transaction processing. First, add "System.Transactions" referenced in the project, and then write the following code:

 

TransactionEx class
    {
        /// <Summary>
        /// transaction performed by a method, and set the timeout
        /// </ Summary>
        /// <param name = "Action"> Agent Method </ param>
        /// < param name = "timeout"> timeout </ param>
        public static void InvokAction (the Action Action, int timeout)
        {
            TransactionOptions new new TransactionOptions TransactionOption = ();
            transactionOption.Timeout = TimeSpan.FromSeconds (timeout);

            the using (the TransactionScope scope = new new the TransactionScope (TransactionScopeOption.Required, TransactionOption))
            {
                action.Invoke ();
                scope.Complete ();
            }
        }
    }

 

This code is the core code above transactions are executed, you only need to execute code as a delegate passed InvokAction, and out of a timeout on it. At the end of transaction execution, call the Complete method, you can commit the transaction. If you do not call the Complete method, even if the transaction executed, it will not be submitted. If an exception occurs, all of the code will not change.

----------------------------------------------------------

ps:

1. also found a problem, also need to open the database to support distributed transactions: right "server connection" properties, open the Properties window select "Connect" in the right window, check the "need for distributed transactions server-to-server communication ", can be determined.

2. The need to open a distributed port, the port number is 135, or add msdtc.exe under System32 exception.

3. The cross-domain situation to be fine.

4. In the transaction, even if nested case where the presence of the same transaction data for all database connections are accessible.

5. In the transaction, if you use Response.Redirect () method to jump to another page, the transaction will not submit!

 

 

Reproduced in: https: //www.cnblogs.com/llbofchina/p/3668271.html

Guess you like

Origin blog.csdn.net/weixin_33805743/article/details/94206606