Transaction

2 min read Last updated Sat Jun 27 2026 08:46:52 GMT+0000 (Coordinated Universal Time)

A logical unit of work that performs one or more database operations (reads/writes). Ensures ACID properties. Consistency might be temporarily lost while a transaction is running. Multiple transactions can run in parallel.

Transaction States

StateDescriptionPossible Next States
ActiveInitial state. Executing.Partially Committed, Failed
Partially CommittedFinal operation done. Waiting to commit.Failed, Committed
FailedError occurred. Cannot proceed normally.Aborted
AbortedRolled back; database restored to previous state.Active (if retrying)
CommittedCompleted successfully.-

If the failure is temporary, can retry.

SQL Commands

BEGIN;                    -- start a transaction
-- ... operations ...
COMMIT;                   -- persist all changes
ROLLBACK;                 -- undo all changes since BEGIN
SAVEPOINT sp1;            -- set a named savepoint
ROLLBACK TO SAVEPOINT sp1;-- rollback to savepoint only

Concurrency Anomalies

Problems that arise when multiple transactions run concurrently without proper isolation.

  • Dirty Read
    Transaction reads uncommitted data written by another transaction. Data may later be rolled back.
  • Non-Repeatable Read
    Transaction reads the same row twice and gets different values because another transaction modified and committed that row in between.
  • Phantom Read
    Transaction re-executes a query and gets a different set of rows because another transaction inserted or deleted rows in between.

Isolation Levels

Higher isolation prevents more anomalies at the cost of reduced concurrency.

Isolation LevelDirty ReadNon-Repeatable ReadPhantom Read
Read Uncommittedpossiblepossiblepossible
Read Committedpreventedpossiblepossible
Repeatable Readpreventedpreventedpossible
Serializablepreventedpreventedprevented
Was this helpful?