FILELOCK |
![]() ![]() ![]() |
The FILELOCK statement sets a file lock on a file.
Format
FILELOCK file.var {ON ERROR statement(s)} {LOCKED statement(s)}
where
The FILELOCK statement sets a file lock which prevents other users from obtaining read or update locks on records within the file or a file lock on the whole file. File access operations that do not obtain locks are can still be performed by other users. These are CLEARFILE, DELETE, DELETEU, MATREAD, MATWRITE, MATWRITEU, READ, READV, WRITE, WRITEU, WRITEV and WRITEVU. Correct application design avoids accidental deletion or overwriting of locked records.
A file lock does not prevent access by the user that owns the lock.
The LOCKED clause is executed if another user holds a file lock or a read or update lock on any record in the file. The STATUS() function will return the user id of a process holding a lock. If the LOCKED clause is omitted, the program will wait for any lock to be released.
Example
FILELOCK STOCK SELECT STOCK TOTAL = 0 LOOP READNEXT ID ELSE EXIT READ REC FROM STOCK, ID ELSE ABORT "Cannot read " : ID TOTAL += REC<QTY> REPEAT FILEUNLOCK STOCK
This program fragment obtains a file lock on the file open as STOCK and then reads all records from the file, forming a total of the values in the QTY field. The lock prevents other users obtaining update locks when they might be updating this field in some record. The lock ensures that the total value represents a true picture of the file when the file lock was obtained. The lock is released on leaving the main processing loop.
See also: |