4.3 Commit, Rollback, Savepoint
A transaction can end with a commit or rollback command. Commit finalizes the changes made so far, and rollback cancels them all. Committed changes cannot be undone. Rolling back cancels all of the transaction's work. Until it ends one way or the other, the transaction is in the Active state.
A rollback can be issued explicitly by the user, but the database can also roll back a transaction by itself. When a situation arises in which it cannot proceed, such as a constraint violation or a deadlock (Chapter 7), the database rolls the transaction back and returns an error to the client. When the server terminates abnormally and restarts, the transactions that were in progress are all rolled back as well.
Consider how a rollback works. Suppose user A's balance has already been changed from 500,000 won to 400,000 won, and a rollback request arrives before user B's balance is changed. The database must have stored user A's original value, 500,000 won, somewhere. That is, before changing a value, it must record the previous value separately and be able to refer to it at any time until the transaction ends. A record that holds the value before the change is called an Undo record. When rolling back, the database uses this Undo record to restore the changed value to its pre-change value.
A savepoint is a feature that leaves a mark midway through a transaction so that only the changes after it can be cancelled. If the eighth step of a ten-step order process fails and you want to retry only the eighth step instead of cancelling everything, you can place a savepoint after the seventh step and roll back only up to it.
BEGIN;
UPDATE account SET balance = balance - 100000 WHERE id = 'hong';
SAVEPOINT after_withdraw;
UPDATE account SET balance = balance + 100000 WHERE id = 'kim'; -- assume this fails
ROLLBACK TO SAVEPOINT after_withdraw; -- cancel only the deposit
-- retry the deposit to another account, or ROLLBACK everything
Flow of rolling back to a savepoint
flowchart TB
B["BEGIN"] --> U1["UPDATE hong<br/>Withdraw −100,000 won"]
U1 --> SP["SAVEPOINT after_withdraw"]
SP --> U2["UPDATE kim<br/>Deposit +100,000 won"]
U2 -->|"Fails"| RB["ROLLBACK TO SAVEPOINT<br/>after_withdraw"]
RB -->|"Only the deposit is cancelled<br/>Withdrawal remains"| CH["Choose the next action"]
CH -->|"Deposit to another account"| U3["UPDATE other account<br/>Deposit +100,000 won"]
U3 --> C["COMMIT<br/>Withdrawal and new deposit are finalized together"]
CH -->|"Give up"| RA["ROLLBACK<br/>Everything including the withdrawal is cancelled"]
The diagram draws the SQL example above in execution order. BEGIN starts the transaction, the withdrawal is made from the hong account, and SAVEPOINT after_withdraw leaves a mark at this point. Next, assume the UPDATE that deposits into the kim account fails.
ROLLBACK TO SAVEPOINT after_withdraw cancels only the changes after the savepoint, that is, the deposit attempt. The withdrawal executed before the savepoint remains, and the transaction has not ended and is still in progress. From this state there are two paths. If you retry the deposit to another account and it succeeds, a single COMMIT finalizes the withdrawal and the new deposit together. If you give up on the deposit, ROLLBACK cancels the whole transaction, including the withdrawal. Either way, the transaction ends with one COMMIT or ROLLBACK.
A savepoint does not split a transaction into smaller ones. There is still one commit, and from outside the whole remains one piece of work. It simply adds a few more "points to return to" in the middle.
NOTE What about a transaction spanning two databases?
If Hong Gildong's account is in Bank A's database and Kim Cheolsu's account is in Bank B's database, a single
COMMITcannot tie the two together. Handling this problem is the job of distributed transactions, and the classic solution is Two-Phase Commit (2PC). A coordinator first asks all participants "are you ready to commit?" (phase 1), and when everyone answers that they are ready, it then instructs them to commit (phase 2). If even one participant is not ready, everyone rolls back. 2PC has the weakness that if the coordinator stops midway, participants are left waiting while holding locks, so recent distributed systems also use other approaches such as compensating transactions (Saga). Later chapters cover this in more detail.