[C #] Exclude no need to incorporate the trading query, avoid unnecessary decentralized transactions (Distributed Transaction)

When the distributed transaction mechanism is accidentally enabled, and the environment setting does not allow it, it will generate [Disabled Distributed Transaction Manager (MSDTC) network access. Please use the component service system management tool to enable DTC to use the network access in the MSDTC security settings] error message, in this case, you can use ?new TransactionScope(TransactionScopeOption.Suppress) to exclude the need to be included Part of the affairs, to avoid the situation of scattered affairs.

Foreword


When operating different DB servers and needs to be completed under the same transaction, you need to use Distributed Transaction to complete; in Microsoft MSDTC (Distributed Transaction Coordinator) on the platform is used to coordinate different DBs to complete all actions in a single transaction, but sometimes we don’t really need this feature, but accidentally enable this mechanism, and environment settings do not allow it. Will generate [Disabled Distributed Transaction Manager (MSDTC) network access. Please use the component service system management tool to enable DTC to use the network access in the MSDTC security settings] error message, and how to eliminate it at this time? Please see the following actual example.

Example


Currently, the system uses two DBs for distribution, one can read/write, and the other read only for data query Use; when executing a function, a series of data transaction behaviors (connect to read / write db) need to be performed on the database, and relevant data needs to be retrieved for reference (connect to read only db), and to ensure that all transactions are completed in the same transaction , Will pack all logic in the transaction scope.

share picture

An error occurs


An error will be reported when the program is executed. The error message means that the current server configuration does not enable the DTC function, so if you use distributed transaction The mechanism will generate the error.

System.Transactions.TransactionManagerCommunicationException: Distributed Transaction Manager (MSDTC ) Network access. Please use the Component Services System Management Tool to enable DTC to use the network access in the MSDTC security settings. —> System.Runtime.InteropServices.COMException: The transaction manager has disabled support for remote/network transactions. (An exception occurs in HRESULT: 0x8004D024)

error elimination


For this case, because the behavior of connecting to read only DB does not need to be included in the transaction (the data is not changed, and has nothing to do with the transaction behavior), but the behavior is interspersed In each transaction, you can use the TransactionScopeOption.Suppress parameter to wrap the part that does not need to be included in the transaction and then wrap it with TransactionScop to avoid the situation of scattered transactions. The schematic code is as follows.

share picture

Reference information


.NET distributed transaction program development FAQ


I hope this article can help those in need

If the content is wrong or have other suggestions, please feel free to leave a message to the author!

Original: Large Column [C#] Exclude queries that do not need to be included in transactions to avoid unnecessary distributed transactions (Distributed Transaction)

Leave a Comment

Your email address will not be published.