A full ROLLBACK undoes every uncommitted change and ends the unit of recovery. Sometimes you only want to undo the last attempt inside a longer transaction. DB2 for z/OS savepoints mark that inner point: set a savepoint, try a step, and if it fails issue ROLLBACK TO SAVEPOINT without throwing away earlier work in the same unit of recovery.
The SAVEPOINT statement sets a named mark inside the current unit of recovery. Later, ROLLBACK TO SAVEPOINT undoes relational database changes made after that mark. The unit of recovery does not end. You can continue INSERT/UPDATE work and eventually COMMIT (keep everything that remains) or ROLLBACK (undo the whole UR).
Classic use: book a flight, then a hotel. If the hotel fails, roll back only to the savepoint before the hotel attempt, try the next date, and COMMIT when both succeed. Up to three dates without losing a successful earlier step you still want — or without committing a half-booking.
123456SAVEPOINT START_AGAIN UNIQUE ON ROLLBACK RETAIN CURSORS; SAVEPOINT A ON ROLLBACK RETAIN CURSORS; SAVEPOINT B UNIQUE ON ROLLBACK RETAIN CURSORS ON ROLLBACK RETAIN LOCKS;
| Clause | Meaning |
|---|---|
| savepoint-name | Name of the mark. Must not begin with SYS. |
| UNIQUE | This name cannot be reused in the same unit of recovery. |
| ON ROLLBACK RETAIN CURSORS | Cursors opened after the savepoint are not tracked for close on rollback to it (they may still be unusable). |
| ON ROLLBACK RETAIN LOCKS | Locks taken after the savepoint are not released when you roll back to it. |
ON ROLLBACK RETAIN CURSORS is required in the z/OS syntax diagram. It means Db2 does not track those later cursors for automatic close when you roll back to this savepoint. The cursors can remain open, but if rollback moved the row they were on, a later positioned UPDATE or DELETE can fail. Plan to CLOSE and OPEN again after a rollback to savepoint if the cursor was active across the undone work.
ON ROLLBACK RETAIN LOCKS is optional. If you specify it, locks taken after the savepoint stay when you roll back to it. That can surprise concurrency: you undid the data change but still hold the lock. Omit RETAIN LOCKS when you want lock release to follow the undone work as closely as the product allows.
UNIQUE means this name is taken for the rest of the UR. A second SAVEPOINT B UNIQUE fails. Without UNIQUE you may issue SAVEPOINT A again. Reusing the name destroys only that savepoint. It is not the same as RELEASE SAVEPOINT A, which also drops every savepoint set after A.
12345SAVEPOINT A ON ROLLBACK RETAIN CURSORS; -- work ... SAVEPOINT B UNIQUE ON ROLLBACK RETAIN CURSORS; -- work ... SAVEPOINT A ON ROLLBACK RETAIN CURSORS; -- reuse: old A is gone, B remains
On Db2 for z/OS you can set any number of savepoints in one unit of recovery. They form a sequence, not a single-slot object. If you set A, then B, then C:
That is the nesting model to learn. Older Db2 LUW documentation said “savepoints cannot be nested” in a different product sense. Do not copy that restriction onto z/OS application design.
Name scope across CALL: if the application sets savepoint S and a stored procedure also sets S, ROLLBACK TO SAVEPOINT S inside the procedure uses the procedure’s more recent S. Design unique names (or UNIQUE) when caller and callee both use savepoints.
You cannot use savepoints in global transactions, triggers, user-defined functions, or stored procedures nested inside triggers or UDFs.
123456789SAVEPOINT START_AGAIN UNIQUE ON ROLLBACK RETAIN CURSORS; -- attempt 1: air + hotel for date 1 -- if hotel fails: ROLLBACK TO SAVEPOINT START_AGAIN; -- attempt 2: air + hotel for date 2 -- if both ok: COMMIT;
TO SAVEPOINT says: do not end the UR; undo only after the named (or last) savepoint. Savepoints set after the target are released. The target remains.
A ROLLBACK with no TO SAVEPOINT is a full rollback: the UR ends, all savepoints in it are released, and (when Db2 is the only recoverable resource) the unit of work ends too.
In IMS and CICS, SQL ROLLBACK TO SAVEPOINT undoes Db2 changes only. VSAM, DL/I, MQ, and CICS resources in the same transaction are not rolled back by that SQL statement. Use EXEC CICS SYNCPOINT ROLLBACK or the IMS rollback call when the whole transaction must reverse.
12RELEASE SAVEPOINT START_AGAIN; RELEASE SAVEPOINT A;
RELEASE SAVEPOINT name drops that savepoint and every savepoint established after it. If you no longer need the mark before COMMIT, release it. Savepoints disappear anyway at the end of the unit of work, but holding them has a practical cost: you cannot use three-part names to reach a remote location while any savepoint is active. Distributed SQL in the same UR needs a clean savepoint list first.
If a utility, SQL statement, or command issues intermediate COMMIT statements while a savepoint exists, the savepoint is implicitly released. Do not assume a savepoint survives someone else’s commit.
Savepoints do not make a bad lock design safe. They only shrink how much SQL you undo when a business rule fails.
Imagine building with blocks. COMMIT is gluing the whole tower to the table. ROLLBACK is knocking the whole unglued tower down. A savepoint is a colored flag you stick in the tower after the first storey. If the second storey falls, you take blocks off down to the flag and try again. The first storey is still there. Taking the flag out (RELEASE SAVEPOINT) means you cannot go back to that exact height anymore. UNIQUE means you promised not to plant another flag with the same color.
1. What does SAVEPOINT do?
2. What does UNIQUE on SAVEPOINT mean?
3. ROLLBACK TO SAVEPOINT without a name rolls back to:
4. ON ROLLBACK RETAIN LOCKS means:
5. Where are savepoints not allowed?