Transactions are often used for data persistence. Generally, ADO. the transaction processing in. NET can meet our needs, but, ADO.. NET transactions cannot be atomic operations performed by colleagues on multiple database connections. If multiple databases or file writes exist in your business environment, you can also consider using it to ensure data integrity and consistency.. NET distributed transaction processing.
The use of Distributed Transaction processing requires support from the Windows system. Therefore, we need to enable the system's MSDTC Service. Steps: Management Tools> component services; expand Console Root Node> component services> Computer> my computer; right-click my computer to open "properties "; on the tab, select "use local coordinator" and click "OK", as shown in:
After the configuration is complete, we can easily use distributed transaction processing. First, add "System. Transactions" reference to the project, and then write the following code:
{
{
TransactionOptions transactionOption. Timeout
{
Action. Invoke ();
Scope. Complete ();
}
}
}
The above code is the core code for executing the transaction. You only need to run the code as a delegate to pass in the InvokAction and enter a timeout value. At the end of the transaction execution, you can call the Complete method to submit the transaction. If the Complete method is not called, no commit is performed even if the transaction execution is Complete. If an exception occurs, all code will not change.
----------------------------------------------------------
Ps:
1. A problem is also found. The database also needs to enable support for distributed transactions: Right-click the "server connection" attribute, select "connection" in the Open Properties window, and on the right of the window, select "you need to use distributed transactions for server-to-server communication" and click OK.
2.20.specify the distribution port number, which is the exception of msdtc.exe under.
3. Cross-origin scenarios need to be studied.
4. In a transaction, even if nested data exists, all database connections in the same transaction are accessible to the data.
5. In a transaction, if you use the Response. Redirect () method to jump to another page, the transaction will not be committed!