The wonderful IBM RPGLE programming language:
What is Chain(N) and Chain(NE)
For file I/O requests in RPG (ie: Chain, Read, Reade, Setll, etc) we can add some options using
(N) – No Lock
(E) – Error Logging
(NE) – No Lock and Error Logging
Writing code in RPG the CHAIN operation code is used to go and get a specific ROW (or record) of data from a file. It returns the first entry that matches the KEY that is being used. In this blog let’s look at what happens when the file(CustomerMasterFile) is defined in our program as an UPDATE file. So, if we wanted to retrieve the Customer information from a Customer Master file (assuming it is keyed by the company name and the customer id number) we might code it simply like this:
CHAIN (Company : CustomerNumber) CustomerMasterFile;
which is basically the same as:
SETLL (Company : CustomerNumber) CustomerMasterFile; READE (Company : CustomerNumber) CustomerMasterFile;
This will get the data that matches the key criteria (Company and Customer) and it will place a record lock to prevent any other programs reading that record while we are exclusively using it. Now, we can add these extender things to add extra functions. So, you might also see this code like this
CHAIN(N) (Company : CustomerNumber) CustomerMasterFile;
And this means – read the data but do not lock the record (obviously this only applies to files defined as update capable). For files defined as UPDATE then records are *locked ready to be updated. Using the (N) extender says – read this update file, treat it like an INPUT file and do not lock the record you are reading.
The trouble is that reading a record with (N) *NOLOCK might still fail if somebody else has already grabbed that record and is exclusively locking it.
So what if we see this:
CHAIN(NE) (Company : CustomerNumber) CustomerMasterFile;
This extends the function to say N=NOLOCK and E=ERRORLOGGING so we are given more information about the CHAIN attempt. Code to use the NE example might look something like this:
I’m trying to get the next record if a record lock?
If a record is locked you cannot read (for update) until that lock is released… but you can read it if you are using an INPUT ONLY or using SQL