DB2 SAVEPOINT in a COBOL Batch: Limit One Bad Unit of Work Without Hiding the Failure
A long-running COBOL batch update may encounter one invalid business record after hundreds of valid changes. This article explains when a DB2 `SAVEPOINT` can contain that one unit of work, what evidence is needed before continuing, and why it is not a substitute for a restart design.
A long-running COBOL batch update may encounter one invalid business record after hundreds of valid changes. This article explains when a DB2 SAVEPOINT can contain that one unit of work, what evidence is needed before continuing, and why it is not a substitute for a restart design.
The example is fictional: an account-adjustment feed is validated and applied to two DB2 tables. Names, rules, and error handling must be adapted to local DB2, application, audit, and change-control standards.
The operational problem
Consider a job that reads adjustment records and applies each one to an account balance and an audit table. Committing only at end of job gives an all-or-nothing outcome, but an input error near the end can make recovery expensive. Committing every record reduces rollback scope, but may create unnecessary log activity and makes a later job restart depend on a precise processed-key control.
A savepoint creates a named point inside the current DB2 unit of work. If a *known, recoverable business failure* occurs after that point, the program can issue ROLLBACK TO SAVEPOINT and undo the SQL changes made after it. It can then record a controlled reject and decide whether to continue.
That last sentence contains important limits:
- A savepoint affects DB2 work in the current unit of work; it does not reverse a sequential input read, a report line already written, an MQ action, or another external side effect.
- It cannot turn a deadlock, storage problem, or unavailable resource into a harmless data reject. Those conditions need their own recovery decision.
- It does not persist across
COMMIT. A later abend still requires the normal restart controls: commit boundary, input position or key, output disposition, and rerun rules.
Use a savepoint to make one deliberate database decision easier to reverse, not to make a risky job appear resilient.
Choose the boundary before coding
The useful boundary is usually one business transaction, not an arbitrary number of SQL statements. In the fictional feed, one adjustment has an account number, an adjustment ID, amount, and effective date. The unit is complete only when both of these are true:
1. The account row has been updated exactly once for the adjustment ID.
2. One matching audit row has been inserted.
A savepoint placed before those statements can undo a partially completed pair. A savepoint placed *after* the account update cannot protect that update if the audit insert fails.
| Outcome after the savepoint | Example interpretation | Safe default |
|---|---|---|
| Input fails local validation | Non-numeric amount or forbidden action | Do not issue SQL; write a controlled reject |
| Expected business conflict | Adjustment ID already applied, under an approved idempotency rule | Roll back this unit if needed; record the reason; continue only if the rule permits |
| SQL error with uncertain scope | Timeout, deadlock, or unexpected constraint issue | Stop or follow the approved recovery runbook; preserve evidence |
| Both updates succeed | One complete adjustment | Keep it in the current commit group |
The exact SQLCODE classification is application-specific. Do not blindly treat every negative code as a per-record reject. In particular, lock and resource conditions may have already changed the transactional state or indicate that a retry could duplicate external work.
Worked COBOL pattern
This compact fragment shows the shape of the decision. It assumes input validation has already confirmed the basic record layout. It deliberately leaves out site-specific SQLCA logging, copybooks, cursor handling, and reject-file definitions.
PERFORM UNTIL END-OF-FILE = 'Y'
READ ADJUSTMENT-IN
AT END
MOVE 'Y' TO END-OF-FILE
NOT AT END
PERFORM VALIDATE-ADJUSTMENT
IF ADJUSTMENT-VALID
EXEC SQL
SAVEPOINT ADJ_UNIT
END-EXEC
PERFORM UPDATE-ACCOUNT
IF SQLCODE = 0
PERFORM INSERT-AUDIT
END-IF
EVALUATE SQLCODE
WHEN 0
ADD 1 TO COMMIT-COUNT
WHEN -803
EXEC SQL
ROLLBACK TO SAVEPOINT ADJ_UNIT
END-EXEC
PERFORM WRITE-DUPLICATE-REJECT
WHEN OTHER
MOVE 'Y' TO STOP-REQUESTED
PERFORM CAPTURE-SQL-EVIDENCE
END-EVALUATE
IF COMMIT-COUNT >= COMMIT-LIMIT
AND STOP-REQUESTED NOT = 'Y'
EXEC SQL
COMMIT
END-EXEC
MOVE 0 TO COMMIT-COUNT
END-IF
ELSE
PERFORM WRITE-VALIDATION-REJECT
END-IF
END-READ
END-PERFORM
```
-803 is shown only as a readable example of a duplicate-key outcome. It is not automatically safe to continue. If the duplicate key is the audit row, it might mean a prior execution already applied the account update; if it is a different table or index, the meaning may be entirely different. The program needs a documented idempotency rule based on the adjustment ID and the target data state.
Notice another detail: the program must check the SQL result from ROLLBACK TO SAVEPOINT too. If rollback fails, the original diagnostic is no longer the whole story. Set an error return code and let normal recovery procedures take over rather than writing a reject and continuing as if the unit was cleanly undone.
A common refinement is to have each SQL paragraph save its own SQLCODE immediately. The final EVALUATE must act on the code from the statement that failed, not on a later SQL call that overwrote SQLCODE. For example, preserve UPDATE-SQLCODE before attempting the audit insert, then decide whether the failure occurred before or after any database change.
Commit groups and restart are separate controls
Suppose the job commits every 500 successfully applied adjustments. If adjustment 327 conflicts under a rule that allows rejection, a rollback to its savepoint removes only that adjustment’s work. Adjustments 1 through 326 remain pending until the group commits. If an abend occurs before the commit, DB2 rolls back the uncommitted group—including all previously successful adjustments in that group.
That behavior is correct, but it means restart logic must not assume that “processed from input” means “committed to DB2.” A supportable design records or derives a restart point only at a successful commit. The next execution must also avoid re-creating any external output produced for rolled-back records.
A plain-text flow makes the separation clear:
Read and validate record
|
Create SAVEPOINT for one adjustment
|
Update account + insert audit
|
success? ---- no --> expected, documented conflict?
| | yes
yes v
| ROLLBACK TO SAVEPOINT
v |
Count toward commit write controlled reject
|
Commit group reached? --> COMMIT and establish restart boundary
Do not put a COMMIT inside a reusable error paragraph simply to “clear” a failure. It changes the recovery boundary for unrelated records and may commit work that a caller expected to roll back.
Before allowing the job to continue
- Confirm the savepoint surrounds every DB2 update that belongs to one business unit and excludes unrelated prior work.
- Identify each SQL statement, target table, unique key, and trigger or stored-procedure side effect in that unit.
- Document which outcomes are approved record-level rejects and which require the step to stop.
- Capture the job ID, input record correlation key, SQLCODE, SQLSTATE if available, statement identity, and current commit-group count. Do not place sensitive record content in broadly visible spool.
- Verify that
ROLLBACK TO SAVEPOINTreturned success before writing a “rejected and continued” audit outcome. - Prove in test that a failure after the first update leaves neither the business update nor its audit row committed.
- Prove restart behavior both before and after a commit boundary, including reject output and any non-DB2 side effects.
- Review DB2 release, precompiler options, and local transaction standards; savepoint naming and permitted use can vary by environment.
Practical takeaway
A DB2 savepoint is valuable when it protects a clearly defined, all-or-nothing business unit inside a larger COBOL batch commit group. It earns that value only when the team distinguishes expected record exceptions from uncertain SQL failures, checks the rollback result, and retains a separate, tested restart design for the job as a whole.