Program Example Using Transaction Boundaries

This program illustrates the use of Transaction Handling in DataBasic and also illustrates the use of item locks.

    *VERSION 0001
    *****************************************************
    * This program demonstrates the use of the
    * Transaction Handling statements and also some of
    * the @(-n) functions.
    *****************************************************
    * Copyright 2000: NEC Software Solutions (UK) Limited
    *****************************************************
    * Open all relevant files and do initialization
    *
    OPEN 'DATA1' TO DATA1 ELSE STOP 201, 'DATA1'
    OPEN 'DATA2' TO DATA2 ELSE STOP 201, 'DATA2'
    OPEN 'DATA3' TO DATA3 ELSE STOP 201, 'DATA3'
    KEY = ""
    RECORD = ""
    *****************************************************
10  * This is the start of the main transaction loop.
    * It requests an item-id from the user and then locks
    * that item in each of three files.  The program then
    * prompts the user to enter data for each of the
    * three files and updates the files as the data is
    * entered (instead of doing all the updates at the
    * end of the transaction). The user is allowed to
    * abort the transaction at any input field by
    * entering '/' which calls a transaction abort,
    * automatically rolling back all of the updates 
    * which have taken place and also releases all of
    * the locks previously set.
    *****************************************************
    *
    CRT @(-1):;                        * Clear the screen
    CRT @(10,5):"Enter record key ":; INPUT KEY
    CRT @(-13);                        * Clear Line 25
    IF KEY = '' OR KEY = '/' THEN CRT @(-1):; STOP
    TRANSTART THEN
      * 
      * The transaction is now in progress and the log
      * will contain a start entry for this port plus
      * the name of the program which started it.
      *
      READU RECORD FROM DATA1,KEY THEN
        CRT @(-13):"Item '":KEY:"' exists in file
        ...'DATA1'":@(-14):
        SLEEP 1
        GOTO 100 ;             * Abort the transaction
      END
      *
      READU RECORD FROM DATA2,KEY THEN
        CRT @(-13):"Item '":KEY:"' exists in file 
        ... 'DATA2'":@(-14):
        SLEEP 1
        GOTO 100 ;             * Abort the transaction
      END
      READU RECORD FROM DATA1,KEY THEN
        CRT @(-13):"Item '":KEY:"' exists in file 
        ... 'DATA1'":@(-14):
        SLEEP 1
        GOTO 100 ;             * Abort the transaction
      END
      *
      * All records are now locked and don't exist.
      *
      * Now get the data for each record and update them.
      ***************************************************
      CRT (10,8):"Enter data for Record 1":
      INPUT RECORD
      IF RECORD='/' THEN GOTO 100; * Transaction abort req.
      WRITE RECORD ON DATA1,KEY
      * File updated, but lock maintained until transaction end
      ***************************************************
      CRT (10,10):"Enter data for Record 2":
      INPUT RECORD
      IF RECORD='/' THEN GOTO 100; * Transaction abort req.
      WRITE RECORD ON DATA2,KEY
      * File updated, but lock maintained until transaction end
      ***************************************************
      CRT (10,12):"Enter data for Record 3":
      INPUT RECORD
      IF RECORD='/' THEN GOTO 100; * Transaction abort req.
      WRITE RECORD ON DATA3,KEY 
      * File updated, but lock maintained until transaction end
      ***************************************************
      *                                                 *
      * All inputs and updates are complete so 'commit' *
      * the transaction.                                *
      *                                                 *
      ***************************************************
      TRANSEND "Transaction completed - ITEM-ID ='":KEY:"'" THEN
        CRT @(-13):"Transaction accepted and logged -Item-Id = 
           ":... 
        "'":KEY:"'":@(-14)
        GOTO 10 ; * Start another transaction
      END ELSE
        *
        * The ELSE clause will be taken if...
        * 1. No TRANSTART statement has previously been executed.
        * 2. Transaction Logging has not been enabled.
        *
        GOTO 100; * Unable to commit the transaction so
        * attempt an abort.
      END
    END ELSE
      *
      * Unable to start a transaction so quit the 
      * program.  This ELSE clause will be taken if...
      * 1. A transaction is already in progress.
      * 2. Transaction Logging has not been enabled.
      *
      CRT @(-13):"Unable to start a new transaction - ":...
      "program cancelled":@(-14)
      STOP
    END
    *
100 * Transaction abort routine
    *
    * Abort the current transaction incorporating some 
    * text and the item-id into the transaction log.
    *
    TRANSABORT "Abort transaction - ID = '":KEY:"'" THEN
      *
      * Display a message on Line 25
      *
      CRT @(-13):"Your transaction has been aborted":@(-14)
      GOTO 10 ; * Prompt for a new transaction start.
      *
END ELSE
            * The ELSE clause will be taken if...
      * 1. No transaction command has been previously 
      * executed.
      * 2. Transaction Logging has not been enabled.
      *
      CRT @(-13):"Unable to abort the transaction.":(-14)
      ABORT; * Exit completely from all processing
    END
END