Record Locking in COBOL COBOL

In runtime environments in which more than one user can be running concurrently, the possibility arises that multiple users may wish to access the same file at the same time. In these circumstances, the following sequence of events may occur: user A reads a record from a file, modifies a field within the record, and then rewrites the record. After user A reads the record but before it is rewritten, user B reads the same record from the same file, modifies it and rewrites it. The final contents of the record depend on the sequence in which these operations occur, and this is not predictable since the two users are not coordinated.

To prevent this sort of destructive interference between concurrent users of shared files, RM/COBOL provides record locking facilities.If the LOCK MODE clause is not specified for a file, the default record locking mode for a file opened in a shared input-output mode is automatic single. If the LOCK MODE clause is omitted, the default record locking mode is as described in the RM/COBOL User’s Guide. There are four record locking modes: automatic multiple, automatic single, manual multiple, and manual single (for more information, see the section on “Record Locking Modes”. Record locking occurs only when the file is open in the shared input-output mode.

Records need not be locked in order to rewrite or delete them. The runtime system will obtain the lock in those cases where it is not already held by the run unit. The record so locked may contain different data than expected because of the action of other run units sharing the file. In addition, the REWRITE or DELETE statement will be unsuccessful if another run unit has deleted that record or if the record is locked by another run unit and the program executing the DELETE or REWRITE statement is such that it does not wait for the record lock to be released. If the record is successfully locked, the record lock is released upon completion of the REWRITE or DELETE statement. It is the application programmer’s responsibility to provide appropriate record locking when necessary for data integrity in a shared file environment by use of the READ statement immediately prior to REWRITE or DELETE statements.

When a run unit attempts to hold multiple record locks, either through one of the multiple record locking modes in one file or single record locking modes in more than one file, it is the application programmer’s responsibility to avoid deadlock situations. A deadlock situation occurs when run unit 1 holds a lock on record A and repeatedly attempts to lock record B while run unit 2 holds a lock on record B and repeatedly attempts to lock record A. Each application that shares the same files should lock records in the same order and, upon unsuccessfully locking one record in the series, unlock all currently locked records before attempting to lock the records again.

Programs that use record locking may specify both a file status data item and an applicable USE procedure for each file that is possibly shared by other concurrent run units. When this condition is met, the runtime system invokes the USE procedure with the file status data item set to a value of 99 when a record cannot be locked because it is currently locked by another run unit. This can occur for a DELETE or REWRITE statement if these statements are executed without having previously locked the record to be deleted or replaced. When the 99 status occurs, the program can unlock (by use of the UNLOCK statement) any records already successfully locked and then attempt to obtain the required locks again.

Programs that do not specify both a file status data item and an applicable USE procedure for a shared file will cause the runtime system to wait for a record to be unlocked by another run unit before locking the record for this run unit. Such programs should never attempt to hold multiple record locks, either in one logical file or in two or more logical files, since the program cannot recover from potential deadlock situations with other concurrently executing run units that share those files.

Regardless of whether both a file status data item and applicable USE procedure are defined, if the run unit attempts to lock the same record through two different COBOL file-names that refer to the same physical file, the input-output statement will be unsuccessful with an I-O status value of 99.

Note:The maximum number of record locks that may be held simultaneously is a system-dependent parameter. An application should not be designed to hold a large number of simultaneous record locks. Special care should be taken when automatic multiple record locking applies to a file because each READ statement without the NO LOCK phrase will obtain another record lock. When a DELETE, READ, or REWRITE statement is executed that attempts to obtain a record lock that exceeds the maximum number of record locks, the statement will complete unsuccessfully.

Record Locking Modes

There are two ways to obtain record locks, either automatically upon execution of a READ statement or manually upon execution of a READ statement that specifies the LOCK phrase.

There are two ways to release record locks, either implicitly such that only a single record is locked by the run unit for the file or explicitly such that multiple records may be locked by the run unit for the file.

These record locking and unlocking methods may be independently combined to give four distinct record locking modes: automatic single, automatic multiple, manual single and manual multiple.

Automatic Record Locking Modes

When the LOCK MODE IS AUTOMATIC clause is specified in the file control entry or is the default applied to the file, then a record is automatically locked when a READ statement without the NO LOCK phrase is executed successfully in the shared input-mode. The NO LOCK phrase may be specified in the READ statement to output suppress this automatic record locking. When automatic record locking applies, the NO LOCK phrase should be specified in READ statements for which it is known that the accessed record will not be updated by a REWRITE statement or deleted by a DELETE statement.

The automatic record locking modes are automatic single and automatic multiple.

Automatic single record locking applies when the LOCK MODE IS AUTOMATIC clause does not specify the MULTIPLE option in the LOCK ON RECORD phrase or when the LOCK MODE clause is omitted and automatic single record locking is the applicable default for the file. In automatic single record locking mode, at most one record in the logical file is locked by the run unit at any one time because any input-output statement that refers to the file causes any existing record lock to be released.

Automatic multiple record locking applies when the LOCK MODE IS AUTOMATIC clause specifies the LOCK ON MULTIPLE RECORDS phrase or when the LOCK MODE clause is omitted and automatic multiple record locking is the applicable default for the file. Automatic multiple record locking allows the run unit to hold a number of record locks in one file simultaneously. In automatic multiple record locking mode, existing record locks are not released until a CLOSE or UNLOCK statement that refers to the file-name is executed, except that the successful execution of the DELETE statement causes the record lock to be released for the deleted record.

Manual Record Locking Modes

When the LOCK MODE IS MANUAL clause is specified in the file control entry or is the default applied to the file, then a record is manually locked when a READ statement with the LOCK phrase is executed successfully in the shared input-output mode. A READ statement that does not specify the LOCK phrase does not attempt to lock the record accessed. The LOCK phrase may be omitted in a READ statement for which it is known that the accessed record will not be updated by a REWRITE statement or deleted by a DELETE statement.

The manual record locking modes are manual single and manual multiple.

Manual single record locking applies when the LOCK MODE IS MANUAL clause does not specify the MULTIPLE option in the LOCK ON RECORD phrase or when the LOCK MODE clause is omitted and manual single record locking is the applicable default for the file. In manual single record locking mode, at most one record in the logical file is locked by the run unit at any one time because any input-output statement that refers to the file causes any existing record lock to be released.

Manual multiple record locking applies when the LOCK MODE IS MANUAL clause specifies the LOCK ON MULTIPLE RECORDS phrase or when the LOCK MODE clause is omitted and manual multiple record locking is the applicable default for the file. Manual multiple record locking allows the run unit to hold a number of record locks in one file simultaneously. In manual multiple record locking mode, existing record locks are not released until a CLOSE or UNLOCK statement that refers to the file-name is executed, except that the successful execution of the DELETE statement causes the record lock to be released for the deleted record.

Single Record Locking Modes

Single record locking modes are specified by omission of the MULTIPLE option in the LOCK MODE clause of the file control entry or by configuration of single record locking as the default for files not described with the LOCK MODE clause. In single record locking modes, locked records are implicitly released upon execution of any input-output statement that refers to the file.Thus, at most a single record at a time is locked by the run unit for the file.This single record lock moves from record to record
as READ statements that obtain a record lock are executed, or is released if any other input-output statement is executed.

The single record locking modes are automatic single and manual single. Automatic single record locking mode is described in “ Automatic Record Locking Modes” Manual single record locking mode is described in “ Manual Record Locking Modes”.

Multiple Record Locking Modes

Multiple record locking modes are specified by explicit inclusion of the WITH LOCK ON MULTIPLE RECORDS phrase in the LOCK MODE clause of the file control entry or by configuration of multiple record locking as the default for files not described with the LOCK MODE clause. In multiple record locking modes, locked records are released only upon execution of a CLOSE or UNLOCK statement that refers to the file-name, except that the successful execution of the DELETE statement causes the record lock to be released for the deleted record.

The multiple record locking modes are automatic multiple and manual multiple. Automatic multiple record locking mode is described in “ Automatic Record Locking Modes”.Manual multiple record locking mode is described in “ Manual Record Locking Modes”.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics