Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TransactionScope vs. IDbTransaction

What are the advantages/disadvantages of using TransactionScope in comparison to IDbTransaction? I will suggest some - please correct/complete the list.

Advantages of TransactionScope:

  1. TransactionScope supports distributed transactions - you can access several datasources or use several connections to one datasource inside one transaction.
  2. TransactionScope is more declarative: we can nest TransactionScopes and it is more pleasant to use it services layer (we do not have to handle IDbConnection and IDbTransaction by ourselves).
  3. I'm not sure about the third point, but here it is. IDbTransaction is specific to connection - you have to keep connection open during the whole transaction. I'm not sure whether connection/connections should be opened during the whole TransactionScope (please clarify it). If not, the following workflow is possible: start transaction, open connection - query - retrieve - close connection, execute resource-intensive computation (keeping connections closed), open connection - query - retrieve - close connection, ..., commit transaction. But I guess it is impossible an TransactionScope keeps connections open till commit.

Disadvantages of TransactionScope:

  1. It doesn't support IsolationLevel change during transaction.
like image 566
SiberianGuy Avatar asked Mar 17 '11 09:03

SiberianGuy


1 Answers

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.

like image 115
Marc Gravell Avatar answered Sep 28 '22 23:09

Marc Gravell