Do I need "transactionScope.Complete();"?

.net, c#, linq-to-sql, sql, sql-server

Solution

So my question is, must it be used or not?

`Complete` must be used when updates are made in order to `COMMIT` the transaction. Otherwise, the transaction manger will issue a `ROLLBACK` and undo changes made.

With a read-only transaction like your example, I can think of no material difference with or without `Complete`. The `COMMIT` or `ROLLBACK` issued by the transaction manager will have the same net effect of releasing locks and resources held by the transaction in both cases.

Although it's not a requirement to invoke `Complete` in a read-only transaction, it's still a best practice IMHO. Consider the poor developer who later unwittingly adds data modification code to your transaction block without seeing `Complete` is missing.

Problem

As far as I understand, the "correct" way to use a `TransactionScope` is to always call `transactionScope.Complete();` before exiting the `using` block. Like this: ``` using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted })) { //... //I'm using this as a NOLOCK-alternative in Linq2sql. transactionScope.Complete(); } ``` However, I've seen that the code works without it, and even the answer I've learnt to use it from omits it. So my question is, must it be used or not?

Original source

Related problems