MODULE 13 · MAINFRAME INTERNALS AND CAPSTONE · 7/10

CICS and Db2 in a nutshell

18 min10 XPQuiz

Batch is where most new COBOL developers start, but job adverts almost always mention CICS and Db2 too. This course's sandbox has neither, so nothing in this lesson can be run here. The goal is that you can read such programs and talk about them confidently.

Both work the same way from COBOL's point of view: you write EXEC CICS ... END-EXEC or EXEC SQL ... END-EXEC blocks inside normal COBOL. Before compiling, a translator (CICS) or precompiler (Db2), today usually built into the compiler, turns each block into CALLs to the product's API.

CICS: online transactions

CICS runs thousands of short transactions at once, each started by a 4-character transaction id (for example ACCT) mapped to a program. A user at a 3270 screen, an ATM or a web API call triggers one; the program answers and ends in milliseconds.

       PROCEDURE DIVISION.
           IF EIBCALEN = 0
               PERFORM SEND-EMPTY-SCREEN
           ELSE
               MOVE DFHCOMMAREA TO WS-COMMAREA
               EXEC CICS RECEIVE MAP('ACCTMAP') MAPSET('ACCTSET')
                    INTO(ACCTMAPI) RESP(WS-RESP)
               END-EXEC
               MOVE ACCTNOI TO WS-ACCT-KEY
               EXEC CICS READ FILE('ACCTMAST') INTO(ACCT-REC)
                    RIDFLD(WS-ACCT-KEY) RESP(WS-RESP)
               END-EXEC
               IF WS-RESP = DFHRESP(NOTFND)
                   MOVE 'ACCOUNT NOT FOUND' TO MSGO
               ELSE
                   MOVE ACCT-BALANCE TO BALO
               END-IF
               EXEC CICS SEND MAP('ACCTMAP') MAPSET('ACCTSET')
                    FROM(ACCTMAPO) DATAONLY
               END-EXEC
           END-IF
           EXEC CICS RETURN TRANSID('ACCT')
                COMMAREA(WS-COMMAREA)
           END-EXEC.

What to notice:

  • Pseudo-conversational design. The program sends a screen and ends with RETURN TRANSID. CICS passes the COMMAREA to the next run when the user presses a key. EIBCALEN = 0 means "first time in".
  • BMS maps describe screens; the map generates copybooks with input (...I) and output (...O) fields.
  • CICS owns the resources. No OPEN, CLOSE, READ, DISPLAY, ACCEPT or STOP RUN. Files (usually VSAM KSDS) are read with EXEC CICS READ FILE ... RIDFLD(key).
  • RESP returns a condition such as NORMAL or NOTFND, the CICS equivalent of checking a file status.
  • The EIB (Execute Interface Block) holds facts about the task: EIBCALEN, EIBAID (which key was pressed), EIBTRNID.

Db2: SQL inside COBOL

Db2 for z/OS is IBM's relational database. COBOL talks to it through host variables, COBOL fields prefixed with : inside SQL:

           EXEC SQL
               SELECT CUST_NAME, CREDIT_LIMIT
                 INTO :WS-CUST-NAME, :WS-CREDIT-LIMIT
                 FROM CUSTOMER
                WHERE CUST_ID = :WS-CUST-ID
           END-EXEC
           EVALUATE SQLCODE
               WHEN 0      PERFORM USE-CUSTOMER
               WHEN +100   PERFORM CUSTOMER-NOT-FOUND
               WHEN OTHER  PERFORM SQL-ERROR
           END-EVALUATE

For more than one row you use a cursor. The loop stops at +100 (no more rows) or at any error:

           EXEC SQL DECLARE C1 CURSOR FOR
               SELECT CLAIM_ID, AMOUNT FROM CLAIM
                WHERE STATUS = 'OPEN' ORDER BY CLAIM_ID
           END-EXEC
           EXEC SQL OPEN C1 END-EXEC
           PERFORM UNTIL SQLCODE NOT = 0
               EXEC SQL FETCH C1 INTO :WS-CLAIM-ID, :WS-AMOUNT
               END-EXEC
               IF SQLCODE = 0 PERFORM PROCESS-CLAIM END-IF
           END-PERFORM
           EXEC SQL CLOSE C1 END-EXEC

Things interviewers ask about:

  • SQLCODE in the SQLCA: 0 ok, +100 not found, -803 duplicate key, -811 more than one row, -911 deadlock or timeout, -805/-818 package missing or out of step with the program.
  • DCLGEN generates the table's host-variable copybook. DECIMAL(9,2) becomes PIC S9(7)V99 COMP-3, INTEGER becomes PIC S9(9) COMP.
  • Null indicators: a PIC S9(4) COMP field after the host variable (:WS-PHONE:WS-PHONE-IND) is negative when the column is null.
  • Precompile and BIND: the SQL is extracted into a DBRM and bound into a package, which is why a recompile without a rebind gives -818.
  • COMMIT frequency in long batch jobs, so locks are released and a restart doesn't lose hours of work.

On the job

Most batch programs you maintain will mix all of this: a Db2 cursor as input, VSAM lookups, a sequential report. The COBOL around the EXEC blocks is the same COBOL you have been writing all course. IBM Z Xplore (free) lets you run real CICS and Db2 exercises when you're ready.

Check your understanding

1. In a pseudo-conversational CICS program, what happens while the user is reading the screen?
2. A singleton SELECT ... INTO returns SQLCODE +100. What does it mean?
3. Which statement is NOT allowed in a CICS program?
4. How does a COBOL program read a Db2 result set of many rows?