I have two transaction scopes, one within another. I would love to know if the inner transaction scope will be rolled back after it has been committed and the outer one does not complete.
-
A good explanation of various `TransactionScopeOption` choices; it finally makes sense: https://learn.microsoft.com/en-us/previous-versions/ms172152(v=vs.90)?redirectedfrom=MSDN – Alex Sep 10 '19 at 10:48
3 Answers
It depends on the scope option you start the nested transaction scope with.
If you use the default option TransactionScopeOption.Required
then the nested scope will enlist in the same transaction as the outer scope and as such when the outer scope rolls back the inner scope will also be rolled back even if it has called Complete
.
If, however, you use TransactionScopeOption.RequiresNew
then the nested scope will begin its own transaction and complete it separately from the outer scope, so it will not roll back even if the outer scope rolls back.
If you use TransactionScopeOption.Suppress
then the nested scope will not take part in the outer transaction and will complete non-transactionally, thus does not form part of the work that would be rolled back if the outer transaction rolls back.

- 133,383
- 43
- 204
- 250
-
2
-
What if you nest two Suppress scopes under a 3rd outer scope? Should I call complete on both the inner Suppress scopes, and will that interfere with the outer parent scope at all? – Triynko Nov 26 '19 at 20:04
Since they are nested, the inner transaction will roll back.
This is not the whole story, and depends on how you create the nested transaction, but by default, it will roll back.
This article goes into depth about TransactionScope
and should answer most of your questions.
Being distributed or not is irrelevant.

- 489,969
- 99
- 883
- 1,009
-
@Frantisek made mention of distributed transactions. How do i know im using a distributed transaction? – Orson Dec 21 '10 at 09:49
-
1I suppose I was wrong. With TransactionScope you probably don't need dtc to have it work. Read this http://msdn.microsoft.com/en-us/library/ms172152(v=vs.90).aspx : "Although a nested scope can join the ambient transaction of the root scope, calling Complete in the nested scope has no affect on the root scope. Only if all the scopes from the root scope down to the last nested scope vote to commit the transaction, will the transaction be committed." – František Žiačik Dec 21 '10 at 09:53
-
4I am sure you know about this, but the terms "inner transaction" and "nested transaction" are IMHO inappropriate when talking about TransactionScope, because there are actually no nested transactions, but just nested TransactionScopes. If you choose TransactionScopeOption.Required it is just 1 big transaction and with TransactionScopeOption.RequiresNew there are 2 completely independent transactions, but they are not nested in the sense of (closed) nested transactions. – Niklas Peter Jan 31 '17 at 10:47
Yes it will, you can refer to code below. Following code wil roll back outer transaction scope if inner transaction throw error and vice versa.
public bool rootMethod(){
using (var transaction = new(TransactionScopeOption.RequiresNew))
try{
// your code here
SomeController someController = new SomeController();
var responseFromChildMethod = someController.childMethodWithTxn();
// your logic here
transaction.Complete();
return true;
}
catch(Exception ex){
transaction.Dispose();
return false;
}
}
}
SomeController.cs
public bool childMethodWithTxn(){
using(var newTransaction = new TransactionScope()){
try{
//your code here
newTransaction.Complete();
return true;
}
catch(Exception ex){
newTransaction.Dispose();
return false;
}
}
}

- 76
- 5