What are the advantages/disadvantages of using TransactionScope in comparison to IDbTransaction? I will suggest some - please correct/complete the list.
Advantages of TransactionScope:
Disadvantages of TransactionScope:
convenience counts for a lot - especially as it can be used to wrap code that you can't control (since the code you wrap will enlist automatically, by default). This means you can wrap pre-existing libraries that use the server
performance takes a slight hit, but note that in many cases you'll be using the lightweight transaction manager, not DTC - this means you don't pay full DTC costs.
another disadvantage is that nested transactions can't be rolled back; any rollback immediately rolls back the outer transaction. Personally I like this approach; if things get ill - stop doing them ASAP.
Re your query in point 3; you can open/close as many connections as you like inside the transaction scope, without affecting the behaviour, except you may find (depending on circumstances) that your transaction elevates to DTC. It is pretty-much guaranteed to elevate if you talk to multiple trnasaction-aware servers.
Another difference: different timeouts apply, especially if DTC gets involved. Which makes sense: a long-running distributed transaction is poison, and may indicate a cross-server deadlock. Deadlocks are usually detected on a single server, but are pretty much impossible to spot automatically when distributed, so a hard timeout is essential.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With