Presentation is loading. Please wait.

Presentation is loading. Please wait.

Transaction Management Overview

Similar presentations


Presentation on theme: "Transaction Management Overview"— Presentation transcript:

1 Transaction Management Overview
The slides for this text are organized into several modules. Each lecture contains about enough material for a 1.25 hour class period. (The time estimate is very approximate--it will vary with the instructor, and lectures also differ in length; so use this as a rough guideline.) This covers Lecture 1A in Module (6); it is a 1-lecture overview of the material, and is an alternative to Lectures 1 through 4 in this module, which provide more detailed coverage. Note that the text contains enough material for an even more detailed treatment than Lectures 1 through 4, e.g., view serializability, B-tree CC, non-locking approaches.) Module (1): Introduction (DBMS, Relational Model) Module (2): Storage and File Organizations (Disks, Buffering, Indexes) Module (3): Database Concepts (Relational Queries, DDL/ICs, Views and Security) Module (4): Relational Implementation (Query Evaluation, Optimization) Module (5): Database Design (ER Model, Normalization, Physical Design, Tuning) Module (6): Transaction Processing (Concurrency Control, Recovery) Module (7): Advanced Topics Instructor: Xintao Wu

2 Transactions Concurrent execution of user programs is essential for good DBMS performance. Because disk accesses are frequent, and relatively slow, it is important to keep the cpu humming by working on several user programs concurrently. A user’s program may carry out many operations on the data retrieved from the database, but the DBMS is only concerned about what data is read/written from/to the database. A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes.

3 Review: The ACID properties
A tomicity: All actions in the Xact happen, or none happen. C onsistency: If each Xact is consistent, and the DB starts consistent, it ends up consistent. I solation: Execution of one Xact is isolated from that of other Xacts. D urability: If a Xact commits, its effects persist. The Recovery Manager guarantees Atomicity & Durability.

4 Atomicity of Transactions
A transaction might commit after completing all its actions, or it could abort (or be aborted by the DBMS) after executing some actions. A very important property guaranteed by the DBMS for all transactions is that they are atomic. That is, a user can think of a Xact as always executing all its actions in one step, or not executing any actions at all. DBMS logs all actions so that it can undo the actions of aborted transactions.

5 Example Consider two transactions (Xacts):
T1: BEGIN A=A+100, B=B END T2: BEGIN A=1.06*A, B=1.06*B END Intuitively, the first transaction is transferring $100 from B’s account to A’s account. The second is crediting both accounts with a 6% interest payment. There is no guarantee that T1 will execute before T2 or vice-versa, if both are submitted together. However, the net effect must be equivalent to these two transactions running serially in some order.

6 Example (Contd.) Consider a possible interleaving (schedule):
T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B This is OK. But what about: T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B The DBMS’s view of the second schedule: T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B)

7 Scheduling Transactions
Serial schedule: Schedule that does not interleave the actions of different transactions. Equivalent schedules: For any database state, the effect (on the set of objects in the database) of executing the first schedule is identical to the effect of executing the second schedule. Serializable schedule: A schedule that is equivalent to some serial execution of the transactions. (Note: If each transaction preserves consistency, every serializable schedule preserves consistency. )

8 Anomalies with Interleaved Execution
Reading Uncommitted Data (WR Conflicts, “dirty reads”): Unrepeatable Reads (RW Conflicts): T1: R(A), W(A), R(B), W(B), Abort T2: R(A), W(A), C T1: R(A), R(A), W(A), C T2: R(A), W(A), C

9 Anomalies (Continued)
Overwriting Uncommitted Data (WW Conflicts): T1: W(A), W(B), C T2: W(A), W(B), C

10 Some Concepts A Serializable schedule over a set S of transactions is a schedule whose effect on any consistent database instance is guaranteed to be identical to that of some complete serial schedule over the set of committed transactions in S. Example Serializable, unrecoverable Cascading aborts A recoverable schedule is one in which transactions commit only after all transactions whose changes they read commit. T1 R(A) W(A) Abort T R(A) W(A) R(B) W(B) Commit

11 Lock-Based Concurrency Control
To ensure only serializable, recoverable schedules are allowed Strict Two-phase Locking (Strict 2PL) Protocol: Each Xact must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing. All locks held by a transaction are released when the transaction completes If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. Strict 2PL allows only serializable schedules.

12 Conflict Serializable Schedules
Two schedules are conflict equivalent if: Involve the same actions of the same transactions Every pair of conflicting actions of two committed transactions is ordered the same way two actions conflict if they operate on the same data object and at least one of them is write. Schedule S is conflict serializable if S is conflict equivalent to some serial schedule

13 Example A schedule that is not conflict serializable:
The cycle in the graph reveals the problem. The output of T1 depends on T2, and vice-versa. T1: R(A), W(A), R(B), W(B) T2: R(B), W(B), R(A), W(A) A T1 T2 Precedence graph B

14 Dependency Graph Precedence graph: One node per Xact; edge from Ti to Tj if an action of Ti precedes and conflicts with one of Tj’s actions Theorem: Schedule is conflict serializable if and only if its dependency graph is acyclic

15 Strict 2PL Strict Two-phase Locking (Strict 2PL) Protocol:
Each Xact must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing. All locks held by a transaction are released when the transaction completes If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. Strict 2PL allows only schedules whose precedence graph is acyclic

16 Strict 2PL(cont’d) Strict 2PL is too strict for serializability
Strict schedules are recoverable, no need cascading aborts A DBMS must be able to ensure that only serializable, recoverable schedules are allowed, and that no actions of committed transactions are lost while undoing aborted transactions. T1: R(A), W(A), R(B), W(B), … C T2: R(A), W(A), R(B), W(B)

17 Two-Phase Locking (2PL)
Two-Phase Locking Protocol Each Xact must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing. If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. A transaction can not request additional locks once it releases any locks.

18 Lock Management Lock and unlock requests are handled by the lock manager Lock table entry: Number of transactions currently holding a lock Type of lock held(shared or exclusive) Pointer to queue of lock requests Locking and unlocking have to be atomic operations Lock upgrade: transaction that holds a shared lock can be upgraded to hold an exclusive lock

19 Deadlocks Deadlock: Cycle of transactions waiting for locks to be released by each other. Two ways of dealing with deadlocks: Deadlock prevention Deadlock detection

20 Deadlock Prevention Assign priorities based on timestamps. Assume Ti wants a lock that Tj holds. Two policies are possible: Wait-Die: It Ti has higher priority, Ti waits for Tj; otherwise Ti aborts Wound-wait: If Ti has higher priority, Tj aborts; otherwise Ti waits If a transaction re-starts, make sure it has its original timestamp

21 Deadlock Detection Create a waits-for graph:
Nodes are transactions There is an edge from Ti to Tj if Ti is waiting for Tj to release a lock Periodically check for cycles in the waits-for graph

22 Deadlock Detection(cont’d)
Example T1: S(A), R(A) S(B) T2: X(B),W(B) X(C) T3: S(C),R(C) X(A) T4: X(B)

23 Aborting a Transaction
If a transaction Ti is aborted, all its actions have to be undone. Not only that, if Tj reads an object last written by Ti, Tj must be aborted as well! Most systems avoid such cascading aborts by releasing a transaction’s locks only at commit time. If Ti writes an object, Tj can read this only after Ti commits. In order to undo the actions of an aborted transaction, the DBMS maintains a log in which every write is recorded. This mechanism is also used to recover from system crashes: all active Xacts at the time of the crash are aborted when the system comes back up.

24 The Log The following actions are recorded in the log:
Ti writes an object: the old value and the new value. Log record must go to disk before the changed page! Ti commits/aborts: a log record indicating this action. Log records are chained together by Xact id, so it’s easy to undo a specific Xact. Log is often duplexed and archived on stable storage. All log related activities (and in fact, all CC related activities such as lock/unlock, dealing with deadlocks etc.) are handled transparently by the DBMS.

25 Motivation Atomicity: Durability: Transactions may abort (“Rollback”).
What if DBMS stops running? (Causes?) Desired Behavior after system restarts: T1, T2 & T3 should be durable. T4 & T5 should be aborted (effects not seen). crash! T1 T2 T3 T4 T5

26 Assumptions Concurrency control is in effect.
Strict 2PL, in particular. Updates are happening “in place”. i.e. data is overwritten on (deleted from) the disk. A simple scheme to guarantee Atomicity & Durability?

27 Handling the Buffer Pool
Force every write to disk? Poor response time. But provides durability. Steal buffer-pool frames from uncommited Xacts? If not, poor throughput. If so, how can we ensure atomicity? No Steal Steal Force Trivial Desired No Force

28 Write-Ahead Logging (WAL)
The Write-Ahead Logging Protocol: Must force the log record for an update before the corresponding data page gets to disk. Must write all log records for a Xact before commit. #1 guarantees Atomicity. #2 guarantees Durability. Exactly how is logging (and recovery!) done? We’ll study the ARIES algorithms.

29 Recovering From a Crash
There are 3 phases in the Aries recovery algorithm: Analysis: Scan the log forward (from the most recent checkpoint) to identify all Xacts that were active, and all dirty pages in the buffer pool at the time of the crash. Redo: Redoes all updates to dirty pages in the buffer pool, as needed, to ensure that all logged updates are in fact carried out and written to disk. Undo: The writes of all Xacts that were active at the crash are undone (by restoring the before value of the update, which is in the log record for the update), working backwards in the log. (Some care must be taken to handle the case of a crash occurring during the recovery process!)

30 Why do we need redo and undo?
Buffer page and disk Steal: the changes made to an object O in the buffer pool by a T can be written to disk before T commits. Force: when a T commits, we must ensure that all changes it has made to objects in the buffer pool are immediately forced to disk. Which strategy we should choose? No-steal, force (no undo, no redo) No-steal, no-force (no undo, redo) Steal, force (undo, no redo) Steal, no-force (undo, redo)

31 Summary Concurrency control and recovery are among the most important functions provided by a DBMS. Users need not worry about concurrency. System automatically inserts lock/unlock requests and schedules actions of different Xacts in such a way as to ensure that the resulting execution is equivalent to executing the Xacts one after the other in some order. Write-ahead logging (WAL) is used to undo the actions of aborted transactions and to restore the system to a consistent state after a crash. Consistent state: Only the effects of commited Xacts seen.

32 Properties of Trans---ACID
atomicity -- a T is treated as a unit of operation. All-or- nothing property consistency --- correctness T does not overwrite dirty data of other Ts. T does not commit any write until it commits all its writes T does not read dirty data from other Ts. Other Ts do not dirty any data read by T before T completes isolation Read uncomitted dirty read, fuzzy read, phantom Read Committed fuzzy read, phantom Repeatable Read phantom Anomaly Serializable --- none durability --- once T commits, its results are permanent d0 d1 d2 d3

33 Phenomena in SQL92 Dirty data refers to data values that have been updated by a T prior to its commitment Dirty Read (WR conflict) …, w1(x),…R2(x),…,C1 (or A1), …C2(or A2) or …, w1(x),…R2(x),…,C2 (or A2), …C1(or A1) Fuzzy Read (unrepeatable reads RW conflict) …, R1(x),…,W2(x),…,R1(x), …,C1(or A1),…,C2(or A2) or …, R1(x),…,W2(x),…,R1(x), …, C2(or A2),…,C1(or A1) Phantom …, R1(P),…,W2(yinP), …, R1(P),…, C1(or A1),…,C2(or A2) or ….,R1(P),…,W2(yinP), …,R1(P),…,C2(or A2),…,C1(or A1)


Download ppt "Transaction Management Overview"

Similar presentations


Ads by Google