.NET TransactionScope class and T-SQL TRAN COMMIT and ROLLBACK

前端 未结 4 1480
庸人自扰
庸人自扰 2020-12-11 20:11

I am current writing an application that will require multiple inserts, updates and deletes for my business entity. I am using the TransactionScope class to guarantee all t

相关标签:
4条回答
  • 2020-12-11 20:31

    ON 2005 its not necessary, on 2000 I would ,Also, i usually put the transactionscope in a "using" block.

    There are some performance issues when using it on 2000 and older vs 2005.

    See here

    Thanks

    0 讨论(0)
  • 2020-12-11 20:32

    If you are enlisted in a TransactionScope or CommittableTransaction then I would strongly recommend that you do NOT explicitly create your own local transactions using begin transaction or SqlConnection.BeginTransaction.

    TransactionScope/CommittableTransaction are a different 'family' and are mutually exclusive from begin transaction/SqlTransaction

    Therefore I would disagree with Saif Khan. It's true that System.Transactions has performance issues on Sql 2000, so it might be better to use SqlTransaction or begin transaction instead. However, if you do that then you should NOT also use a TransactionScope/CommittableTransaction.

    By the way, the behaviour that Marc Gravell described has been changed in .Net 4.0. Even if you don't use Explicit Unbind, it is no longer possible to have some commands rolled back and some committed. (However he is correct that in older versions you should use Explicit Unbind).

    0 讨论(0)
  • 2020-12-11 20:34

    You shouldn't need to - it should be handled within the TransactionScope. It does depend a little bit as to exactly what you are doing and how you are handling transaction (explicit or implicit) More HERE

    0 讨论(0)
  • 2020-12-11 20:50

    No, you don't need explicit transactions if using TransactionScope for your transactions - however: important you should probably set Transaction Binding=Explicit Unbind; in the connection string. The full details are here, but otherwise you can end up with the first few operations getting rolled back, and the last few committing (or rather, running outside of any transaction).

    0 讨论(0)
提交回复
热议问题