locked
TransactionScope RRS feed

  • Question

  • hi experts, I got a wired problem need your help. I have nested transactions. The outer method calls an inner method SaveChangesToSource that opens another transaction scope. Actually I know my underlying sql may have problem. What I cannot understand is why my inner transaction complete successfully and it looks like the exception is been postponed until I call outer cope.Complete?? it's like the inner transaction does not report error until you call outer scope.complete.. then the the outer scope rollback the whole thing...??

    using (TransactionScope scope = new TransactionScope())
    {
          SaveChangesToSource(changes);
          scope.Complete();
    }
    
    
    protected override void SaveChangesToSource(IEnumerable<IChange> changes)
    {
        using (TransactionScope scope = new TransactionScope())
        {            
             _bl.SaveChanges(changes);
             scope.Complete();
        }
    }
    Tuesday, April 7, 2015 10:27 PM

Answers

  • Hello Samu Zhang,

    >>it's like the inner transaction does not report error until you call outer scope.complete.. then the the outer scope rollback the whole thing...??

    Yes, this is by default because if when creating the TransactionScope class instance without specified requirements. It will use the TransactionScopeOption.Required as the default value:

    A transaction is required by the scope. It uses an ambient transaction if one already exists. Otherwise, it creates a new transaction before entering the scope. This is the default value.

    So it will perform what you see in the application, the inner transaction does not report error until calling outer scope.complete.. then the the outer scope rollback the whole thing.

    If what you want is the nested transaction should be separate which means even the outer transaction fails, the nested one would be committed successfully, you could use the TransactionScopeOption.RequiresNew option when creating the TransactionScope class instance as below:

    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
    
    {
    
    …
    
    }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Wednesday, April 15, 2015 4:34 AM
    Wednesday, April 8, 2015 2:01 AM