SQL: without an explicit transaction, every statement is its own

Autocommit means a failure halfway through a sequence of statements leaves the earlier ones applied. Wrapping them in BEGIN/COMMIT is what makes them one unit.

Code
-- autocommit: two independent transactions
UPDATE account SET bal = bal - 100 WHERE id = 1;
UPDATE account SET bal = bal + 100 WHERE id = 2;   -- if this fails, money is gone

-- one unit:
BEGIN;
  UPDATE account SET bal = bal - 100 WHERE id = 1;
  UPDATE account SET bal = bal + 100 WHERE id = 2;
COMMIT;                       -- or ROLLBACK, and neither happened
Output
-- After an error inside a PostgreSQL transaction, every later statement fails:
--   ERROR: current transaction is aborted, commands ignored until end of
--          transaction block
-- SAVEPOINT lets you recover part of the way rather than losing all of it.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-25