Teach Yourself COBOL in 21 days, Second Edition

Previous chapterNext chapterContents


- F -
Transaction Processing

Transaction processing is an additional area of COBOL that you should know something about.

The Basics of Transaction Processing

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

The steps of a transaction are generally as follows:

1. Inputting the data frequently involves keying in the information at a terminal. Sometimes it involves inputting information from another source, such as a floppy disk containing payroll information to be processed.

2. Temporarily saving the existing data is a significant step if the input data is making changes to already existing information. Existing data must be preserved in such a way that a partial update does not occur. A transaction is an all-or-nothing proposition, and the simple rule is either all of the requested new data is written or rewritten to the file, or none of it is.

3. Transferring the data from source to destination can be a complex step in a distributed environment. You can key in information at a PC or other terminal, and then send it as a packet of information to a remote computer. Some transaction systems are responsible for ensuring that the information travels from source to destination without data corruption.

4. Locking or holding the records to be changed prevents multiuser corruption. For example, if two users are both updating purchase order number 12345, whose information is actually written to the disk? A transaction processing system allows one user in and locks the other user out.

5. Changing the records is the core of a transaction processing system. The whole point of a transaction is to make some change to the information in a file or files. Recording the information to be written to the disk is the task of making the changes permanent and involves ensuring that all changes are recorded or none is recorded. In this step, either the new information is recorded, or the existing information saved in a previous step might be restored because of a failure to record the new data.

6. Releasing locks on records is done so that other users can now have access to and can update the same information.

7. Logging the changes is done by all transaction processing systems. In some systems, the logged information can be accessed by special programs that can be used by a system programmer or system administrator who is trying to trace a problem, or by a security administrator who is trying to track down a security breach. More commonly, the transaction system logs the information internally and uses it in the processing of the previous steps.

The Need for Transactions

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 Transactions

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:

1. Signal the beginning of a transaction. This usually uses the following syntax:
BEGIN TRANSACTION
2. Issue one or more writes or rewrites to a file. This usually involves standard COBOL file I-O syntax, such as
REWRITE CUSTOMER-RECORD
    INVALID KEY
        MOVE "E" TO TRANSACTION-FLAG.

WRITE PO-RECORD
    INVALID KEY
        MOVE "E" TO TRANSACTION-FLAG.
3. Complete the transaction or abort the transaction. These usually use the keyword COMMIT to signal that the transaction is completed successfully, or the keyword ROLLBACK to indicate that the transaction is to be aborted. The following is the syntax:
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 with Transactions

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.


Previous chapterNext chapterContents


Macmillan Computer Publishing USA

© Copyright, Macmillan Computer Publishing. All rights reserved.