Implementation of restart logic in a COBOL-DB2 program

DB2DatabaseBig Data AnalyticsCOBOL

Problem: A COBOL-DB2 program takes the data from an input file having 1000 records and inserts the data in a DB2 table. The program failed after 432nd record. How will you implement restart logic?


The restart logic can be implemented in a COBOL-DB2 program by fixing a commit frequency. If we choose a commit frequency of 100, then the following steps need to be performed:

  • Declare a variable for a counter, say WS-COUNT.
  • Place a loop in which we will read the record from the file and insert it in a database. Increment the counter WS-COUNT by one each time a record is inserted successfully.
  • As soon as WS-COUNT reaches the commit frequency 100, we will write the last inserted record key (key field) in a separate output file, initialize WS-COUNT and fire COMMIT statement. Make sure to open this separate output file in I/O mode so that we can REWRITE the record each time.
  • Repeat the above process for each 100th record inserted, i.e., 100th, 200th, 300th and so on.
  • When the last record i.e. 1000th record is inserted successfully we will empty the output file. However, if the program appends due to any reason the output file will have the last successfully inserted and committed record.
  • At the beginning of the program include a logic to check if the output file is empty or non-empty. If the file is empty, then the start will be considered as a fresh start and if the file is non-empty then we will consider it as a program restart after an append.
  • In case of restart, the output file already has the last inserted/committed record. We need to simply read the input file in loop and compare its key with the output file. Once the key matches, we need to start processing the records and insert them in the database.
Published on 30-Nov-2020 13:40:42