Transaction Schedules and Concurrency
Transaction, Schedules and Serializability
Section titled βTransaction, Schedules and Serializabilityβ1. Transaction
-
A transaction is a sequence of operations== performed ==as a single logical unit of work.
- Must satisfy ACID properties:
-
Atomicity β All or none of its operations are executed.
-
Consistency β Database remains in a valid state before and after transaction.
-
Isolation β Concurrent transactions do not interfere with each other.
-
Durability β Once committed, changes are permanent.
-
2. Schedule
-
A schedule is an order in which the operations (read/write) of multiple transactions are executed.
- Types of Schedules:
-
Serial Schedule: Transactions are executed one after another β no interleaving.
-
Concurrent Schedule: Operations of multiple transactions are interleaved for better performance.
-
3. Serializability
- A schedule is serializable if its effect on the database is equivalent to a serial schedule.
-
It ensures consistency in concurrent executions.
Types of Serializability β
- Conflict Serializability
- View Serializability
Conflict Serializable Schedule
- Conflict β
- Two operations conflict if:
- They belong to different transactions,
- Access the same data item, and
- At least one is a write operation.
- Two operations conflict if:
- Conflict Serializability
- A schedule is conflict-serializable if it can be transformed into a serial schedule by swapping non-conflicting operations.
Precedence Graph (Serialization Graph)
- Nodes represent transactions.
- Directed edge
Ti -> Tjmeans an operation inTiprecedes and conflicts with an operation inTj. - If the graph has no cycles β Schedule is conflict-serializable.
Conflict Serializability & View Serializability
Section titled βConflict Serializability & View Serializabilityβ1. Conflict Serializability
1. Transactions & Operations
- Each transaction: sequence of operations:
r(read),w(write) - Data items: X, Y, β¦
2. Conflict Rules
- Conflict occurs if all three are true:
- Same data item
- Different transactions
- At least one operation is a write
- No conflict: r vs r on same item
3. Steps to Check Conflict Serializability
- List all operations in schedule
- Identify conflicting pairs (use rules above)
- Draw Precedence Graph:
- Nodes = transactions
- Edge T1 β T2 if T1βs operation conflicts with T2βs later operation
- Check graph:
- Cycle β Not conflict serializable
- No cycle β Conflict serializable
4. Common Edge Patterns
r-wβ T1 β T2w-rβ T1 β T2w-wβ T1 β T2r-rβ No edge
5. Example
- Transactions:
- T1:
r1(X), r1(Y), w1(X) - T2:
r2(X), r2(Y), w2(Y)
- T1:
- Schedule S1:
r1(X); r1(Y); r2(Y); w2(Y); w1(X)- Conflicts:
r1(Y)-w2(Y) β T1 β T2 - Graph: T1 β T2 β No cycle β Serializable
- Conflicts:
- Schedule S2:
r2(X); r2(Y); w2(Y); r1(Y); w1(X)- Conflicts:
w2(Y)-r1(Y), r2(X)-w1(X) β T2 β T1 - Graph: T2 β T1 β No cycle β Serializable
- Conflicts:
View Serializability
1. Basic Idea
- Schedule is view serializable if it is view equivalent to some serial schedule
- We compare what values are read and written, not operation order
2. View Equivalence Conditions
Two schedules S and Sβ² are view equivalent if all three hold:
- Initial Read Rule
- If a transaction
TireadsXwritten by no one inS, then inSβ²alsoTimust read the initial value ofX
- If a transaction
- Read-From Rule
- If
TireadsXwritten byTjinS, then inSβ²alsoTimust readXfrom the sameTj
- If
- Final Write Rule
- The transaction that performs the last write on X in S must also be the last writer of X in Sβ²
3. Steps to Check View Serializability
- Identify read-from relationships
- Identify final writes on each data item
- Try to find a serial order satisfying both
- If possible β View Serializable
Else β Not View Serializable
4. Key Properties
- View serializability is weaker than conflict serializability
- Every conflict-serializable schedule is view-serializable
- Some view-serializable schedules are not conflict-serializable
5. Special Case: Blind Write
- Write without prior read (
w(X)only) - Causes schedules that are:
- View serializable
- But not conflict serializable
6. Example (View Serializable but NOT Conflict Serializable)
- Transactions
- T1:
w1(X) - T2:
w2(X)
- T1:
- Schedule
w1(X); w2(X)
- No reads β no read-from constraints
- Final write on
Xis byT2 - Equivalent to serial order
T1βT2 - View Serializable
- But
w1(X)conflicts withw2(X)β cycle possible in precedence graph - Not Conflict Serializable
Comparison: Conflict vs View Serializability
| Aspect | Conflict Serializability | View Serializability |
|---|---|---|
| Based on | Conflicting operations | Read-from + final write |
| Test Method | ==Precedence graph== | ==Logical checking== |
| Blind Writes | Not allowed | Allowed |
| Power | Stronger | Weaker |
| Practical Use | Easy to test | Hard (NP-complete) |
8. Exam-Oriented Conclusion
- If precedence graph has no cycle β Conflict Serializable β View Serializable
- If cycle exists, still may be View Serializable
- GATE/PSU exams:
- Conflict serializability β graph-based
- View serializability β focus on blind writes + final write
Concurrency Control
Section titled βConcurrency Controlβ- Mechanism to control the simultaneous execution of transactions to preserve isolation and consistency.
Objectives
-
Maintain database consistency.
-
Allow maximum parallelism.
- Prevent anomalies like: β
- Lost Update
- Temporary Update (Dirty Read)
- Incorrect Summary
- Unrepeatable Read
Anomalies Overview
1. Lost Update
-
Occurs when two or more transactions read the same data item and update it simultaneously, causing one update to be overwritten by another.
-
Problem: The final value reflects only one transactionβs update; others are lost.
-
Example:
T1: Read(X=100)T2: Read(X=100)T1: X = X + 50 β X=150T2: X = X - 30 β X=70Final value = 70 (T1βs update lost)
2. Temporary Update (Dirty Read)
-
Occurs when a transaction reads a data item updated by another uncommitted transaction.
-
If the updating transaction rolls back, the first transaction has read an invalid (dirty) value.
-
Problem: Leads to inconsistent or incorrect results.
-
Example:
T1: Write(X=500) β Not yet committedT2: Read(X=500)T1: Rollback β X restored to old valueT2 used a value (500) that never actually existed in the database.
3. Incorrect Summary
-
Occurs when a transaction computes an aggregate (e.g., SUM, AVG) on data that is being modified by other transactions concurrently.
-
Problem: Some updates are reflected while others are not, leading to partial or incorrect results.
-
Example:
T1: SUM(salaries) of all employeesT2: Update salary of one employeeIf T1 reads part of the table before and part after T2βs update β incorrect total.
4. Unrepeatable Read
-
Occurs when a transaction reads the same data item twice and gets different values because another transaction modified the data in between.
-
Problem: Inconsistent view of the same data within one transaction.
-
Example:
T1: Read(X=100)T2: Write(X=200) β CommitT1: Read(X=200)T1 reads different values for X in the same execution β unrepeatable read.
Concurrency Control Protocols
Section titled βConcurrency Control Protocolsβ- Lock-Based Protocols (e.g., Two-Phase Locking)
- Two Phase Locking (2PL)
- Strict Two Phase Locking (Strict 2PL)
- Timestamp-Based Protocols
- Timestamp Ordering
- Thomasβ Write Rule
1. Two-Phase Locking (2PL) Protocol
- Transactions use locks to control access to data items.
- Lock types:
-
Shared Lock (S) β For reading (multiple transactions can share).
-
Exclusive Lock (X) β For writing (only one transaction can hold).
-
Phases
- Growing Phase β Transaction acquires locks, cannot release any.
- Shrinking Phase β Transaction releases locks, cannot acquire new ones.
Properties
- Conflict Serializability: Ensured.
- No Deadlock Freedom: Not guaranteed (deadlocks may occur).
- No Recoverability: Possible, but not always guaranteed β depends on lock release timing. Cascading rollbacks can occur if a transaction releases locks before commit.
2. Strict Two-Phase Locking (Strict 2PL)
- A stricter form of 2PL ensuring recoverability and avoiding cascading rollbacks.
Rules
- All exclusive (write) locks are held until the transaction commits or aborts.
- Locks are released only after completion of the transaction.
Features:
- Conflict Serializability: Guaranteed.
- No Deadlock Freedom: Not guaranteed (deadlocks can still occur).
- Recoverability: Guaranteed (no cascading rollbacks or dirty reads).
- Strictness: Ensures strict schedules (safe for recovery).
3. Timestamp-Ordering Protocol
-
Each transaction T gets a unique timestamp (TS(T)) when it starts.
- The DBMS ensures that all conflicting operations execute in timestamp order.
For Each Data Item (Q):
-
read_TS(Q): Largest timestamp of any transaction that read Q.
-
write_TS(Q): Largest timestamp of any transaction that wrote Q.
Rules:
- Read Rule:
- If
TS(T) < write_TS(Q)β Abort T (T is too old). - Else, execute read and set
read_TS(Q) = max(read_TS(Q), TS(T)).
- If
- Write Rule:
- If
TS(T) < read_TS(Q)orTS(T) < write_TS(Q)β Abort T. - Else, perform write and set
write_TS(Q) = TS(T).
- If
Features:
- Conflict Serializability: Ensured.
- Deadlock Freedom: Guaranteed (no waiting; ordered by timestamps).
- No Recoverability: Achieved if transactions are executed strictly in timestamp order. However, not guaranteed by default β requires additional mechanisms to ensure commit order consistency.
- Can cause many aborts due to timestamp conflicts.
4. Thomasβ Write Rule (with Timestamp-Ordering Protocol) β
-
A modification of the timestamp-ordering protocol== that allows ==ignoring obsolete writes.
- If a transaction tries to write an item older than the current version, its write is skipped instead of aborting the transaction.
Rule:
- If
TS(T) < write_TS(Q)β Ignore the write (instead of abort).
Advantages:
- Serializability: Maintained (though not strict).
- Deadlock Freedom: Guaranteed (same as timestamp ordering).
- Recoverability: Same as basic timestamp ordering β must enforce commit ordering to ensure full recoverability.
- Improved Concurrency: Fewer aborts, better throughput.
- No Strictness: Not ensured (obsolete writes may be ignored).
Concurrency Control Protocols Comparison (Transposed)
| Property | Two-Phase Locking (2PL) | Strict Two-Phase Locking (Strict 2PL) | Timestamp Ordering (TO) | Thomasβ Write Rule |
|---|---|---|---|---|
| Conflict Serializability | β Ensured | β Ensured | β Ensured | β Ensured (non-strict) |
| Recoverability | β Conditional | β β Guaranteed | β Conditional (depends on commit order) | β Conditional (similar to TO) |
| Deadlock Freedom | β Deadlocks possible | β Deadlocks possible | β Deadlock-free | β Deadlock-free |
| Strictness | β Not strict | β Strict | β Not strict | β Not strict |
| Cascading Rollback | β Possible | β Prevented | β Prevented | β Prevented |
| Extra Notes / Remarks | Basic protocol; ensures serializability but may cause cascading rollbacks. | Most used; ensures serializability + recoverability + strictness; only issue: deadlocks. | Deadlock-free but causes frequent aborts due to timestamp conflicts. | Ignores obsolete writes; improves concurrency; serializable but non-strict. |
Transaction Schedules - Property Detection Cheat Sheet
Section titled βTransaction Schedules - Property Detection Cheat Sheetβ1. Recoverability
Check:1. If Ti reads a value written by Tj, then Tj must commit before Ti commits.2. If Ti commits before Tj (whose data Ti used), schedule is NOT recoverable.- Recoverable:
T1: W(X) -------- CommitT2: R(X) ---------------- CommitCheck: T2 reads X from T1, and T1 commits before T2 β Recoverable- Non-Recoverable:
T2: R(X) ----- CommitT1: W(X) --------------- CommitT2 read uncommitted data of T1 and committed before T1 β Not Recoverable2. Avoids Cascading Aborts (ACA / Non-Cascadability)
Check:1. If Ti reads a value written by Tj, Tj must COMMIT before Ti reads.2. No READ allowed from an uncommitted write.3. If any read happens from an uncommitted write β Cascading possible.- Non-Cascadability (ACA) Example
T1: W(X) ---- CommitT2: R(X) -------- CommitRead happens only after T1 commits β ACA- Cascading:
T1: W(X)T2: R(X) ---- Abort T1 β T2 must abort β Cascading3. Strictness (Stricter than ACA)
Check:1. No READ or WRITE allowed on an item written by an uncommitted txn.2. Both read-after-write and write-after-write must wait for commit.3. If any op touches data of an uncommitted write β Not Strict.- Strict:
T1: W(X) ---- CommitT2: W(X) -------- CommitNo R/W on X until T1 commits β Strict- Not strict:
T1: W(X)T2: W(X) ---- CommitT2 writes X before T1 commits β Not Strict4. Conflict Serializability
Steps:1. Build precedence graph (Ti β Tj if Tiβs conflicting op comes before Tj).2. Conflicting ops: R-W, W-R, W-W on SAME data item.3. If graph has a cycle β Not conflict serializable.4. If no cycle β Conflict serializable.- Conflict Serializability:
T1: R(X) ---- W(X)T2: R(X) ---- W(X)
Conflicts:T1 W(X) before T2 R(X) β T1βT2T1 W(X) before T2 W(X) β T1βT2Graph has no cycle β Conflict Serializable (order T1,T2)- Non-conflict-serializable:
T1: R(X) ----- W(Y)T2: W(X) ----- R(Y)
Conflicts:T2 W(X) β T1 R(X) β T2βT1T1 W(Y) β T2 R(Y) β T1βT2Cycle β Not conflict serializable5. View Serializability
Check:1. Same initial reads.2. Same reads-from relationships.3. Same final writes.4. If matches some serial order β View serializable.5. If not β View non-serializable.- View Serializability
T1: W(X)T2: R(X) ---- W(X)
Initial reads same, reads-from same, final write by T2 β View serializable (T1,T2)- Not view serializable:
T1: W(X)T2: W(X)T3: R(X) (but ambiguous origin)
Ambiguous reads-from β Not view serializable6. Cascading Aborts Detection
If Tj aborts and Ti has read uncommitted data of Tj β Ti must abort.If any such chain exists β Cascading abort schedule.- Cascading Aborts:
T1: W(A)T2: R(A)T1 aborts β T2 must abort β Cascading aborts- Dirty Read:
T1: W(X)T2: R(X)T1 not committed β Dirty read7. Dirty Read Detection
If Ti reads data written by uncommitted Tj β Dirty read.- Dirty Read:
T1: W(X)T2: R(X)T1 not committed β Dirty read8. Dirty Write Detection
If Ti writes to an item previously written by uncommitted Tj β Dirty write.- Dirty Write:
T1: W(X)T2: W(X)T1 uncommitted β Dirty write9. Serial Schedule Check
All transactions execute in non-overlapping blocks.If operations of each transaction are contiguous β Serial.- Serial Schedule:
T1: R(X), W(X), CommitT2: R(Y), W(Y), CommitAll T1 ops then all T2 ops β Serial10. Serializable but Not Conflict Serializable
If precedence graph is cyclic but schedule still maintains view conditions βView-serializable but NOT conflict-serializable.- Serializable but Not Conflict Serializable
T1: W(X)T2: W(X)T3: R(X)
Final write and reads-from match a serial order β View serializableBut W-W conflicts cause cycles β Not conflict serializable11. Precedence Graph Rules (Quick)
TiβTj when:1. Ti writes X before Tj reads X2. Ti reads X before Tj writes X3. Ti writes X before Tj writes X12. Strict vs ACA vs Recoverable (Hierarchy)
Strict β ACA β RecoverableReverse not always true.