How to determine in a rpgle program if a file is locked or not?

614 Views Asked by At

Suppose , my program processes a file , but before processing the file, how can I check if the file is locked (I want to check this because I dont want the program to crash in case of a file lock).

If I do a Chain(E) (File Key) Critical_File and then check %error , this case it will not tell me if the specific error is for a File Lock case.

So in a RPGLE program how can I get the status if a file used by the program is locked?

A answer will be really helpful.

2

There are 2 best solutions below

1
nfgl On

There is multiple ways.

One can use a minimal infds like

dcl-f file usage(*update) keyed infds(infds);

dcl-ds infds qualified;
  status *status;
end-ds;

chain(e) key file;
if %error and infds.status = 01218
  dsply '%error';
endif;

But since %error monitor/on-error has been added to the language (notice there is no operation extender here)

monitor;
  chain key file;
on-error 01218;
  dsply 'on-error';
endmon;

And monitor/on-excp has recently been added (notice there is no operation extender here either)

monitor;
  chain key file;
on-excp 'CPF5027';
  dsply 'on-excp';
endmon;
0
Buck Calabro On

There are several ways to check for file locks:

List Object Locks (QWCLOBJL) API

OBJECT_LOCK_INFO view

ALCOBJ

Since checking for a lock requires doing so before opening the file, you might consider avoiding the check and handling the error instead. Change the file to USROPN, and check for the error on the OPEN operation.