Sub topic 10
Why Later Savepoints Become Invalid
This chapter assumes you can write basic Apex, run insert and update statements, use try/catch with DmlException, and have seen governor limits. You should also know how Database.setSavepoint() and Database.rollback() work.
The rule
A savepoint marks a position in the transaction's sequence of work. When you roll back to an earlier savepoint, Apex discards every change made after it. Any savepoint you set after that earlier one now points to work that no longer exists, so Apex invalidates it.
If you pass an invalidated savepoint to Database.rollback, Apex throws a runtime error. Catching the error does not make the savepoint usable again.
Savepoint sp1 = Database.setSavepoint();
insert new Account(Name = 'A');
Savepoint sp2 = Database.setSavepoint();
insert new Account(Name = 'B');
Database.rollback(sp1);
Database.rollback(sp2); // fails at runtime: sp2 was invalidated by rolling back to sp1The first rollback removes both accounts. The second line tries to return to a point that was inside the work that was just erased.
The order that works
Rolling back to a later savepoint first is safe. It undoes only the work after that savepoint, and the earlier savepoint stays valid.
Savepoint sp1 = Database.setSavepoint();
insert new Account(Name = 'A');
Savepoint sp2 = Database.setSavepoint();
insert new Account(Name = 'B');
Database.rollback(sp2); // removes B only
Database.rollback(sp1); // removes A; sp1 is still validThink of savepoints as a stack. You can pop back to any entry, but doing so discards every entry above it.
Common errors
- Reusing a savepoint after a deeper rollback. This happens when two methods each set a savepoint and a caller rolls back to its own earlier one. Any savepoint held by the inner code is then stale.
- Rolling back in a loop. If you keep a list of savepoints and roll back to each one in turn, go from newest to oldest. Going oldest first invalidates the rest.
- Assuming a catch block repairs the savepoint. The error means the savepoint is no longer valid. Set a new savepoint if you need another restore point.
Setting a fresh savepoint
After a rollback, you can set a new savepoint and carry on. The new savepoint is valid because it marks a position in the current, remaining work.
Savepoint sp1 = Database.setSavepoint();
insert new Account(Name = 'A');
Database.rollback(sp1);
Savepoint spNew = Database.setSavepoint(); // valid: set after the rollback
insert new Account(Name = 'C');
Database.rollback(spNew); // removes C onlyExercise
A method sets sp1, inserts an Account, sets sp2, inserts a Contact, then calls Database.rollback(sp1) and afterwards Database.rollback(sp2). What happens, and how would you reorder the calls so both rollbacks succeed?
Show answer
The second call fails at runtime because rolling back to sp1 invalidated sp2, which was set later. Roll back to sp2 first, which removes the Contact, then roll back to sp1, which removes the Account. Rolling back to sp1 alone also works if you do not need the intermediate step.