TransactionScope: Avoiding Distributed Transactions

后端 未结 3 1550
逝去的感伤
逝去的感伤 2020-12-05 05:14

I have a parent object (part of a DAL) that contains, amongst other things, a collection (List) of child objects.

When I\'m saving the object b

3条回答
  •  我在风中等你
    2020-12-05 05:46

    Empirically, I have determined that (for the SQL Server provider) if the process can take advantage of connection pooling to share the connection (and the transaction) between the parent and child processes, the DTC will not necessarily become involved.

    This is a big "if", however, as per your example, the connection created by the parent process cannot be shared by the child processes (you do not close/release the connection before invoking the child processes). This will result in a transaction that spans two actual connections, which will result in the transaction being promoted to a distributed transaction.

    It seems that it would be easy to refactor your code to avoid this scenario: just close the connection created by the parent process before invoking the child processes.

提交回复
热议问题