Progress
Programming
Handbook


How Much to Undo

You have seen how, when the system crashes or when you press STOP, Progress undoes the current transaction. Suppose you want to undo a transaction under program control or want to undo a smaller amount of work than that done since the beginning of the transaction.

Take a look at a different version of the p-txn3.p procedure. The outer REPEAT block is the transaction block:

p-txn3a.p
REPEAT: 
    INSERT order WITH 2 COLUMNS. 
    FIND customer OF order. 
    REPEAT: 
      CREATE order-line. 
      order-line.order-num = order.order-num. 
      DISPLAY order-line.order-num. 
      UPDATE line-num order-line.item-num qty price. 
    END. 
END. 

Suppose that you wanted to restrict the maximum amount of an order to $500. In the event that the value of an order exceeded that amount, you want to undo the current order–line entry and display a message to the user. For example, look at p-txn5.p:

p-txn5.p
DEFINE VARIABLE extension LIKE order-line.price. 
DEFINE VARIABLE tot-order LIKE order-line.price. 
REPEAT: 
    INSERT order WITH 2 COLUMNS. 
    tot-order = 0. 
    o-l-block: 
    REPEAT: 
      CREATE order-line. 
      order-line.order-num = order.order-num. 
      DISPLAY order-line.order-num. 
      UPDATE line-num order-line.item-num qty price. 
      extension = qty * price. 
      tot-order = tot-order + extension. 
      IF tot-order > 500 THEN DO: 
        MESSAGE "Order has exceeded $500". 
        MESSAGE "No more order-lines allowed". 
        UNDO o-l-block. 
      END. 
   END. 
END. 

The outer REPEAT block is still the transaction block; it is the outermost block that contains direct updates to the database. However, in this example, Progress starts a subtransaction when it reaches the inner REPEAT block.

If an error occurs during a subtransaction, all the work done since the beginning of the subtransaction is undone. Subtransactions can be nested within other subtransactions.

A subtransaction is started when a transaction is already active and Progress encounters a subtransaction block. The following are subtransaction blocks:

Now go ahead and run the p-txn5.p procedure. Enter the data shown in the following display:

After you enter the second order–line, the amount of the order is $225. When you try to enter the third order–line, the following happens:

Table 12–1 shows when transactions and subtransactions are started.

Table 12–1: Starting Transactions and Subtransactions 
Type of Block
Transaction Not Active
Transaction
Active
DO transaction
FOR EACH transaction
REPEAT transaction
Any DO ON ENDKEY, DO ON ERROR, FOR EACH, REPEAT, or procedure block that directly contains statements that modify database fields or records or that read records using an EXCLUSIVE–LOCK.
Starts a transaction
Starts a subtransaction
Any FOR EACH, REPEAT, or procedure block that does not directly contain statements that either modify the database or read records using an EXCLUSIVE–LOCK.
Does not start a subtransaction or a transaction
Starts a subtransaction

Note that data handling statements that cause Progress to automatically start a transaction for a database table do not cause Progress to automatically start a transaction for a work table or temporary table.


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095