14.6.9 Gaining Access to Locked Records
If you are trying to access a record that is currently locked,
one possible solution is to use the REGARDLESS clause on
the GET or FIND statement. The REGARDLESS clause is
useful when you are interested in having only read access
to the specified record. Be aware, however, that using the
REGARDLESS clause to read a locked record can lead to un-
expected results because the record you read can be in the
process of being changed by another program.
Another solution is to include a WAIT clause on the GET
or FIND statement. Note that you cannot specify a WAIT
clause and a REGARDLESS clause on the same statement
line. By specifying the WAIT clause, you can tell RMS to
wait for a locked record to become available. You can op-
tionally specify an integer expression from 0 to 255 with the
WAIT clause. This integer expression indicates the number
of seconds RMS should wait for a locked record to become
available. If the record does not become available within
the specified number of seconds, RMS signals the error
``Keyboard wait exhausted'' (ERR=15).
If you do not specify an integer expression with the WAIT
clause, RMS waits indefinitely for the record to become avail-
able. Once the record becomes available, RMS delivers the
record to the program.
Note that a deadlock condition can occur when you cause
RMS to wait indefinitely for a locked record. A deadlock con-
dition occurs when two users simultaneously try to access
locked records in each other's possession. When a deadlock
occurs, RMS signals the error, ``RMS$_DEADLOCK''. In
turn, BASIC signals the error, ``Detected deadlock error while
waiting for GET or FIND'' (ERR=193). To handle this er-
ror, you can either stop trying to access the particular record,
or, if you must access the record, free all locked records (re-
gardless of the channel) and then attempt the GET or FIND
again. You need to unlock all records because you cannot
know which record the other process wants.
The following example uses the WAIT clause to overcome
a record locked condition and traps the resulting error
condition:
EXAMPLE: Click to display EXAMPLE.
The first WHEN ERROR block traps any deadlock conditions.
The WHEN ERROR handler unlocks the current record on
channel #2 in case another program is trying to access it and
then retries the operation. The detached handler for the sec-
ond WHEN ERROR block traps timeout errors and deadlock
errors. If the desired information does not become available
in the specified amount of time, or a deadlock condition oc-
curs, the employee's badge number is printed out with an
appropriate message, and the GET statement tries to retrieve
the next record in the sequence.