Trbegin

Trbegin is used to start a database transaction.

Syntax

  Trbegin TABLE_LIST

Examples

# This function debits an account ACCOUNT1 and credits and account ACCOUNT2 with an AMOUNT value
# It manages a transaction except if a transaction is already in progress
# It also updates a statistical table
# Returns  [V]CST_AOK if the operation was successful, otherwise returns [V]CST_AERROR.

Funprog TRANSFER(ACCOUNT1, ACCOUNT2, AMOUNT)
Value Char ACCOUNT1(), ACCOUNT2()
Value Decimal AMOUNT
Local Integer IF_TRANS

# Start the transaction if no transaction is in progress
  Local File ACCOUNT [ACC]
  If adxlog
    Trbegin [ACC], STATISTICS
    IF_TRANS=0
  Else
    # The transaction has been started by the calling program
    IF_TRANS=1
  Endif

# Debit operation (CODE is a unique index so only one database line is updated)
  Update ACCOUNT Where CODE=ACCOUNT1 With BALANCE=BALANCE-AMOUNT
  If fstat
    # If IF_TRANS=1, the transaction must been aborted by the calling program
    If IF_TRANS=0 : Rollback : Endif
    End [V]CST_AERROR
  Endif

# Credit operation
  Update ACCOUNT Where CODE=ACCOUNT2 With BALANCE=BALANCE+AMOUNT
  If fstat
    # If IF_TRANS=1, the transaction must been aborted by the calling program
    If IF_TRANS=0 : Rollback : Endif
    End [V]CST_AERROR
  Endif

# Updates account movement statistics
  Update [STA] Where STACODE="ACCOUNTS" With MOVEMENTS=MOVEMENTS+1
  If fstat
    # If IF_TRANS=1, the transaction must been aborted by the calling program
    If IF_TRANS=0 : Rollback : Endif
    End [V]CST_AERROR
  Endif

# The operation is successful. If IF_TRANS=1, the transaction must been committed by the calling program
  If IF_TRANS=0 : Commit : Endif
End [V]CST_AOK

Description and comments

Associated errors

CodeDescription
7Abbreviation not found.
20Table not found.
27Table access error.
28Table opened twice.
29Too many tables opened simultaneously.
32Transaction started at a higher level of the nesting call.
49Transaction already in progress.

See also

File, Commit, Rollback, Onerrgo, Read, Write, Writeb, Rewrite, RewriteByKey, Delete, DeleteBykey, Lock, adxlog.