Mainframe Path Start learning free
Core7 min readLesson 4 of 4

Commits, locking and why batch jobs collide

DB2 protects data by locking it. Locks are held until you commit. A batch job that updates a million rows without committing holds a million locks, blocks everything else, and eventually falls over. This is the most common DB2 performance problem in batch.

The unit of work

Everything between one commit and the next is a unit of work. DB2 guarantees it happens completely or not at all. COMMIT makes it permanent and releases locks; ROLLBACK undoes it.

Isolation levels

LevelNameBehaviour
URUncommitted readReads data others have not committed. Fastest, and may be wrong. Fine for reporting on stable data.
CSCursor stabilityThe default. Holds a lock only on the row being processed.
RSRead stabilityRows you have read stay stable; new matching rows can still appear.
RRRepeatable readThe strictest. Holds locks on everything read. Highest integrity, worst concurrency.

Commit frequency in batch

A batch program updating many rows should commit periodically — every few thousand rows is typical. The trade-off is real in both directions:

Choosing a commit interval
Commit rarely
Fewer commits, less overheadLocks held a long timeOther work is blockedRestart replays a lot of work
Commit often
Locks released quicklyGood concurrencyMore commit overheadRestart loses little

Restartability is the other half

Committing means that a failure halfway through leaves half the work done. A restartable batch program therefore keeps a restart record: it stores the key it last processed in a control table, commits that in the same unit of work as the data, and on restart reads it back and resumes from there.

The commit-and-checkpoint pattern
       2000-PROCESS-ROW.
           PERFORM 2100-UPDATE-ACCOUNT
           ADD 1 TO WS-SINCE-COMMIT
           IF WS-SINCE-COMMIT >= WS-COMMIT-FREQ
               PERFORM 2900-CHECKPOINT
           END-IF.

       2900-CHECKPOINT.
           EXEC SQL
               UPDATE PAYPROD.RESTART_CTL
                  SET LAST_KEY = :WS-CURRENT-KEY,
                      ROWS_DONE = :WS-COUNT
                WHERE PGM_NAME = :WS-PGM-NAME
           END-EXEC
           EXEC SQL COMMIT END-EXEC
           MOVE ZERO TO WS-SINCE-COMMIT.

Recognising a locking problem

Common mistakes

No commits in a long-running update

Locks accumulate, concurrency collapses, and the job may exceed limits and abend after hours of work.

Committing without a restart record

You cannot rerun from the top (work is already done) and cannot resume (you do not know where you were).

Using RR because it sounds safer

Repeatable read holds far more locks. It is occasionally correct and frequently an accident.

Committing inside a cursor without WITH HOLD

A commit closes cursors unless they are declared WITH HOLD, so the next FETCH fails with -501.

What you will see at work

Key terms

Check your understanding.
Take this lesson's quiz and save your progress. Free.

Take the lesson quiz
← SQLCODEs and what to do about themBack to DB2 basics