G27
Self-checkedTier 1 7 min readdbms.txn-acid

Transactions and ACID

What a transaction is (a unit of work made of reads, writes, commit and abort), its states, the four ACID properties and which part of the system guarantees each one, and the anomalies that appear when transactions run without control (lost update, dirty read, non-repeatable read, inconsistent summary, crash in the middle of a transfer), each shown as a schedule executed on real numbers; the SQL isolation levels are described as quoted from the standard.

Why it exists

A bank transfer is two writes: subtract from one account, add to another. If the machine stops between the writes, or another program reads the accounts in between, the data are wrong. A transaction groups operations into one unit that the database system promises to run all or nothing and as if alone. This page defines the unit and the four promises (ACID); the next pages study how the promises are kept for several transactions at once.

Prerequisites

None. Knowing what a table and a record are is enough (relational model if you want the vocabulary).

Precise definitions

A transaction is a sequence of operations on database items that the system treats as one logical unit. In the theory used for GATE only these operations are visible:

  • ri(X)r_i(X): transaction TiT_i reads item XX (a record, a block, a value: the unit is called an item);
  • wi(X)w_i(X): TiT_i writes item XX;
  • cic_i: TiT_i commits (its effects become permanent);
  • aia_i: TiT_i aborts (its effects are undone).

Each transaction ends with exactly one of commit or abort. A schedule (or history) of several transactions is an interleaving of their operations that keeps the order inside each transaction.

States of a transaction (as in the textbooks; the names differ slightly): active (executing), partially committed (the last operation has run, the commit is not yet safe on disk), committed, failed (it cannot continue) and aborted (rolled back, the database is as before it started; the transaction can then be restarted or killed).

fromtobecause
activepartially committedthe last operation has been executed
activefailedan error or a deadlock victim
partially committedcommittedthe system has made the effects durable (log written)
partially committedfailedthe commit could not be completed
failedabortedrollback finished

The ACID properties:

letterpropertymeaningwho guarantees it
AAtomicityall the operations of the transaction take effect or none doesrecovery manager (undo using a log)
CConsistencya transaction that starts in a consistent state (all integrity constraints hold) ends in a consistent statethe programmer who writes the transaction and the integrity constraints; the system checks the constraints
IIsolationeach transaction behaves as if it ran alone; concurrent execution is equivalent to a serial oneconcurrency control (locking, timestamps)
DDurabilityafter commit the effects survive any failurerecovery manager (redo using a log, forced to disk before the commit)

Consistency is the only property that is not provided by a mechanism alone: if every transaction is correct when it runs alone, then atomicity and isolation together keep the database consistent.

How it works

The five things that go wrong

Each animation below runs the transactions on real numbers. A=100A = 100 unless stated.

Lost update. Two transactions read the same value and both write a result computed from it.

Lost update: two transactions read the same balance: start

Computed by a script, not typed
time123456
T1
T2
database

Step 0

A = 100. T1 withdraws 30, T2 deposits 50. Both read before either writes.

0 / 6
computed by script: schedules are executed on real values, conflicts and classes follow the definitions, locks are granted by a real lock manager. python3 scripts/gen-db5-traces.py

The final value is 150150; every serial execution gives 120120. The update of T1T_1 is lost although both transactions committed.

Dirty read (write-read conflict). A transaction reads a value written by a transaction that has not committed and later aborts.

Dirty read: reading what a transaction later undoes: start

Computed by a script, not typed
time12345
T1
T2
database

Step 0

A = 100. T1 withdraws 30 but will abort.

0 / 5
computed by script: schedules are executed on real values, conflicts and classes follow the definitions, locks are granted by a real lock manager. python3 scripts/gen-db5-traces.py

T2T_2 has used the value 7070, which was removed by the abort.

Non-repeatable read (read-write conflict). A transaction reads the same item twice and gets two values because another transaction committed a change in between.

Non-repeatable read: start

Computed by a script, not typed
time123456
T1
T2
database

Step 0

A = 100. T1 reads A twice; T2 changes A between the reads.

0 / 6
computed by script: schedules are executed on real values, conflicts and classes follow the definitions, locks are granted by a real lock manager. python3 scripts/gen-db5-traces.py

Inconsistent summary (also called incorrect summary). A transaction that reads several items sees some of them before and some after another transaction's updates.

Inconsistent summary: reading in the middle of a transfer: start

Computed by a script, not typed
time12345678
T1
T2
database

Step 0

A = 100, B = 50, total 150. T1 moves 40 from A to B; T2 adds A and B.

0 / 8
computed by script: schedules are executed on real values, conflicts and classes follow the definitions, locks are granted by a real lock manager. python3 scripts/gen-db5-traces.py

T2T_2 computes 110110 although the total is 150150 before and after the transfer.

Failure in the middle (violates atomicity):

Why atomicity needs recovery: a crash in the middle of a transfer: start

Computed by a script, not typed
time123
T1
database

Step 0

A = 100, B = 50. T1 transfers 40 from A to B and the machine crashes after the first write.

0 / 3
computed by script: schedules are executed on real values, conflicts and classes follow the definitions, locks are granted by a real lock manager. python3 scripts/gen-db5-traces.py

The textbooks add the phantom problem: a transaction runs a query with a condition twice and a second run returns an extra row inserted meanwhile by another transaction (the item-based model above does not show it because it has no insertions).

Isolation levels in SQL (quoted)

The SQL standard defines four levels by the anomalies they allow. The following table is quoted from the standard as the textbooks present it and needs verification against the edition you study; implementations differ.

leveldirty readnon-repeatable readphantom
READ UNCOMMITTEDpossiblepossiblepossible
READ COMMITTEDpreventedpossiblepossible
REPEATABLE READpreventedpreventedpossible
SERIALIZABLEpreventedpreventedprevented

SERIALIZABLE means that the outcome equals that of some serial execution; the next pages make this precise.

Worked examples

EX 1Name the property

Easy

(a) A transfer leaves AA debited and BB not credited after a crash: atomicity. (b) After the system confirmed a payment, a power failure erases it: durability. (c) Two transactions interfere so that the result matches no serial order: isolation. (d) The total of all balances must stay constant, and each transfer keeps it constant: consistency.

EX 2Which anomaly

Medium

r1(A) w1(A) r2(A) a1r_1(A)\ w_1(A)\ r_2(A)\ a_1: T2T_2 reads a value of an uncommitted transaction that then aborts: a dirty read. r1(A) r2(A) w1(A) w2(A)r_1(A)\ r_2(A)\ w_1(A)\ w_2(A): both overwrite from the same old value: a lost update. r1(A) w2(A) c2 r1(A)r_1(A)\ w_2(A)\ c_2\ r_1(A): non-repeatable read.

EX 3Numbers of the lost update

Medium

A=100A = 100, T1T_1: A:=A−30A := A - 30, T2T_2: A:=A+50A := A + 50. Serial: 100−30+50=120100 - 30 + 50 = 120 in either order. The interleaving r1 r2 w1 w2r_1\ r_2\ w_1\ w_2 writes 7070 and then 150150: result 150150. With r1 r2 w2 w1r_1\ r_2\ w_2\ w_1 the result would be 7070. Both are wrong, in different ways.

EX 4Typical questions

GATE level

(a) "Which ACID property is ensured by the recovery manager?" Atomicity and durability. (b) "Which by the concurrency control manager?" Isolation. (c) "A transaction that has issued its last operation but whose log is not yet on disk is in which state?" Partially committed. (d) "After rollback a transaction can be restarted": true for aborted transactions caused by a hardware or software error that is not an internal logical error; it may be killed.

Cheat box

Cheat box

  • Transaction: unit of work, ends with commit or abort. Operations ri(X)r_i(X), wi(X)w_i(X), cic_i, aia_i.
  • ACID: atomicity and durability (recovery manager, log); isolation (concurrency control); consistency (programmer plus constraints).
  • States: active, partially committed, committed, failed, aborted.
  • Anomalies: lost update (WW), dirty read (WR), non-repeatable read (RW), inconsistent summary, phantom.
  • Serializable is the strongest isolation level; READ UNCOMMITTED the weakest.

Traps and GATE patterns

Connections

Isolation is made precise by serializability and enforced by locking; the interaction of aborts with reads is the subject of recoverability; atomicity and durability are implemented by log-based recovery.

PYQs

6 previous year questions from your book. I re-solved each one myself (1 by running or simulating, 5 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2005 · MCQ · 2 marksQ36 · page 411 of the book
A company maintains records of sales made by its
salespersons and pays them commission based on each
individual's total sales made in a year. This data is
maintained in a table with following schema:

salesinfo = (salespersonid, totalsales, commission)

In a certain year, due to better business results, the
company decides to further reward its salespersons by
enhancing the commission paid to them as per the
following formula:

If commission < = 50000, enhance it by 2%
If 50000 < commission < = 100000, enhance it by 4%
If commission > 100000, enhance it by 6%

The IT staff has written three different SQL scripts to
calculate enhancement for each slab, each of these scripts
is to run as a separate transaction as follows:

DATABASE MANAGEMENT  SYSTEM
  T1
Update salesinfo Set commission
= commission * 1.02 Where
commission < = 50000;
 T2
Update salesinfo Set commission
= commission * 1.04 Where
commission > 50000 and
commission is < = 100000;
 T3
Update salesinfo Set commission
= commission * 1.06 Where
commission > 100000;

Which of the following options of running these
transactions will update the commission of all
salespersons correctly
  • (a)Execute T1 followed by T2 followed by T3
  • (b)Execute T2, followed by T3; T1 running concurrently throughout
  • (c)Execute T3 followed by T2; T1 running concurrently throughout
  • (d)Execute T3 followed by T2 followed by T1
GATE 2025 · Set 2 · MSQQ3 · page 433 of the book
An audit of a banking transactions system has found that
on an earlier occasion, two joint holders of account A
attempted simultaneous transfers of Rs. 10000 each from
account A to account B. Both transactions read the same
value, Rs. 11000, as the initial balance in A and were
allowed to go through. B was credited Rs. 10000 twice.
A was debited only once and ended up with a balance of
Rs. 1000.

Which of the following properties is/are certain to have
been violated by the system?
  • (a)Atomicity
  • (b)Consistency
  • (c)Isolation
  • (d)Durability
GATE 2024 · Set 2 · MSQ · 2 marksQ6 · page 433 of the book
Once the DBMS informs the user that a transaction has
been successfully completed, its effect should persist even
if the system crashes before all its changes are reflected
on disk. This property is called
  • (a)Durability
  • (b)Atomicity
  • (c)Consistency
  • (d)Isolation DATABASE MANAGEMENT SYSTEM
GATE 2016 · Set 1 · MCQ · 1 markQ17 · page 435 of the book
Which one of the following is NOT a part of the ACID
properties of database transactions?
  • (a)Atomicity
  • (b)Consistency
  • (c)Isolation
  • (d)Deadlock-freedom DATABASE MANAGEMENT SYSTEM
GATE 2015 · Set 2 · MCQ · 1 markQ22 · page 436 of the book
Consider the following transaction involving two bank
accounts x and y.

read (x) ;

x : =x - 50;

write (x) ;

read (y);

y : = y + 50;

write (y)

The constraint that the sum of the accounts x and y should
remain constant is that of

DATABASE MANAGEMENT  SYSTEM
  • (a)Atomicity
  • (b)Consistency
  • (c)Isolation
  • (d)Durability
GATE 2005 · MCQQ35 · page 440 of the book
Amongst the ACID properties of a transaction, the
'Durability' property requires that the changes made to
the database by a successful transaction persist
  • (a)Except in case of an Operating System crash
  • (b)Except in case of a Disk crash
  • (c)Except in case of a power failure
  • (d)Always, even if there is a failure of any kind of failure

Self-check

Question 1

Which ACID property does the lost update violate?

Show answer

Isolation: the result is not that of any serial execution.

Question 2

A=100A = 100 and B=50B = 50. T1T_1 moves 4040 from AA to BB and crashes after writing AA. What does a correct system do after the crash?

Show answer

It undoes the write of AA (atomicity), restoring A=100A = 100, B=50B = 50; the total stays 150150.

Question 3

Which anomaly is it if T2T_2 reads AA twice and sees two values?

Show answer

A non-repeatable read, caused by a committed write of another transaction between the reads.

Revision card

One-screen revision

  • Transaction = all-or-nothing unit; ends with commit or abort.
  • A, D: recovery manager; I: concurrency control; C: programmer and constraints.
  • Anomalies: lost update, dirty read, non-repeatable read, inconsistent summary, phantom.
  • States: active, partially committed, committed, failed, aborted.

References

  • ·Silberschatz, Korth, Sudarshan, Database System Concepts (7th edition), Chapter 17 (Transactions)
  • ·Elmasri, Navathe, Fundamentals of Database Systems (7th edition), Chapter 20 (Introduction to Transaction Processing Concepts and Theory)

Verification

Every anomaly animation executes the transactions on real numbers in scripts/db_txn.py (execute), including the restoration of before-images on abort; the numbers on the page (150 instead of 120, 110 instead of 150) are those outputs. The final values of the serial executions were asserted in scripts/gen-db5-traces.py. The five transaction states and the table of SQL isolation levels are QUOTED from the textbooks and the SQL standard and need verification against the editions used; real database systems differ from the standard table (their levels are implemented with locks or multiversion snapshots), and SQLite, used as the oracle elsewhere in this subject, only offers serializable behaviour. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and re-derived against the textbook definitions and the sample database. No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never