Sw4   >   Sql   >   Manual Transactions

Manual Transactions

If you are working with an RDBMS that supports COMMIT and ROLLBACK you may want to make use manual transactions when you are saving batches of records, or a sequence of inserts where a parent records is inserted immediately followed by one or more child records.

In these situations if you set the session object $transactionmode to kSessionTranManual at the start of the batch; you can issue a $rollback at the end if an error occurs and the database will undo the database writes, or if all goes well you can issue a $commit and all the database writes are fully saved by the database.

The control point for the decision to $commit or $rollback transactions is outside of the table class. If you are using manual transactions be sure to set the pbIssueSaveListWork parameter of $doworkBatch to kFalse, and then remember to send a $savelistwork message to your smartlist when you commit the transactions.

Always be sure to set the $transactionmode back to kSessionTranAutomatic at the of the method.

The following sample code shows how you might use manual transactions in your code:

; Store the current $transactionmode, set to manual transactions.
Calculate StartMode as db1sess.$transactionmode
Do db1sess.$transactionmode.$assign(kSessionTranManual)

; Insert parent list records.
Do ParentList.$doworkBatch(kFalse) Returns FlagOK ;; (bIssueSaveListWork)
If FlagOK
   
   ; Insert child records.
   Do ChildList.$doworkBatch(kFalse) Returns FlagOK ;; (bIssueSaveListWork)
   
End If
If not(FlagOK)
   
   ; Something went wrong. Rollback all database changes.
   Do db1sess.$rollback()
   
Else
   
   ; All went well, commit the database changes and make the history list the same as the normal list.
   Do db1sess.$commit()
   Do ParentList.$savelistwork()
   Do ChildList.$savelistwork()
   
End If

; Always set the transaction mode back to its original setting.
Do db1sess.$transactionmode.$assign(StartMode)

Quit method FlagOK