Avatar of Arikael
Arikael
Flag for Switzerland asked on

My transaction does not abort

Hi experts

I'm trying to use the following code to create a transaction.
Now, to test it the line
TManager.VehiclesTableAdapter.Adapter.Update(ChangedSet.Vehicles);

will always raise a NullReferenceException.
According to http://msdn2.microsoft.com/en-us/library/system.transactions.transactionscope.aspx the transaction should be rolled back as soon as an exception occurs within the transactionscope.
This doesn't happen and the transaction always suceeds.
It only works when I put the Ts.Complete within the try-statement (which kinda makes sense).

Am I misinterpreting something from the msdn-site?
try
                {
                    using(TransactionScope Ts = new TransactionScope())
                    {
                        KrFakturaDataSet ChangedSet = (KrFakturaDataSet)TmpSet;
                        AddressesTableAdapter Adapter = new AddressesTableAdapter();
                        TableAdapterManager TManager = new TableAdapterManager();
                        try
                        {
                            Adapter.Update(ChangedSet.Addresses);
                            TManager.VehiclesTableAdapter.Adapter.Update(ChangedSet.Vehicles);
                            
                        }
                        catch(NullReferenceException e)
                        {
                            MessageBox.Show(e.Message);
                            ChangedSet.RejectChanges();
                        }
 
                        Ts.Complete();
                    }
                }
                catch(TransactionAbortedException e)
                    {
                        MessageBox.Show(e.Message);
                    }
                catch (ApplicationException e)
                {
                    MessageBox.Show(e.Message);
                }

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment
PockyMaster

8/22/2022 - Mon
PockyMaster

the Ts.Complete(); should be in your try part, not after the catch!
try
                        {
                            Adapter.Update(ChangedSet.Addresses);
                            TManager.VehiclesTableAdapter.Adapter.Update(ChangedSet.Vehicles);
                              Ts.Complete();
 
                        }
                        catch(NullReferenceException e)
                        {
                            MessageBox.Show(e.Message);
                            ChangedSet.RejectChanges();
                        }
 
                      

Open in new window

Arikael

ASKER
Thank you

After reading the msdn-article which says

"If no exception occurs within the transaction scope (that is, between the initialization of the TransactionScope object and the calling of its Dispose method), then the transaction in which the scope participates is allowed to proceed. If an exception does occur within the transaction scope, the transaction in which it participates will be rolled back."

I thought it automatically rolls back if an exception occurs.
So, if I'am wrong, what does the article actually mean?
ASKER CERTIFIED SOLUTION
PockyMaster

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Arikael

ASKER
You are right.

When I throw the exception in my catch-block it doesn't complete the transaction even when I put the Ts.Complete after the catch block.

I find microsoft's example a bit misleading, especially for beginners.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
PockyMaster

that's why experts exchange is there ;-)