Best Practices - Data Transaction Handling

The purpose of this document is to provide best practices for data transaction handling.

Main principles

Managing database transactions requires that you know the main principles associated with this function in the SAFE X3 engine. The main principles are as follows:

Main consequences on the Sage X3 code

Example

# Example of a transaction on a table (ACCOUNT)
Subprog CREDIT_DEBIT(ACCOUNT1,ACCOUNT2,AMOUNT,RET_STATUS,RET_MESSAGE)
Value Char ACCOUNT1, ACCOUNT2
Value Integer AMOUNT
Variable Integer RET_STATUS
Variable Char RET_MESSAGE()

Local Integer TRANS_OPEN
Local Char ACCOUNT_CODE
Local File ACCOUNT [ACCOUNT]

# Open a transaction if not opened
  If adxlog=0
    TRANS_OPEN=0
    Trbegin [ACCOUNT]
  Endif

# Perform a credit / debit movement
# CODE is a unique code so adxuprec can only return 0 or 1
  Update [ACCOUNT] Where CODE=ACCOUNT1 With [ACCOUNT]BALANCE-AMOUNT
  If adxuprec=1
    Update [ACCOUNT] Where CODE=ACCOUNT2 With [ACCOUNT]BALANCE+AMOUNT
  Else
    ACCOUNT_CODE=ACCOUNT1
  Endif

  If adxuprec=0
    ACCOUNT_CODE+=string(ACCOUNT_CODE="",ACCOUNT2) : # Contains the first account not found
    RET_STATUS=[V]CST_AERROR
    RET_MESSAGE="Account"-ACCOUNT_CODE-"not found"
    If TRANS_OPEN=0
      Rollback
    Endif
  Else
    Raz RET_MESSAGE
    RET_STATUS=[V]CST_AOK
    If TRANS_OPEN=0
      Commit
    Endif
  Endif
End