Module 1: DB2 Introduction
DB2 and Mainframe
On the mainframe, DB2 lives inside z/OS right next to your COBOL programs, JCL jobs and VSAM files. This page shows how the pieces connect: how a COBOL program reaches DB2, how JCL runs DB2 work, and where the data physically sits.
How a COBOL program talks to DB2
- SQL is embedded directly in the COBOL program between EXEC SQL and END-EXEC.
- Before compiling, the program goes through the DB2 precompiler, which converts each SQL statement into COBOL CALL statements.
- The extracted SQL is bound into DB2, creating a plan (or package) that DB2 will execute.
- At run time the program runs as a normal batch job; the bound plan connects it to the right tables with the right authority.
- After every SQL statement, the program should check SQLCODE: 0 means success, 100 means no rows found, negative means an error.
Running DB2 work with JCL
- Batch DB2 programs are submitted with JCL, like any other mainframe job.
- A common pattern is to run the program under IKJEFT01 and use the DSN command processor to attach to the DB2 subsystem and RUN the program with its plan.
- DB2 utilities (load, unload, reorg, image copy) are also run as batch jobs with their own control statements.
- Example JCL that runs a DB2 program:-
//RUNDB2 JOB (ACCT),'RUN DB2 PGM',CLASS=A,MSGCLASS=X //GO EXEC PGM=IKJEFT01,DYNAMNBR=20 //STEPLIB DD DSN=DB2.SDSNLOAD,DISP=SHR //SYSTSPRT DD SYSOUT=* //SYSTSIN DD * DSN SYSTEM(DB2P) RUN PROGRAM(MYPROG) PLAN(MYPLAN) END /*
- IKJEFT01 is the TSO monitor program used here to run the DSN command processor in batch.
- DSN SYSTEM(DB2P) connects to the DB2 subsystem; the subsystem name (DB2P here) is different in every shop.
- RUN PROGRAM(MYPROG) PLAN(MYPLAN) executes the bound plan for the COBOL program MYPROG.
Where DB2 data physically lives
- DB2 tablespaces and indexspaces are stored in VSAM data sets on disk (DASD).
- A STOGROUP names the DASD volumes DB2 may use, and DB2 allocates the VSAM data sets for you.
- Data read from disk passes through a memory bufferpool before reaching your program.
- So the stack is: your program, then SQL, then DB2 tables in tablespaces, then VSAM data sets on DASD, with bufferpools speeding up repeat reads.
