TransactionScope: Avoiding Distributed Transactions

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

    In your example the TransactionScope is still in the context of a method, you could simply create a SqlTransaction with multiple commands beneath that. Use TransactionScope if you want to move the transaction out of a method, to say, the caller of that method, or if you access multiple databases.

    Update: never mind I just spotted the child call. In this situation, you could pass the connection object to child classes. Also, you don't need to manually dispose the TransactionScope - using blocks act like try-finally blocks and will execute the dispose even on exceptions.

    Update 2: better yet, pass the IDbTransaction to the child class. The connection can be retrieved from that.

提交回复
热议问题