Chain(NE) hang low….

November 17, 2015

The wonderful IBM RPGLE programming language:

What is Chain(N) and Chain(NE)

Overview

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;

NickLitten


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad 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 remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

  • Your comments are the best, now I have a question program chain a xxx file and calls another program that chains same file and update, return to previous progra to update xxx and getting error. Can not update without prior input operation. I want to avoid doing another chain, any suggestion?

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Subscribe NOW
    7-day free trial

    Take This Course with ALL ACCESS

    Unlock your Learning Potential with instant access to every course and all new courses as they are released.
     [ For Serious Software Developers only ]

    Online Learning for IBM i Software Technology Professionals

    “The more that you read, the more things you will know. The more that you learn, the more places you’ll go.” – Dr. Seuss

    >