Link to home
Start Free TrialLog in
Avatar of mkarthik415
mkarthik415

asked on

TransactionScope rollback.

Hi

How to rollback a TransactionScope. I tried adding new elements List in a transaction using TransactionScope and never called the complete method. Even after that the methods inside the Transaction are executed and are not rolled back. Below is the sample code.

I was expecting output to be
1
2
1

but the actual output was
1
2
2

Could anyone tell me how to get the expected output using the below code.

Thank You

static void Main(string[] args)
        {
            List<string> foo = new List<string>();
            foo.Add("a");
            Console.WriteLine("Before Transaction: "+foo.Count);
            
            try
            {
                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
                {                    
                    foo.Add("b");                    
                    Console.WriteLine("During Transaction: "+foo.Count);
                }
            }
            catch 
            {
            }

            Console.WriteLine("After Transaction: " + foo.Count);            
            Console.Read();
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial