Compaq COBOL AAQ2G1FTK User Manual

Page 271

Advertising
background image

Sharing Files and Locking Records

8.4 Ensuring Successful Record Locking

You deaccess a record by using the next READ operation, a REWRITE or
a DELETE operation on the record, or by closing the file. In addition, you
can release locks applied by automatic record locking by using the UNLOCK
statement.

In automatic record-locking mode, you can release the current record lock
by using an UNLOCK RECORD statement or an UNLOCK ALL RECORDS
statement. (On Tru64 UNIX systems for indexed files only, there is no current
record lock.) However, because in automatic record locking you can lock only one
record at a time, the UNLOCK ALL RECORDS statement unnecessarily checks
all records for additional locks.

The sample program in Example 8–5 uses automatic record locking. The program
opens the file with I-O ALLOWING ALL. Another access stream in another
program also opens the file with INPUT ALLOWING ALL.

Note that two parallel access streams use the program in Example 8–5.

If the first access stream is updating records in random order, a record lock can
occur to the second stream from the READ until the REWRITE statement of the
first stream. Record locks can also occur to the first stream when the second
stream reads a record and the first stream tries to read the same record.

Example 8–5 Automatic Record Locking (Compaq Standard)

SELECT FILE-1

ORGANIZATION IS RELATIVE
ASSIGN TO "SHAREDAT.DAT"
.
.
.

PROCEDURE DIVISION.

OPEN I-O FILE-1 ALLOWING ALL.
READ FILE-1

AT END DISPLAY "end".

.
.
.

REWRITE FILE-1-REC.
CLOSE FILE-1.
STOP RUN.

When you close a file, any existing record lock is released automatically. The
UNLOCK RECORD statement releases the lock only on the current record on
OpenVMS Alpha systems, which is the last record you successfully accessed. On
Windows NT and Tru64 UNIX systems for indexed files only, there is no current
record lock.

Manual Record Locking (Compaq Standard)

You specify manual record locking by using the APPLY LOCK-HOLDING clause
(in the I-O-CONTROL paragraph), the OPEN ALLOWING statement, and the
ALLOWING clauses on the Compaq COBOL record operations (except DELETE).
Manual record locking allows greater control of locking options by permitting
users to lock multiple records in a file and by permitting different types of locking
to apply to different records.

Manual record locking applies the specified lock when you access the record and
releases the lock when you unlock the record.

Sharing Files and Locking Records 8–19

Advertising