Under what circumstances can code wrapped in a System.Transactions.TransactionScope
still commit, even though an exception was thrown and the outermost scope ne
Be aware how TransactionScope
works:
It sets property System.Transactions.Transaction.Current
at the begining of using scope and then set it back to previous value at end of using scope.
Previous value depends on where given scope is declared. It can be inside another scope.
You can modify code like this:
using (var sqlConnection = new ConnectionScope())
using (var transaction = new TransactionScope())
{
sqlConnection.EnlistTransaction(System.Transactions.Transaction.Current);
// Do something that modifies data
transaction.Complete();
}
I show this possibility for those who have their code more complicated and cannot simply change code to open DB connection first.