File and Record Locking
When a file is being shared by multiple programs, some means must be employed prevent the programs from interfering with each other. PL/B provides the ability to lock files and records so they cannot be accessed by other programs. These methods are called file locking and record locking. File and record locking may not be active simultaneously. File and record locking is not supported by the PLBCE runtime
File Locking
File locking is a method that grants one program exclusive access to entire files and their indexes for a short period.
File locking is implemented with a FILEPI statement in PL/B. When a file or group of files is locked with a FILEPI statement, the user is granted exclusive access to that group of files for some number of PL/B statements.
The number of statements for which the files are locked is specified in the FILEPI statement. Once the specified number of statements are executed, the files become unlocked automatically. However, not all instructions apply against the count in the FILEPI statement. In addition, untrapped runtime errors and some instructions cancel the file lock. The lock may be programmatically canceled by specifying zero as the operand in the FILEPI statement.
When files are locked due to a FILEPI statement, another FILEPI statement may not be executed. This means that the program must lock all of the files that are locked for a particular operation at once. This limitation means that indefinite postponement due to mutual exclusion will never occur in a system that uses file locking.
Record Locking
Record locking is a method that provides exclusive access to individual records. This allows multiple users to access a file simultaneously while allowing the individual users to protect the data they are working on from other users.
Because users compete for records and not whole files, systems that use record locking will usually have better performance than those that use file locking.
Record locking is not limited to short periods of time. A program may lock a record and keep it locked as long as necessary.
Record locking is not supported when using SQLIO.
Three types of record locking are supported. The appropriate type may be selected using the PLB_RECORDLOCK keyword.
Record Locking Modes
Two record locking modes are available with PL/B: manual and automatic. The desired mode is specified when a file is opened.
If a file is opened specifying the manual locking mode, special forms of the read-class instructions may be employed read a record and lock it. The normal forms of the read-class instructions do not lock records. The locking forms of the read instructions are formed by adding the suffix "LK" to a normal read-class instruction. For example, the READKS instruction does not lock records. READKSLK (LK is appended to the end of the READKS instruction) is a read key sequential with lock.
If a file is opened specifying the automatic locking mode, any time a record is read, it is locked.
Record Unlocking Modes
Two unlocking modes are available with PL/B: multiple and single. The desired mode is specified when a file is opened.
If a file is opened specifying the multiple unlocking mode, all locked records remain locked until specifically unlocked. An UNLOCK instruction is provided to unlock all locked records in a file. Once multiple records are locked it is not possible to unlock only one record in a file.
With the single unlocking mode, only one record in a file may be locked at a time. When an operation is performed on a file and it has a locked record that record is unlocked according to the following:
A read instruction unlocks any previously locked record before the read is attempted.
An update or delete operation unlocks the record after the operation is complete.
A write to a record that is not locked unlocks the currently locked record before the write starts.
A write to the locked record unlocks the record when the operation completes.
If a file is opened with record locking, it is not possible to use that file in a FILEPI statement. An A03 error is given if a FILEPI is attempted. If a file is opened without record locking, it is not possible to use the any of the locking READ variants or the UNLOCK statement. An A04 error is given if attempted.
Record Locking Wait Options
If a record is locked by another user and an attempt is made to read or lock it, the time to wait for the record to become unlocked may be specified. The available options are:
Wait forever for the record. Mutual exclusion may be a particular problem when the operation to wait forever is selected. In these cases, care should be taken to prevent mutual exclusion.
Wait for some period for the record. A time period may be specified in the OPEN instruction. If the record is locked when the operation is attempted and stays locked for the time period, the LESS flag is set after the operation.
Return immediately. The LESS flag is set after the operation.
Locking in Different Operating Environments
Various operating environments implement locking (file or record) differently. Some systems guarantee locks and others do not. A lock is guaranteed if the record or file cannot be access by another user even if that user does not attempt to lock the record or file. If locks are not guaranteed by the system, a program that updates a file or record without locking it first may interfere with those using locking and may even damage the file.
To prevent unexpected results on systems that do not guarantee locks, it is important to ensure that locking strategies are consistent in all programs accessing a file or group of files. Before implementing a locking strategy in some programs and not in others sharing the same file, review the appropriate sections of your operating system documentation to ensure that the results are expected. In addition, such systems should not be considered portable to environments other than the one for which they are designed.
Record Locking Linux OS Considerations
By default, the Sunbelt record locking implementation uses the Linux OS write lock (exclusive lock) for each data record locked in a PL/B data file. This insures that only one process can hold a lock for any one specific data record.
However, the Linux OS also supports a read lock which allows multiple processes to hold a read/shared lock on the same PL/B data file record at the same time. The Linux PL/B runtime supports a SETMODE *LINUXREADLOCK={0|1} instruction to allow files opened with LOCKAUTO, SINGLE, and NOWAIT parameters to use Linux OS read locks for PL/B read instructions. In this case, only the following PL/B read instructions can apply/use the Linux OS read/shared locks.:
|
File Type |
PL/B Reads that support Linux OS read locks |
|
FILE |
READ |
|
AFILE |
READ, READKG, READKGP |
|
IFILE |
READ, READKS, READKP |
See Also: Disk I/O Instructions
![]() |