Handling Files

CICS allows you to access file data in a variety of ways. In an online system, most file accesses are random, because the transactions to be processed aren’t batched and sorted into any kind of order. Therefore CICS supports the keyed access method provided by VSAM. It also allows you to access data using a variety of database managers.

This book covers only VSAM Key-Sequenced Data Sets (KSDS) that are accessed by supplying a key. Other VSAM data set format types—Entry-Sequenced Data Sets (ESDS) and Relative-Record Data Sets (RRDS) are described in more detail in the CICS Application Programming Guide. CICS also supports sequential access to keyed files in several forms; one of these, browsing, is covered in the next section.

The most important information kept for each file is the symbolic filename (normally the MVS DDNAME) or the data set name. When a CICS program makes a file request, it always uses a symbolic filename. CICS looks up this name in the FCT, and from the information there it makes the appropriate request of the operating system. This technique keeps CICS programs independent of the specific resources on which they are operating.

In the examples that follow, we’ll use the symbolic filename ACCTFIL for the account file and ACCTNAM for the name file which is actually a path to the same data set via an alternate index on the last name (surname).

There are three READ commands:

The sample application uses the first two: READ and READNEXT as seen in Browsing a File, later in this chapter.

The command to read a single record from a file is:

EXEC CICS READ FILE(filename)
               INTO(data-area) LENGTH(data-area)
               RIDFLD(data-area)
           options
END-EXEC

The options for this command are:

FILE(filename)

Specifies the name of the file that you want to access. It is required in all READ commands. The entry in the CICS File Control Table (FCT) is used to find out the characteristics of the data set and whether it is on a local or remote system. Filenames can be up to eight characters long and, like any parameter value, should be enclosed in quotes if they are literals.

INTO(data-area)

Specifies the name of the data area into which the record retrieved from the data set is to be written. When INTO is specified, LENGTH must be specified or must be capable of being deduced from the INTO option. INTO is used in the sample application associated with this book.

LENGTH(data-area)

Specifies the maximum number of characters where the record is to be put. The LENGTH parameter is required for access to variable record length files. After the READ command is completed, CICS replaces the maximum value you specified with the true length of the record. For this reason, you must specify LENGTH as the name of a data area rather than a literal. For the same reason, you must re-initialize this data area if you use it for LENGTH more than once in the program. An overlong record raises an error condition. For the uses of the READ command we’re covering in this book, the LENGTH option is not required because it is a fixed length file and can always be deduced from the INTO parameter.

RIDFLD(data-area)

Specifies the record identification field. The contents can be a key, a relative byte address (RBA for an ESDS), or relative record number (RRN for RRDS). This parameter is also required.

Immediately upon completion of the command, the RIDFLD data area is available for reuse by the application program, even if UPDATE was specified.

Other options (partial list)

Any of the following options which apply to this command can be used. Note that multiples of these may be used. Except where noted, you can use them in any combination.

In program NACT02, we need to find out whether the requested record is there. The command we use is shown in Example 5-1.

The major return code will be placed in CS-CRUD-RESP and a qualifier may be supplied in CA-CRUD-REAS.

In this command, the record is placed in the data area named NACTREC-DATA so it should be a data structure corresponding to the account file record. You could define this structure directly in the program, but you’ll also need it in several other programs. So instead you should put the record definition into a library in NACCTREC and copy it into the NACCRUD copybook, which is then copied into the NACT02 program.

In NACCCRUD:

10  NACTREC-DATA.
    COPY NACCTREC.

In NACT02:

01  DFHCOMMAREA
    COPY NACCCRUD.

Here ACCTDO IN NACTREC-DATA is where the account number was passed by the frontend that called the program. NACTREC-DATA is the location where the data is to be passed back to the frontend program.

In any application, it is a good idea to keep your record layouts in a library and copy them into the programs that need them. Even in the simplest of applications, the same record is usually used by several programs. This procedure prevents multiple programs from using different definitions of the same thing.

This argument applies equally well to any structure used in common by multiple programs. Map structures are a prime example, as are parameter lists and communication areas, which we’ll discuss later. Apart from its value in the initial programming stage of an application, this technique greatly reduces the effort and hazards associated with any change to a record or map format. You can make the changes in just one place (your library) and then simply recompile all the affected programs. Example 5-1 also introduces the method of testing the major CICS return code. The CICS translator replaces the DFHRESP(condition) with the numeric return code so programmers are able to write intelligible code.

Example 5-2 shows the COBOL record definition we need of the account file in the sample application. Its fields are described beginning at the 20 level in order to allow it to be used within other structures.

Example 5-2. Record Definition for the Account File

20  ACCTDO               PIC X(5).   //  Account number
20  SNAMEDO              PIC X(18).  //  Last/surname
20  FNAMEDO              PIC X(12).  //  First name
20  MIDO                 PIC X.      //  Middle initial
20  TTLDO                PIC X(4).   //  Title e.g., Mr./Mrs.
20  TELDO                PIC X(10).  //  Telephone number
20  ADDR1DO              PIC X(24).  //  Address line 1
20  ADDR2DO              PIC X(24).  //  Address line 2
20  ADDR3DO              PIC X(24).  //  Address line 3
20  AUTH1DO              PIC X(32).  //  Additional Authorized Card  User 1
20  AUTH2DO              PIC X(32).  //  Additional Authorized Card  User 2
20  AUTH3DO              PIC X(32).  //  Additional Authorized Card  User 3
20  AUTH4DO              PIC X(32).  //  Additional Authorized Card  User 4
20  CARDSDO              PIC X.      //  Number of cards issued to Customer
20  IMODO                PIC X(2).   //  Month card issued
20  IDAYDO               PIC X(2).   //  Day card issued
20  IYRDO                PIC X(2).   //  Year card issued
20  RSNDO                PIC X.      //  Reason code for card issued
20  CCODEDO              PIC X.      //  Card status coded e.g. G for Gold
20  APPRDO               PIC X(3).   //  Code of card issue approvers
20  SCODE1DO             PIC X.      //  Additional privilege code 1
20  SCODE2DO             PIC X.      //  Additional privilege code 2
20  SCODE3DO             PIC X.      //  Additional privilege code 3
20  STATDO               PIC X(2).   //  Account status
20  LIMITDO              PIC X(8).   //  Customer Account Credit Limit
20  PAY-HIST OCCURS 3.               //  Pay History first of last three
    25  BAL              PIC X(8).   //  Account Balance
    25  BMO              PIC 9(2).   //  Month
    25  BDAY             PIC 9(2).   //  Day
    25  BYR              PIC 9(2).   //  Year
    25  BAMT             PIC X(8).   //  Amount of balance
    25  PMO              PIC 9(2).   //  Payment month
    25  PDAY             PIC 9(2).   //  Payment day
    25  PYR              PIC 9(2).   //  Payment year
    25  PAMT             PIC X(8).   //  Payment mmount

The record format is already fixed and used by batch programs. Our online CICS sample program must use the same record definition.

When you search by name using NACT05, you need to be able to point to a particular record in the file, based on end-user input which maybe anywhere in the file. Then you can start reading the file sequentially from that point on. The need for this combination of random and sequential file access—called browsing—arises frequently in online applications. Consequently, CICS provides a special set of EXEC CICS browse commands to make this job easier, namely: STARTBR, READNEXT, READPREV, and ENDBR.

Before looking at these commands, a few words about the performance implications of browsing. Transactions that produce lots of output screens can monopolize system resources. A file browse is often guilty of this. Having a long browse can put a severe load on the system, delaying other transactions and increasing overall response time.

The CICS default design model assumes the end user initiates a transaction that accesses a few data records, processes the information, and returns the results to that user. This process may involve numerous I/O waits that allow CICS to schedule and run other tasks. However, CICS is not an interrupt-driven multitasking system—tasks that involve small amounts of I/O relative to processing are able to monopolize the system regardless of priority.

Starting a browse does not make the first eligible record available to your program; it merely tells CICS where you want to start when you begin issuing read commands. To get the first record, and for each one in sequence after that, you use the READNEXT command:

EXEC CICS READNEXT FILE(filename)
                   INTO(data-area) LENGTH(data-area)
                   RIDFLD(data-area)
END-EXEC

The FILE, INTO and LENGTH parameters are defined in the same way as they are in the READ command. You need the FILE parameter because CICS allows you to browse several files at once, and the FILE parameter tells which one to read.

The first thing you have to do is construct a key that starts the browse in the right place. The key of the name file consists of the last name (surname). The idea is to build a key that consists of the characters the user keyed in as the last name (surname). Then you can use the GTEQ option on our STARTBR command to get the first qualifying record. For example:

05  WS-LIMIT-NAMES.
    10  WS-BROWSE-SNAME             PIC X(18) VALUE SPACES.
    10  WS-MAX-SNAME                PIC X(18) VALUE SPACES.
    10  WS-MIN-FNAME                PIC X(12) VALUE SPACES.
    10  WS-MAX-FNAME                PIC X(12) VALUE SPACES.

You can then set up the parameters for the search limits. This data comes from the area passed to the NACT05 program which performs this name search. You also need to know where to stop the browse.

Certainly this stops when you overflow the capacity of the data passing area, but you may run out of eligible names before that. So you need to construct a last name value that is the highest alphabetically that could meet your match criteria. If the last name in the record exceeds this value, you know that you’ve read all the (possibly) eligible records. This limiting value is named WS-MAX-SNAME.

Finally, as you read, you need to test whether the forename matches sufficiently to return the record to the caller or not. If you define WS-MIN-FNAME as the smallest allowable value and WS-MAX-FNAME as the largest, then you need the following code to set up all of the relevant areas:

 B-010.
     MOVE SNAMEDO IN CA-BRWS-ENTRY (1) TO WS-PASSED-SNAME
                                          WS-BROWSE-SNAME
                                          WS-MAX-SNAME
*
     INSPECT WS-MAX-SNAME REPLACING ALL SPACES BY HIGH-VALUES
*
     MOVE FNAMEDO IN CA-BRWS-ENTRY (1) TO WS-PASSED-FNAME
                                          WS-MAX-FNAME
                                          WS-MIN-FNAME
*
     INSPECT WS-MAX-FNAME REPLACING ALL SPACES BY HIGH-VALUES
     INSPECT WS-MIN-FNAME REPLACING ALL SPACES BY LOW-VALUES

SNAMEDO IN CA-BRWS-ENTRY(1) is where the input last name is passed to the program. Similarly FNAMEDO IN CA-BRWS-ENTRY(1) is where the input forename is passed to the program.

So we can now look at how the code is put together with these commands. But we need to introduce a couple of controlling values:

05  WS-AVAILABILITY-IND             PIC X.
    88  SOME-AVAILABLE              VALUE 'Y'.
    88  NONE-AVAILABLE              VALUE 'N'.

WS-AVAILABILITY-IND is used to control the reading loop. Also we need to note a part of the area passed to the program:

10  CA-BRWS-FOUND               PIC 9(4).
    88  CA-BRWS-NONE-FOUND      VALUE ZERO.
10  CA-BRWS-MORE                PIC 9(4).
10  CA-BRWS-MORE-X REDEFINES CA-BRWS-MORE
                               PIC X(4).
    88  CA-BRWS-NO-MORE         VALUE '0000'.

CA-BRWS-FOUND indicates how many matches were found for the criteria supplied. CA-BRWS-MORE indicates if there were more matches than the number allowed for. These are copied into NACT05 from copybook NACCBRWS. Example 5-3 shows the copybook. It is also found on the CD-ROM in \Cobol Application\PC\source\copy.

Example 5-3. Sample of the NACCBRWS Copybook

* The interface to the Browse program is described in a copy book
* in order to ensure consistency. The values in this area designed
* to be in character format to enable ease of translation when the
* program is invoked from a remote system which uses a different
* encoding scheme (e.g., ASCII) than the EBCDIC of the mainframe.
*
* This is the working storage version of the interface to the
* Browse program.
*
     05  WS-BRWS-COMMAREA.
*
* This is an "Eyecatcher" and integrity check field.
*
         10  WS-BRWS-VERSION             PIC XXX VALUE SPACES.
             88  WS-BRWS-CORRECT-VERSION VALUE 'V1A'.
*
* Only two functions are provided by the Browse program:
* initiation of a Browse and Continuation of a previously
* initiated browse.
*
         10  WS-BRWS-FUNCTION            PIC X VALUE SPACE.
             88  WS-BRWS-REQ-BROWSE      VALUE 'B'.
             88  WS-BRWS-REQ-CONTINUE    VALUE 'C'.
             88  WS-BRWS-VALID-REQUEST   VALUE 'B' 'C'.
*
* The response field is designed to conform to the CICS EIBRESP
* characteristics which always contains a numeric value. There
* are also architected values to indicate errors detected by the
* Browse program itself. If there was an interface error, this
* contains a special value of 'FRMT'.
*
         10  WS-BRWS-RESP                PIC 9(4) VALUE ZERO.
         10  WS-BRWS-RESP-X REDEFINES WS-BRWS-RESP
                                         PIC X(4).
             88  WS-BRWS-NO-ERROR        VALUE '0000'.
             88  WS-BRWS-BAD-FORMAT      VALUE 'FRMT'.
*
* The reason field is designed to conform to the CICS EIBRESP2
* characteristics which always contains a numeric value. There
* are also architected values to indicate errors detected by the
* Browse program itself. If there was an interface error, this
* contains 'VERE' for Version Error, 'LENE' for Length Error (if
* possible), 'REQE' for Request Error, 'LIME' for Limit Error or
* 'MORE' for More Error (only occurs for a continuation request).
*
         10  WS-BRWS-REAS                PIC 9(4) VALUE ZERO.
         10  WS-BRWS-REAS-X REDEFINES WS-BRWS-REAS
                                         PIC X(4).
             88  WS-BRWS-VERSION-ERROR   VALUE 'VERE'.
             88  WS-BRWS-LENGTH-ERROR    VALUE 'LENE'.
             88  WS-BRWS-REQUEST-ERROR   VALUE 'REQE'.
             88  WS-BRWS-LIMIT-ERROR     VALUE 'LIME'.
             88  WS-BRWS-MORE-ERROR      VALUE 'MORE'.
*
* If the response contains a numeric value, this contains the
* character representation of the EIBFN value giving rise to
* the exception condition.
*
         10  WS-BRWS-CICS-FUNCTION       PIC 9(5) VALUE ZERO.
         10  WS-BRWS-CICS-FUNCTION-X
                REDEFINES WS-BRWS-CICS-FUNCTION
                                         PIC X(5).
*
* In order to prevent excessive searches, the caller must specify
* the maximum number of matches (s)he is prepared to handle.
* Also because a COMMAREA is limited to a maximum of approximately
* 32,000 bytes, the maximum limit has been set at 80.
*
         10  WS-BRWS-LIMIT-TO-GET        PIC 9(4) VALUE ZERO.
         10  WS-BRWS-LIMIT-TO-GET-X REDEFINES WS-BRWS-LIMIT-TO-GET
                                         PIC X(4).
*
* The Browse program indicates the number of matches found.
* The range is zero to the limit.
*
         10  WS-BRWS-FOUND               PIC 9(4) VALUE ZERO.
             88  WS-BRWS-NONE-FOUND      VALUE ZERO.
*
* After satisfying the limit, the Browse program will place
* either '0000' in here if there are no more records satisfying
* the search criteria or a number if there are more. On a
* continuation request this number must be returned to the Browse
* program since it is used to reposition the request.
*
         10  WS-BRWS-MORE                PIC 9(4) VALUE ZERO.
         10  WS-BRWS-MORE-X REDEFINES WS-BRWS-MORE
                                         PIC X(4).
             88  WS-BRWS-NO-MORE         VALUE '0000'.
*
* The records found on file for a match. Input is in the
* surname and first name fields of the first Entry.
*
         10  WS-BRWS-MATCHES.
             15  WS-BRWS-ENTRY           OCCURS 80.
*
* The description of the account record is placed in a copy book.
*
             COPY NACWTREC.

The code shown in Example 5-4 initializes the browse of the file using the STARTBR command and sets the availability indicator based on the possibility of a match.

The code shown in Example 5-5 attempts to read a record from the file and sets the availability indicator based on its success.

The code shown in Example 5-6 terminates the browsing operations against the file.

There are three file output commands:

A wide variety of conditions can arise when using file commands. Here are some of the conditions passed to your program in the RESP field that can arise when you use the file commands that have just been described. A mapping between the response value and the following name can be found in the NACT04 program:

DISABLED

Occurs if a file is disabled. A file may be disabled because:

DUPKEY

Means that if a VSAM record is retrieved by way of an alternate index with the NONUNIQUEKEY attribute; another alternate index record with the same key follows. It does not occur as a result of a READNEXT command that reads the last of the records having the non-unique key.

DUPREC

Means that there is already a record in the file with the same key as the one that you are trying to add with a WRITE command. This condition may result from a user error or may be expected by the program. In either of these cases, there should be specific code to handle the situation. This condition is handled in the NACT02 program when dealing with the locking file, since this could mean another user has already locked the account.

It can also fall into the should-not-occur category. In this case no special code is required beyond logging information that may help find the problem, cleaning up if necessary, and identifying the problem to the user.

ENDFILE

Means that you’ve attempted to read sequentially beyond the end of the file in a browse (using the READNEXT command). This is a condition that you should program for in any browse operation. In the example application, for instance, a search on “Zuckerman” or a similar name might cause ENDFILE, and you code for it explicitly as shown in Example 5-4 in the C-START-BROWSE SECTION in the browse program, NACT05.

FILENOTFOUND

Means that the symbolic filename in a file command cannot be found in the File Control Table. This is often a coding error; look for a difference in spelling between the command and the FCT entry. If it happens after the program is put into actual use (“in production”), look for an accidental change to the definition for that file. The file may be on another system or it hasn’t been created; there are any number of possibilities that could cause this response.

ILLOGIC

Is a catch-all class for errors detected by VSAM that don’t fall into one of the other categories that CICS recognizes. The RESP2 value will tell you the specific error.

INVREQ

Means that CICS regards your command as an invalid request for one of the following reasons:

IOERR

Means that the operating system is unable to read or write the file, presumably because of physical damage. This can happen at any time, and there is usually nothing to do in the program except to abend the transaction and inform the user of the problem.

ISCINVREQ

Means that the remote system indicates a failure which does not correspond to a known condition.

LENGERR

Is usually caused by a coding error and could result from one of the following reasons:

NOSPACE

Means that there’s no space in the file to fit the record you’ve just tried to put there with a WRITE or REWRITE command. This doesn’t mean that there’s no space at all in the data set; it simply means that the record with the particular key you specified will not fit until the file is extended or reorganized. Like IOERR, this may occur at any time, and should be handled accordingly.

NOTAUTH

Means that a resource or command security check has failed.

NOTFND

Means that there is no record in the file with the key specified in the RIDFLD, parameter on a READ, READNEXT, READPREV, STARTBR, or DELETE command. NOTFND may result from a user error, may be expected by the program, or may indicate an error in the program logic:

IF NOT WS-CRUD-BAD-LOCK
   EVALUATE WS-CRUD-RESP
     WHEN DFHRESP(NORMAL)
      CONTINUE
     WHEN DFHRESP(NOTFND)
        IF NOT CA-SENT-MENU
          PERFORM TA-BAD-CRUD-RESPONSE (Handle abend)
        END-IF
     WHEN OTHER
      PERFORM TA-BAD-CRUD-RESPONSE
   END-EVALUATE
 END-IF

When adding:

      EVALUATE TRUE
**
* when this account already exists, the user must be informed
*
        WHEN WS-CRUD-NO-ERROR
            SET MSG-DUPLICATE TO TRUE
            MOVE-1  TO ACCTML
            MOVE DFHBMBRY TO ACCTMA
            SET WS-BB-ERROR-PRESENT TO TRUE
        .
        .
        .
      END-EVALUATE
NOTOPEN

Occurs if:

As you have probably gathered from this description, NOTOPEN usually results from an operations problem, and you may want to notify the operations staff of the problem, or send a message to the user to do so.

SYSIDERR

Means that the SYSID option specifies either a name of a CICS system that is not defined in the intersystem table or a system to which the link is closed.

Before leaving the topic of file commands, we’ll list some of the other facilities that are available. You can find guidance information on using file control in the CICS Application Programming Guide, and a full list of commands, options, and exceptional conditions in the CICS Application Programming Reference. Both of these books are part of the CICS library and are found on the CD-ROM.



[2] CEMT is a CICS-supplied transaction that enables administrators to query and alter the state of CICS resources.