Return to the RPG Tips
Checking for a Record-Lock Condition in RPG
Q. How can I check for a locked-record condition in an RPG program? A. The general answer to your question is to check for status code 01218 in the file information data structure (INFDS). The code below shows how to do this in an RPG/400 program: (A) FCUSTOMERUF E K DISK KINFDS CUSTDS * (B) ICUSTDS DS I *STATUS STS * C Z-ADD1 CUNUM C CUNUM CHAINCUSTOMER 9998 * (C) * HERE IS THE TEST FOR LOCKED RECORD C *IN98 IFEQ *ON C STS ANDEQ01218 C EXSR PRCTO C MOVE *ON *INLR C RETRN C ENDIF Notice at (A) that CUSTDS is defined as the name of the data structure containing the file feedback information for the CUSTOMER file. At (B), I define an INFDS data structure for the CUSTOMER file. I named the file status field STS. Notice that the CHAIN instruction just above (C) codes 98 as the LO resulting indicator. All I/O opcodes in RPG/400 use the LO resulting indicator to trap file errors. A record-lock timeout is considered a file error. Therefore, at (C), I test indicator 98 and the STS field to determine whether a timeout has occurred. If I hadn't coded the LO resulting indicator in the CHAIN instruction, the program would abnormally terminate when a record-lock timeout occurs. In RPG IV, the most effective way to handle a record-lock timeout is with ILE condition handlers, explained in "Handle It - With RPG Exception Handlers!" (http://www.as400network.com/article.cfm?ID=2919 ). Instead of describing the exception-handler technique here, I demonstrate how to perform the RPG IV equivalent to the technique described for RPG/400 lockouts. The code below illustrates this technique: FCustomer UF E K Disk C Z-Add 1 CuNum (A) C CuNum Chain (E) Customer * Here is the test for timeout (B) C If %Status = 01218 C ExSr ProcTimeOut C Eval *INLR = *On C Return C EndIf The first thing to notice is that we don't need an INFDS feedback data structure to test for a record lockout in RPG IV. The RPG IV built-in function (BIF) %Status is sufficient for our purposes. Notice at (A) that I don't code any indicators for the Chain instruction. Instead, I code the "(E)" extender. This tells RPG I want to use error-checking BIFs such as %Error and %Status. In this example, I use only %Status. As in the RPG/400 example, I check for a timeout by testing for status code 01218 (at B). One final word about record-lock timeouts. If you think you'll test for timeouts in your programs, you might consider shortening the timeout period. By default, the CRTPF command sets the WAITRCD attribute to 60 seconds. The WAITRCD attribute determines how long the system will wait for a locked record upon access. Sixty seconds is too long for your users to wait for a timeout message. You can change the timeout associated with a file by specifying a value for the WAITRCD parameter on commands CRTPF, CRTLF, CHGPF, and CHGLF. For example, to change the timeout on the Customer file to 10 seconds, you'd code the following: CHGPF FILE(YOURLIB/CUSTOMER) WAITRCD(10) by Mike Cravitz
[report a broken link by clicking here]