MainframeMaster

COBOL Tutorial

RETRY - Implementing Retry Logic

Progress0 of 0 lessons

Overview

Retry is a pattern you implement to handle transient failures safely. Keep attempts bounded and add backoff.

🔁 Analogy

Like calling a busy office: you try again a few times, waiting longer between calls, and eventually leave a message if no answer.

Simple Retry Loop

Example (File Open)

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
WORKING-STORAGE SECTION. 01 ATTEMPTS PIC 9 VALUE 0. 01 MAX-ATTEMPTS PIC 9 VALUE 3. 01 OPEN-STATUS PIC XX. ... PROCEDURE DIVISION. OPEN-TRY. ADD 1 TO ATTEMPTS OPEN INPUT MYFILE MOVE FILE-STATUS OF MYFILE TO OPEN-STATUS IF OPEN-STATUS NOT = '00' IF ATTEMPTS < MAX-ATTEMPTS PERFORM BACKOFF GO TO OPEN-TRY ELSE DISPLAY 'Open failed after retries, RC=8' STOP RUN END-IF END-IF. ... BACKOFF. *> Implement wait (site-specific) DISPLAY 'Waiting before retry # ' ATTEMPTS.

Adjust for your compiler; use proper sleep APIs where available.

Exponential Backoff (Concept)

  • Attempt 1: wait 1s
  • Attempt 2: wait 2s
  • Attempt 3: wait 4s

Best Practices

  • Bound attempts and use backoff
  • Log each attempt and reason
  • Rollback/close resources between attempts
  • Make thresholds configurable
  • Coordinate with JCL restart/automation

Common Mistakes

MistakeProblemFix
Infinite loopsHangs and resource wasteAdd max attempts
No delayHammering a busy resourceUse backoff
Retrying non-transient errorsWastes timeCheck error codes; fail fast

Quick Reference

ConceptTipExample
Max attemptsKeep small (3-5)MAX-ATTEMPTS = 3
BackoffWait longer each time1s, 2s, 4s
FailoverReturn code, messageRC=8

Test Your Knowledge

1. Is RETRY a standard COBOL verb?

  • Yes
  • No

2. What is a good strategy for transient errors?

  • Retry immediately forever
  • Exponential backoff with max attempts
  • Never retry
  • Random jumps

3. Where do you store attempt counters?

  • WORKING-STORAGE
  • FILE SECTION
  • REPORT SECTION
  • LINKAGE only

4. What should happen after max attempts?

  • Loop forever
  • Graceful fail or escalate with return code
  • Restart IPL
  • Ignore error

5. Which JCL concept relates to post-failure recovery?

  • EXEC PGM
  • STEP RESTART/COND
  • DD DUMMY
  • IEBGENER

Frequently Asked Questions