Transaction / Batch Processing Business Objects  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Transaction / Batch Processing Business Objects

Postby SBarnes » Thu Nov 08, 2018 6:29 pm

Hi Mike,

Is there a way to create the scenario with a group of business objects such as a list of orders that if one fails to save they would all get rolled back?
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1620
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: Transaction / Batch Processing Business Objects  Topic is solved

Postby Scott.Pearce » Fri Nov 09, 2018 11:10 am

If you create a sql transaction at the start, then all the objects will use that transaction and as such will all be rolled back if you roll back the transaction. For example (please excuse the VB code):

Code: Select all
Dim weStartedTransaction As Boolean

With Manager.Database
    Try
        If .SQLTransaction Is Nothing Then
            ' flag that we created the transaction, so we know if we should be the ones to .commit or .rollback later.
            .BeginNewTransaction()
            weStartedTransaction = True
        End If
       
        'Your goes code here, perhaps a for loop over a list of objects, calling myObject.Save for each one.

        If weStartedTransaction Then
            .SQLTransaction.Commit()
            .SQLTransaction.Dispose()
            .SQLTransaction = Nothing
        End If
    Catch ex As Exception
        If weStartedTransaction AndAlso .SQLTransaction IsNot Nothing Then
            If .SQLTransaction.Connection IsNot Nothing Then
                .SQLTransaction.Rollback()
            End If
            .SQLTransaction.Dispose()
            .SQLTransaction = Nothing
        End If

        'Maybe a messagebox or a Throw goes here, plus any other code you need to deal with the error condition (ie. set status properties or whatever).
    Finally
        If weStartedTransaction Then
            .SQLTransaction = Nothing
        End If
    End Try
End With


Warning - some complicated objects do reads during the save process, which could result in sql blocking if multiple object saves are part of the same sql transaction, so your mileage may vary.
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 743
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 221


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 7 guests