TransactionScope: Avoiding Distributed Transactions

后端 未结 3 1518
逝去的感伤
逝去的感伤 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:48

    Many database ADO providers (such as Oracle ODP.NET) do indeed begin distributed transactions when you use TransactionScope to transact across multiple connections - even when they share the same connection string.

    Some providers, (like SQL2008 in .NET 3.5+) recognizes when a new connection is created in a transaction scope that refers to the same connection string, and will not result in DTC work. But any variance in the connection string (such as tuning parameters) may preclude this from occuring - and the behavior will revert to using a distributed transaction.

    Unfortunately, the only reliable means of ensuring your transactions will work together without creating a distributed transaction is to pass the connection object (or the IDbTransaction) to methods that need to "continue" on the same transaction.

    Sometimes it helps to elevate the connection to a member of the class in which you're doing the work, but this can create awkward situations - and complicates controlling the lifetime and disposal of the connection object (since it generally precludes use of the using statement).

提交回复
热议问题