Solved

TransactionScope rollback.

Posted on 2011-03-24
1
856 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:mkarthik415
1 Comment
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35211415
TransactionScope is related to database transactions and it has no impact on collections (List<T> in your example).

More about TransactionScope here:
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now