Results 1 to 5 of 5

Thread: TransactionScope Does this work??

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    TransactionScope Does this work??

    Code:
        Public Sub PerformDBWork()
            Try
                Using scope As New TransactionScope
                    Using cnBusinessDB
                        'Open connection to Business DB
                        cnBusinessDB.Open()
                        'Do Business operations
                        DelAnyExistingAddress(UserID)
                        InsertAddress()
    
                        Using cnFinancialDB
                            'Open connection to Financial DB
                            cnFinancialDB.Open()
                            'Do Financial operations
                            InsertPOHeaderRecord()
                            InsertPODetailsRecords()
                        End Using
    
                    End Using
                    scope.Complete()
                End Using
            Catch txnEx As TransactionAbortedException
                RaiseEvent Transaction_Failure(MSG_TXN_ABORTED.Replace("%1", txnEx.Message.ToString()))
            Catch ex As Exception
                RaiseEvent Unknown_Exception(MSG_UNEXPECTED_ISSUE.Replace("%1", ex.Message.ToString()))
            End Try
    
        End Sub
    Can I perform the actual database operations in routines by calling them inside the TransactionScope block if my connection objects are declared at module level?

    Cheers.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: TransactionScope Does this work??

    Where your connection variables are declared is irrelevant, as is where your connection objects are created. All that matters is where the connection is opened. If a connection is opened outside the TransactionScope then commands executed over that connection will not be enlisted. Conversely, if a connection is opened within a TransactionScope then all commands executed over that connection within the TransactionScope will automatically be enlisted.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: TransactionScope Does this work??

    Thanks for that JMc. Mind if I ask another question? Would I handle oleDBExceptions in my InsertPOHeader routine or let my Catch txnEx in PerformDBWork handle the issue?

    Cheers for your help pal.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: TransactionScope Does this work??

    Given that an OleDbException is specifically related to data access, it should probably not leave the data access layer. Currently it would be caught by your second Catch block.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: TransactionScope Does this work??

    Sorry my question wasn't very clear. Currently my InsertPOHeaderRecord routine has exception handling for oledbexceptions. I simply raise an event out of my class for the client consuming the class to inform users of issue.

    My question is would you raise an event for the oledbexception in InsertPOHeaderRecord and also raise an event in the PerformDBWork routine for the transactionscope failure?

    Cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width