When a step fails
After this: Say why an error leaves a transaction open, and end it with ROLLBACK.
On this page · 5 sections · 3 checks
In lesson 3 the program stopped halfway on its own. This time the database stops it. Ana asks to send Ben 120, but she has only 100, and a balance can never go below 0. The program happens to write the transfer the other way round, adding to Ben first and taking from Ana second, inside a transaction:
The run stops at statement 3 with CHECK constraint failed: balance >= 0. Ana's balance would be 100 − 120 = −20, which breaks the rule. The list shows that statements 1 and 2 ran: BEGIN, and Ben's UPDATE with "1 row changed".
What the error undid, and what it did not
Lesson 2 said that one statement is all-or-nothing. That still holds: the failed UPDATE changed nothing, so Ana still has 100. But it is the only thing the error undid. Look at the state step by step:
BEGINopened a transaction.- Ben's UPDATE worked. Inside the transaction, Ben has 170.
- Ana's UPDATE failed and was undone, on its own.
- The transaction is still open, and Ben's 170 is still inside it, waiting for a COMMIT or a ROLLBACK.
The Run button stops at the first error, as a program does when it does not catch the error, so the block above cannot show the next step. A program that catches the error can keep going on the same connection. If it reads the table, it sees this:
BEGIN;UPDATE accounts SET balance = balance + 120 WHERE id = 2;UPDATE accounts SET balance = balance - 120 WHERE id = 1;SELECT * FROM accounts;| id | owner | balance |
|---|---|---|
| 1 | Ana | 100 |
| 2 | Ben | 170 |
Two ways to end it
The program must now end the open transaction, and it has two choices.
ROLLBACK undoes every change since BEGIN, including Ben's:
BEGIN;UPDATE accounts SET balance = balance + 120 WHERE id = 2;UPDATE accounts SET balance = balance - 120 WHERE id = 1;ROLLBACK;SELECT * FROM accounts;| id | owner | balance |
|---|---|---|
| 1 | Ana | 100 |
| 2 | Ben | 50 |
Ana 100, Ben 50, total 150. The transfer did not happen, which is right: Ana did not have the money.
COMMIT makes every change in the transaction final, and the only change left in it is Ben's +120:
BEGIN;UPDATE accounts SET balance = balance + 120 WHERE id = 2;UPDATE accounts SET balance = balance - 120 WHERE id = 1;COMMIT;SELECT * FROM accounts;| id | owner | balance |
|---|---|---|
| 1 | Ana | 100 |
| 2 | Ben | 170 |
Ana 100, Ben 170: a total of 270. The bank has created 120 out of nothing. The database did not object, because each change it was asked to keep obeyed the rules. It cannot know that Ben's +120 made sense only together with Ana's −120.
You can run that ending here. The failed UPDATE changed nothing, so leaving it out reaches exactly the same end:
The rule
An error inside a transaction does not end the transaction, and does not undo the earlier statements. Your code must do that, every time:
- Send
BEGIN. - Send each statement, and watch for an error.
- If every statement worked, send
COMMIT. - If any statement failed, send
ROLLBACK, and do not send anything else first.
Never answer an error with COMMIT. In Python and Java this becomes a try block with ROLLBACK in the part that handles the error; the tutorial "Using a database from code" writes it out.
Check yourself
Ana has 100 and Ben 50. The third statement fails with CHECK constraint failed: balance >= 0. Right after the error, before the program sends anything else, what is the state?
BEGIN;UPDATE accounts SET balance = balance + 120 WHERE id = 2;UPDATE accounts SET balance = balance - 120 WHERE id = 1;The program catches the error but then sends COMMIT instead of ROLLBACK. What does the SELECT show?
BEGIN;UPDATE accounts SET balance = balance + 120 WHERE id = 2;UPDATE accounts SET balance = balance - 120 WHERE id = 1;COMMIT;SELECT * FROM accounts;The transfer failed halfway. Complete the missing line so that the SELECT shows Ana 100 and Ben 50 again.
BEGIN;UPDATE accounts SET balance = balance + 120 WHERE id = 2;UPDATE accounts SET balance = balance - 120 WHERE id = 1;-- the line above failed: CHECK constraint failed: balance >= 0_________ -- ← undo everything since BEGINSELECT * FROM accounts;Honesty notes
- Say that an error inside a transaction undoes only the failed statement, and leaves the transaction open with the earlier changes.
- Say what COMMIT after the error keeps: Ben's +120 without Ana's −120, a bank total of 270 instead of 150.
- End a transaction after any error with ROLLBACK, and send COMMIT only when every statement worked.
- Say that PostgreSQL refuses further statements after an error until ROLLBACK, where SQLite and MySQL keep going.