☰ Chapters
Week 9 · Transaction Management
CT004-3.5-3 Advanced Database Systems · Week 9

Transaction Management

ACID, concurrency problems, serializability, locking and 2PL, and database recovery with logs and checkpoints.

Contents
    Learning objectives

    Transactions

    Definition

    A transaction is an action, or series of actions, carried out by a user or application, which reads or updates the contents of the database.

    Example transactions (DreamHome)

    (a) Give a pay rise

    read(staffNo = x, salary)
    salary = salary * 1.1
    write(staffNo = x, new_salary)

    (b) Delete a staff member

    delete(staffNo = x)
    for all PropertyForRent records, pno
    begin
      read(propertyNo = pno, staffNo)
      if (staffNo = x) then
      begin
        staffNo = newStaffNo
        write(propertyNo = pno, staffNo)
      end
    end

    Transaction (b) touches many rows. If it fails half-way, some properties would point to a deleted staff member — the database would be inconsistent. Hence the need for transaction support.

    Outcomes of a transaction

    State transition diagram

    ACTIVE PARTIALLYCOMMITTED COMMITTED FAILED ABORTED BEGIN_TRANSACTION END_TRANSACTION COMMIT ABORT ABORT read/write
    StateMeaning
    ACTIVEThe transaction is executing its reads and writes.
    PARTIALLY COMMITTEDAfter the final statement has executed. It may still be found to violate serializability or an integrity constraint, or the system may fail before updates are safely recorded → FAILED.
    COMMITTEDSuccessful; updates are safely recorded.
    FAILEDCannot be committed, or aborted while ACTIVE (user abort, or concurrency control aborts it to ensure serializability).
    ABORTEDRolled back; database restored to its prior state.

    ACID properties

    PropertyMeaningResponsible subsystem
    Atomicity“All or nothing” — an indivisible unit, performed entirely or not at all.Recovery subsystem
    ConsistencyMust transform the database from one consistent state to another.DBMS (constraints) and application developers
    IsolationPartial effects of incomplete transactions must not be visible to other transactions.Concurrency control subsystem
    DurabilityEffects of a committed transaction are permanent and must not be lost because of later failure.Recovery subsystem
    Why consistency needs the developer too

    The DBMS enforces declared constraints, but if a programmer’s transfer transaction debits one account and credits the wrong account, every constraint is still satisfied — the DBMS can’t detect the logic error.

    Concurrency control

    Definition

    Concurrency control is the process of managing simultaneous operations on the database without having them interfere with one another.

    Three classic problems: Lost update Uncommitted dependency Inconsistent analysis

    Lost update problem

    A successfully completed update is overridden by another user.

    T1 withdraws £10 from an account with balx = £100; T2 deposits £100 into the same account. Run serially, the final balance would be £190.

    TimeT1T2balx
    t1begin_transaction100
    t2begin_transactionread(balx)100
    t3read(balx)balx = balx + 100100
    t4balx = balx − 10write(balx)200
    t5write(balx)commit90
    t6commit90

    Both read £100. T2 writes £200, then T1 overwrites it with £90 — T2’s £100 deposit is lost.

    Fix: prevent T1 from reading balx until after T2’s update is complete.

    Uncommitted dependency (dirty read) problem

    Occurs when one transaction can see intermediate results of another transaction before it has committed.

    TimeT3T4balx
    t1begin_transaction100
    t2read(balx)100
    t3balx = balx + 100100
    t4begin_transactionwrite(balx)200
    t5read(balx)⋮200
    t6balx = balx − 10rollback100
    t7write(balx)190
    t8commit190

    T4 updates balx to £200 but then aborts, so balx should return to £100. But T3 has already read the new value (£200) — dirty data — and uses it for its £10 reduction, giving £190 instead of £90.

    Fix: prevent T3 from reading balx until after T4 commits or aborts.

    Inconsistent analysis problem

    Occurs when a transaction reads several values but a second transaction updates some of them during the execution of the first. Sometimes called a dirty read or unrepeatable read.

    T6 totals balances x (£100), y (£50), z (£25) → correct total £175. Meanwhile T5 transfers £10 from balx to balz.

    TimeT5T6balxbalybalzsum
    t1begin_transaction1005025
    t2begin_transactionsum = 010050250
    t3read(balx)read(balx)10050250
    t4balx = balx − 10sum = sum + balx1005025100
    t5write(balx)read(baly)905025100
    t6read(balz)sum = sum + baly905025150
    t7balz = balz + 10905025150
    t8write(balz)905035150
    t9commitread(balz)905035150
    t10sum = sum + balz905035185
    t11commit905035185

    T6 read balx before the transfer (100) and balz after it (35), so the £10 is counted twice: result £185 — £10 too high.

    Fix: prevent T6 from reading balx and balz until after T5 has completed its updates.

    Serializability

    TermDefinition
    ScheduleA sequence of reads/writes by a set of concurrent transactions.
    Serial scheduleOperations of each transaction are executed one after the other, with no interleaving.
    Nonserial scheduleOperations from concurrent transactions are interleaved.
    Serializable scheduleA nonserial schedule that is equivalent to some serial schedule.

    The objective of serializability is to find nonserial schedules that allow transactions to execute concurrently without interfering — i.e. that produce the same result as some serial execution.

    When does order matter?

    1. If two transactions only read a data item → no conflict, order not important.
    2. If two transactions read or write completely separate data items → no conflict, order not important.
    3. If one transaction writes a data item and another reads or writes the same item → order is important (conflict).
    Memory aid

    A conflict needs same item + different transactions + at least one write: R–W, W–R, W–W.

    Conflict serializability example

    TimeS1: T7S1: T8S3 (serial): T7S3: T8
    t1beginbegin
    t2read(balx)read(balx)
    t3write(balx)write(balx)
    t4beginread(baly)
    t5read(balx)write(baly)
    t6write(balx)commit
    t7read(baly)begin
    t8write(baly)read(balx)
    t9commitwrite(balx)
    t10read(baly)read(baly)
    t11write(baly)write(baly)
    t12commitcommit

    In S1 (nonserial), every conflicting pair (on balx and on baly) has T7 before T8 — the same order as the serial schedule S3. By swapping non-conflicting operations (e.g. T8’s balx ops with T7’s baly ops) S1 can be transformed into S2 and then S3. So S1 and S2 are conflict serializable.

    Conflict serializable

    A schedule that orders any conflicting operations in the same way as some serial execution.

    Extra · precedence graph test

    Draw a node per transaction and an edge Ti → Tj whenever an operation of Ti conflicts with and precedes one of Tj. The schedule is conflict serializable iff the graph has no cycle. In the lost-update schedule, T1 reads before T2 writes (T1→T2) and T2 reads before T1 writes (T2→T1): a cycle → not serializable.

    Concurrency control techniques

    Locking

    Basic rules

    Lock compatibility matrix

    Ti holds \ Tj requestsSX
    S✅ true❌ false
    X❌ false❌ false

    Only S + S is compatible. Locks must be held for the duration of access.

    Two-Phase Locking (2PL)

    Definition

    A transaction follows the 2PL protocol if all locking operations precede the first unlock operation in the transaction.

    Growing phaseShrinking phase commit pointtime number of locks held

    It can be proved that if every transaction in a schedule follows 2PL, the schedule is guaranteed to be conflict serializable.

    Preventing lost update with 2PL

    TimeT1T2balx
    t1begin_transaction100
    t2begin_transactionwrite_lock(balx)100
    t3write_lock(balx)read(balx)100
    t4WAITbalx = balx + 100100
    t5WAITwrite(balx)200
    t6WAITcommit/unlock(balx)200
    t7read(balx)200
    t8balx = balx − 10200
    t9write(balx)190
    t10commit/unlock(balx)190 ✓

    T2 obtains an exclusive lock first; T1’s lock request is not granted, so T1 waits until T2 commits and releases the lock.

    Preventing uncommitted dependency with 2PL

    TimeT3T4balx
    t1begin_transaction100
    t2write_lock(balx)100
    t3read(balx)100
    t4begin_transactionbalx = balx + 100100
    t5write_lock(balx)write(balx)200
    t6WAITrollback/unlock(balx)100
    t7read(balx)100
    t8balx = balx − 10100
    t9write(balx)90
    t10commit/unlock(balx)90 ✓

    T3 cannot read balx until T4’s rollback has completed and released the lock, so it reads the correct value (100).

    Preventing inconsistent analysis with 2PL

    T5 precedes its reads with exclusive locks; T6 precedes its reads with shared locks. When T5 starts it obtains X on balx; T6’s S-lock request on balx must WAIT until T5 commits and unlocks balx and balz. T6 then reads 90 + 50 + 35 = 175 ✓.

    Deadlock

    Extra · since 2PL doesn’t prevent deadlock

    A deadlock is an impasse where two (or more) transactions each wait for a lock held by the other — e.g. T1 holds X(balx) and wants baly; T2 holds X(baly) and wants balx. Neither can proceed.

    Database recovery

    Definition

    Database recovery is the process of restoring the database to a correct state in the event of a failure.

    Need for recovery control

    Types of failure

    Transactions and recovery

    Example

    t0tctf crash T1 T2 T3 T4 T5 T6

    A bar ending in | = committed. Orange = still active at the crash; blue = committed between the checkpoint and the crash.

    The DBMS starts at t0 and fails at tf. Assume data for T2 and T3 have been written to secondary storage.

    ScenarioUNDOREDONo action
    No checkpointT1, T6 (active at crash)T2, T3, T4, T5 (all committed — recovery manager can’t know which were flushed)—
    Checkpoint at tcT1, T6T4, T5 (committed after tc)T2, T3 (committed before tc, already on disk)

    Recovery facilities

    The DBMS should provide:

    Log file

    Contains information about all updates to the database: transaction records and checkpoint records. Often used for other purposes too (e.g. auditing).

    Transaction records contain:

    Sample log file

    TidTimeOperationObjectBefore imageAfter imagepPtrnPtr
    T110:12START02
    T110:13UPDATESTAFF SL21(old value)(new value)18
    T210:14START04
    T210:16INSERTSTAFF SG37(new value)35
    T210:17DELETESTAFF SA9(old value)46
    T210:17UPDATEPROPERTY PG16(old value)(new value)59
    T310:18START011
    T110:18COMMIT20
    10:19CHECKPOINTT2, T3
    T210:19COMMIT60
    T310:20INSERTPROPERTY PG4(new value)712
    T310:21COMMIT110

    Notice: an INSERT has only an after-image; a DELETE has only a before-image. The checkpoint record lists the transactions active at that moment (T2, T3).

    Checkpointing

    Checkpoint

    A point of synchronization between the database and the log file. All buffers are force-written to secondary storage.

    Quick review

    State the ACID properties and who is responsible for each.
    Atomicity — recovery subsystem. Consistency — DBMS + application developers. Isolation — concurrency control subsystem. Durability — recovery subsystem.
    Name and briefly explain the three concurrency problems.
    Lost update: one committed update is overwritten. Uncommitted dependency (dirty read): a transaction uses a value written by another that later rolls back. Inconsistent analysis: a transaction reading many values sees some before and some after another transaction’s update.
    What is a serializable schedule?
    A nonserial (interleaved) schedule that produces the same result as some serial schedule.
    What are the two phases of 2PL and what does it guarantee?
    Growing (acquire only) and shrinking (release only). It guarantees conflict serializability but not freedom from deadlock.
    Why does a checkpoint reduce recovery work?
    All buffers are flushed at the checkpoint, so transactions that committed before it are already safely on disk and don’t need to be redone.