FILELOCK

FILELOCK

Top  Previous  Next

 

The FILELOCK statement sets a file lock on a file.

 

 

Format

 

FILELOCK file.var {ON ERROR statement(s)}

{LOCKED statement(s)}

{THEN statement(s)}

{ELSE statement(s)}

 

where

 

file.varis the file variable associated with the file.

 

statement(s)are statements to be executed depending on the outcome of the operation.

 

 

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.

 

The THEN and ELSE clauses are both optional and neither need be present. The THEN clause will be executed if the file lock is successfully obtained. The ELSE clause will be executed if the lock cannot be obtained for a reason other than it being held by some other user. There are currently no conditions that would execute the ELSE clause.

 

 

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:

Locking, FILEUNLOCK