Example program for refreshing a user-maintained data table

To help you write your own program, here is an example of a COBOL program that demonstrates how to refresh a UMT while it is still open, to match the source data set.

If updates are applied frequently to the source data set, and could be applied while the refresh program is running, this could mean that the source data set is never exactly reflected by the UMT, because the record being processed or records already processed could be changed. This means that the program has to be tolerant to the possibility of the records changing. The program is also written to allow for the possibility that the UMT itself is updated by other programs, although you are not recommended to operate in this way (that is, the only program that updates the UMT should be the refresh program).

How the example program operates

First, the environment is initialized. A check is made that the UMT file is local and is already open. If the UMT file is remote, the program issues a message and ends. If the UMT file is not open, the program opens it and ends (because opening the UMT will load the latest data from the source data set without the need to perform any more processing). A check is also made that the source file is local; if it is remote, the program issues a message and ends. The file that directly accesses the UMT's source data set is opened. Start browse operations are then performed on both files to allow the program to step through them both sequentially.

If the environment is set up without error, the update of the UMT starts. This involves the retrieval and comparison of pairs of records, one from the UMT and one from the base data set.

The records retrieved are compared:
  • If the records are equal, the flags are set to read the next record from the UMT and the data set.
  • If the UMT has a greater key than the data set, there is a record in the data set that must be added to the UMT.
  • If the data set has a greater key than the UMT, there is an extra record in the UMT that must be removed.
  • If the keys are equal, but the records are different, the UMT should be updated with the record in the data set.
If a record must be added to the UMT, a write operation is performed.
  • If the write operation succeeds, the program goes on to process the next pair of records.
  • If the write operation fails because of a record that has been inserted by another transaction between the read and the write operation performed by the program, an attempt is made to delete the record and write it again.
  • If the second attempt fails, the program processes the next pair of records.
  • When the next pair of records is processed, the current UMT record is compared with the next record in the data set to check for further UMT record omissions.
If a record must be deleted from the UMT, a delete operation is performed.
  • If the delete operation succeeds, the program goes on to process the next pair of records.
  • If the delete operation fails because the record has already been deleted between the read and delete operations, the program continues to process the next pair of records.
  • When the next pair of records is processed, the current data set record is compared with the next record in the UMT to check for further records that should not be in the UMT.
If a record must be updated in the UMT, a read for update operation is performed, to get a lock on the record.
  • If this is a success, the updated record is rewritten to the UMT, and the program goes on to process the next pair of records.
  • If the operation fails because another transaction has deleted the record, a write operation is performed to put it back in.
  • If the write operation fails, the program continues to process the next pair of records.
  • When the next pair of records is processed, new records are read from both the UMT and the data set.

When the end of both files has been reached, and there are no more records left to process, the program performs end browses on both the data set and the UMT and returns. Note that the example does not close the file that directly accesses the data set. If the data set cannot operate for update in a shared environment, the file that accesses it should be set to CLOSED DISABLED to allow it to be updated.

The program traps any unexpected errors and issues an error message on the screen. Only the first operation on the UMT is checked (either the delete, write or read/rewrite operations). If that fails with a return code that could be caused by a record being changed after it was originally read, one final attempt is made to correct the record, but this attempt is not checked. This is to prevent the program entering a loop state.

There are further comments in the code.