sqlserver Distributed Transaction Distributed Transaction

When developing a project with webapi+ef+sqlserver, when using transcope to implement application-level transactions, distributed transaction errors are occasionally reported, and they are very recurring, especially painful. Now I will preliminarily sort out my own solution.

Analysis of the reason: when building the repository+ef framework, savechange was written in the repository crud. And savechange itself contains transaction functions. When several services using the repository method are nested in transcope, a distributed transaction error will be reported, which I suspect is caused by transaction nesting.

Solution 1: Adjust the code structure, remove transcope, unify savechange in unitofwork, and submit transactions at one time (it is necessary to ensure that the DbContext used in the service in one request is one, which can be achieved by autofac dependency injection). instead of savechange in the repository.

Solution 2: Set the database server to support distributed transactions. (Not recommended for the time being, in some cases it is not a real distributed transaction, but a problem with its own framework and code)

https://bbs.csdn.net/topics/390682626/

# Open MSDTC on two machines [Administrative Tools---Component Services---My Computer "Properties"---MSDTC]
 
# Open SQL SERVER's distributed transaction support [Properties---Connection---Most The following check box]

#In the service: Distributed Transaction Coordinator set to automatically open #Database

agent service in the service: SQL Server Agent (MSSQLSERVER) set to automatically open
 
# Make sure that port 135 of both machines is open [Pay attention to the firewall]
 
# Make sure that the host name can be pinged, and the corresponding IP is correct [PING works, but the IP is incorrect, then configure the host

file ] #Ensure that the
 
SQL SERVER proxy service is enabled

 


#Make sure that the msdtc.exe process can be seen in the process if If you can't see it, please run "net start msdtc" under CMD and start the service

normally.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324982643&siteId=291194637