• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Wrapping many database changes in a transaction

I have a web service which uses a dataset input parameter to update the master database.  Most of update/insert code is of this variety

For each row in dataset.tables("Table1").rows
   'update or insert
For each row in dataset.tables("Table2").rows
   'update or insert
For each row in dataset.tables("Table10").rows
    'update or insert

it updates 10+ tables.  

My problem is that when we get errors with connectivity or bad data, we get it partly updated.  

What is the best way to wrap this in a transaction?  I have done them before but not to this degree.  Does wrapping all the statements in one transaction work?

Also, one of the tables has this:

For each row in dataset.tables("Table6").rows
    'update or insert

how does that affect using a transaction?

Thanks in advance
  • 2
  • 2
1 Solution
Bob LearnedCommented:
If you apply the transaction to the Connection object, then committing or rolling back should apply to all changes made during that session.
gdinunzioAuthor Commented:

Apparantly using the transactionscope is the preferred way to do it now as opposed to the old transaction object.

This is my code
            Using ts As New TransactionScope
                Using cnn As New OracleConnection(connectionstring)
                    Using cmdVldtUsr As New OracleCommand("SELECT ...", cnn)
                        With cmdVldtUsr
                            .BindByName = True                           
                            rdr = .ExecuteReader
                        End With
                        'Validate User
                        If rdr.Read() Then                           
                            Using cmd As New OracleCommand()
				'Update all tables here   
                            End Using                              
                        End If
                    End Using
                End Using
                'Commit Changes
            End Using
        Catch ex As TransactionAbortedException
            ' throw("TransactionAbortedException Message: {0}", ex.Message)
        Catch ex As ApplicationException
            ' MessageBox.Show("ApplicationException Message: {0}", ex.Message)

        End Try

Open in new window

Bob LearnedCommented:
You can use the TransactionScope, but IMHO that is a much heavier operation, since it can handle crossing system boundaries, and include many different sources.  I would love to see a reason why TransactionScope would be preferred in this case, over the standard transaction.  You don't appear to be doing anything complex.  Ultimately, that is your decision.

You need to remember to rollback changes when an exception occurs, unless an implicit rollback occurs, since changes weren't committed.
gdinunzioAuthor Commented:
I took the MSDN info as gospel, but you are correct since i am only connecting to one data source, I'll test both and report back what i find.

Thanks again.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now