Progress
Programming
Handbook
Transactions and Program Variables
You have read quite a bit about how transactions are backed out and how database changes are undone. But what happens to work done with variables?
Any changes made to variables in a transaction or subtransaction block are undone whenever a transaction or subtransaction is backed out. The variables are restored to the values they had at the beginning of the transaction or subtransaction that is undone. Variables specifically defined as NO–UNDO are not undone in this case. However, changes to variables made outside a transaction are never undone since only transaction and subtransaction blocks can be undone.
Suppose you are creating customer records and using a variable to keep track of how many records have been created:
Run
p-var.p
and enter the data shown in the following display:
![]()
At this point, the following has happened:
- On the first iteration of the REPEAT block, the CREATE statement created a customer record.
- The ctr variable was incremented by 1, so its value became 1.
- You used the UPDATE statement to put the values of 500 and “Fly Away Gliding” in the cust–num and name fields of the new customer record.
- On the second iteration of the REPEAT block, the CREATE statement creates a second customer record.
- The ctr variable is incremented by 1 again, giving it a new value of 2.
Press END–ERROR. Progress undoes the work done in the current iteration of the REPEAT block and leaves that block. The customer record created on the second iteration, which had all its fields set to their initial values, is backed out and the value of ctr is reset from 2 to the value it had at the beginning of the iteration (transaction), which is 1. The DISPLAY statement tells you that just one customer has been created, as shown in the following display:
![]()
Within a transaction, any changes made to variables or to database fields are undone when that transaction is undone. However, any changes made to variables prior to the start of the transaction are not backed out. For example, consider
p-tnchp.p
.
In this procedure, Progress starts a transaction at the beginning of each iteration of the FOR EACH block because the block contains an UPDATE statement to modify records. The UNDO cusloop statement undoes changes made during the current iteration of the FOR EACH block. When this occurs, Progress undoes the changes to the order being updated and to the variable j. Variable j is restored to its value at the beginning of the current iteration of the FOR EACH block (at the beginning of the transaction). However, Progress does not undo changes to the variable i because those changes were not done within the transaction.
Although backing out of variables is useful in many cases, keep the following in mind:
- There is a certain amount of overhead associated with undoing variables in transactions and subtransactions.
- If you are going to be doing extensive calculations involving variables or arrays, then consider using the NO–UNDO option on those variables if you have no need for the undo services on those variables.
Copyright © 2004 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |