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.