Chain(NE) hang low….


Nov 17
rpg chain

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; If %Found(CustomerMasterFile);   Exsr DoCustomerStuff; Else;   Exsr OhDearIcouldntFindIt; Endif;

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:

If %Found(CustomerMasterFile);   Exsr DoCustomerStuff; ElseIf %ERROR;   Exsr HandTheFileReadError; Else;   Exsr OhDearIcouldntFindIt; Endif;

About the Author

IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Alpha Nerd and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and in the words of the most interesting man in the world: Stay thirsty my friend.