I understood the implicit rollback (that usually happens when exception happens and Dispose is called) is not guaranteed for all providers. However many examples uses:
using (DbTransactio txn = cnctn.BeginTransaction())
is there a reason for that?
The most plain answer there would be “because it implements
IDisposable“. Any type that implementsIDisposable: it is your job to dispose it appropriately. In this case, with procedural code, the simplest way to do that would be via ausingstatement.In this specific case, the answer would be: because in the event of error, you want the transaction rolled back rather than left to the GC. Personally I would probably tend to use
catchtoRollback, but one should hope that theDispose()already does that. I wouldn’t rely on it myself, though, unless it was documented. For example:Note that in the related case of
TransactionScope, the “Dispose()without marking it completed” is the expected way of signalling a rollback: