Page 1 of 1

Transaction / Batch Processing Business Objects

PostPosted: Thu Nov 08, 2018 6:29 pm
by SBarnes
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?

Re: Transaction / Batch Processing Business Objects  Topic is solved

PostPosted: Fri Nov 09, 2018 11:10 am
by Scott.Pearce
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.