[Previous] [TOC] [Next]

Transactions in Action


So if action objects are responsible for transactions, how do they maintain transactions? When an action object is initiated, it instantiates a cDAL object and begins a transaction. This cDAL object is passed to all business objects that the action object creates so that every business object in this action object has the same transaction.

Just before the action object is destroyed, it checks a module-level variable (bPiTransactionOK) in the class Terminate event to see whether the transaction should be committed. This module-level variable can be set by any of the procedures within the action object. Normally, if a transaction has to be rolled back, an exception is raised to the client and bPiTransactionOK is set to False so that the user can be informed that something very bad has happened. Checking this module-level variable in the Terminate event ensures that the action object is responsible for protecting the transaction, not the client.

Private bPiTransactionOK    As Boolean
  Private oPicDAL             As cDAL

  Private Sub Class_Initialize()
      Set oPicDAL = New cDAL
      oPicDAL.BeginTransaction
      bPiTransactionOK = True
  End Sub

  Private Sub Class_Terminate()
      If bPiTransactionOK Then
          oPicDAL.CommitTransaction
      Else
          oPicDAL.RollbackTransaction
      End If
  End Sub


[Previous] [TOC] [Next]