|
Avoiding Record Locks in RPG
Hey, Ted:
Some of our users are in the habit of using file maintenance programs to look up information. We've
provided them with inquiry screens, but old habits die hard.
This practice causes problems for other jobs that update the
locked records. The programs freeze for a minute or two and then
send an error message.
How can we keep an RPG program from freezing up when it tries
to randomly read a record that is locked by another job?
-- Andy
That is a very good question, Andy. I assume you're already
checking to make sure that the record was found. You can add a
similar check for an error condition. Code an indicator in the
LO resulting indicator position.
The following RPG code illustrates this technique:
FCUSTOMERUF E K DISK
F KINFDS INFO
I SDS
I 91 170 ERRMSG
IINFO DS
I *STATUS FSTAT
** HILOEQ
C CUSKEY CHAINCUSTREC 4142
C SELEC
C *IN42 WHEQ *ON
.... put record-locked logic here
C *IN41 WHEQ *ON
.... put record-not-found logic here
If the record is locked, the system turns on indicator 42 and loads an error message into positions 91
through 170 of the program status data structure. The message looks like this:
Record 5 in use by job 185008/SMITH/SMITHJOB.
Also, the system will place the value 01218
in the *STATUS subfield (positions
11 through 15) of the file information data structure.
If you're using RPG IV, you can throw away the indicators. Use
the E operation extender on the chain op code to tell RPG to signal
an error, and follow with a test for the %ERROR
and %STATUS built-in functions.
Here's an example:
FCUSTOMER UF E DISK
DSDS SDS
D ERRMSG 91 170
C CUSKEY CHAIN(E) CUSTREC
C SELECT
C* Error retrieving record
C WHEN %ERROR
C* Record lock error
C IF %STATUS = 1218
C ENDIF
C WHEN %FOUND
C UPDATE CUSTREC
C OTHER
C ENDSL
The delay time is an attribute of the database file. The default
is 60 seconds. Use the Maximum Record Wait Time (WAITRCD)
parameter of the Create Physical File (CRTPF),
Change Physical File (CHGPF),
or Override with Database File (OVRDBF)
commands to adjust this value. If you do not want the job to wait
for the record to become available, specify WAITRCD(*IMMED),
as this override command illustrates:
OVRDBF FILE(CUSTOMER) WAITRCD(*IMMED)
I want to thank Barbara Morris of IBM Toronto for her assistance in working up this tip.
-- Ted
|