Transaction processing is an additional area of COBOL that you should know something about.
Transactions are used to force a group of two or more writes, rewrites, or deletes to one or more files to be treated as a single group in which all of the file updates must occur successfully.
In general terms, a transaction consists of the input data, the existing data in the file that is being changed, the transfer of the data from the input computer to the computer where it will be stored, locking the records to be changed, changing the records and recording the changes, releasing locks on records, and logging the changes.
The steps of a transaction are generally as follows:
At least two very good reasons exist for using transactions in processing data. Multiple users might be trying to update the same information at the same time, and a transaction might involve more than one file.
When multiple users are trying to update the same information, there is no guarantee that they are entering the same data. Two users entering information for the same employee, but entering different addresses, could cause a data disaster, unless there were some mechanism to ensure that only one user at a time can update the information.
When multiple files are involved, there is the risk of a system problem as information is being updated. Take the case of an invoice file that is actually made up of two separate files. One file might contain the name and address of the customer, the invoice number, and the total amount of the invoice. The second file might contain several records, including one line for each of the items being purchased on the invoice and the amount for each item. If the invoice is changed, it is essential that the invoice and invoice lines all be updated simultaneously, or the total amount will no longer correctly reflect the sum of all the invoice lines. A mechanism is needed here to ensure that updates occur for both files or for neither file.
The mechanism that ensures this process is transaction processing.
COBOL transaction processing is implemented in different ways by different versions of COBOL, but the following is a fairly common set of steps used in COBOL transaction processing:
BEGIN TRANSACTION
REWRITE CUSTOMER-RECORD INVALID KEY MOVE "E" TO TRANSACTION-FLAG. WRITE PO-RECORD INVALID KEY MOVE "E" TO TRANSACTION-FLAG.
IF TRANSACTION-FLAG = "E" ROLLBACK TRANSACTION ELSE COMMIT TRANSACTION.
All file writes or rewrites are saved up from the point that a BEGIN TRANSACTION is executed until a COMMIT or a ROLLBACK is executed. At that point, all writes and rewrites are either executed or abandoned, causing an all-or-nothing update to the disk drives.
Designing a system with transaction processing involves careful planning of what constitutes a complete transaction.
Beginners frequently bundle large numbers of file writes and rewrites together and call these a transaction, which causes performance problems and prevents other users from being able to update the same files.
A more disastrous error is that of starting a transaction but never finishing it. This can happen when a COMMIT or ROLLBACK is not executed because of an error in the flow of the program that somehow skips the statements. A GO TO that skips over a crucial piece of code or placing the COMMIT or ROLLBACK in a separate paragraph and then neglecting to perform it are examples of possible errors.
Be sure that each transaction is as small as it can be while ensuring the integrity of the information in the files.
© Copyright, Macmillan Computer Publishing. All rights reserved.