?
Solved

TransactionScope rollback.

Posted on 2011-03-24
1
Medium Priority
?
870 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 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