Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Concurrency Control II: Locking and Isolation Levels.

Similar presentations


Presentation on theme: "1 Concurrency Control II: Locking and Isolation Levels."— Presentation transcript:

1 1 Concurrency Control II: Locking and Isolation Levels

2 2 Locking: A Technique for C. C. v Concurrency control usually done via locking. v Lock info maintained by a “lock manager”: –Stores (XID, RID, Mode) triples. u This is a simplistic view; suffices for now. –Mode  {S,X} –Lock compatibility table: v If a Xact can’t get a lock, it is suspended on a wait queue. -- SX S X      LOCK REQUESTED LOCKHELDLOCKHELD

3 3 Two-Phase Locking (2PL) v 2PL: –If T wants to read an object, first obtains an S lock. –If T wants to modify an object, first obtains X lock. –If T releases any lock, it can acquire no new locks! v Locks are automatically obtained by DBMS. v Guarantees serializability! –Why? Time # of locks lock point growing phase shrinking phase

4 4 Strict 2PL v Strict 2PL: –If T wants to read an object, first obtains an S lock. –If T wants to modify an object, first obtains X lock. –Hold all locks until end of transaction. v Guarantees serializability, and ACA, too! –Thus ensures recoverability! Time # of locks

5 5 Precedence Graph v A Precedence (or Serializability) graph: –Node for each committed Xact. –Arc from Ti to Tj if an action of Ti precedes and conflicts with an action of Tj. v T1 transfers $100 from A to B, T2 adds 6% –R 1 (A), W 1 (A), R 2 (A), W 2 (A), R 2 (B), W 2 (B), R 1 (B), W 1 (B) T1 T2

6 6 Conflict Serializability v 2 schedules are conflict equivalent if: –they have the same lists of actions, and –each pair of conflicting actions is ordered in the same way. v A schedule is conflict serializable if it is conflict equivalent to a serial schedule. –Note: Some serializable schedules are not conflict serializable! –Another Note: Some consistent schedules are not serializable!

7 7 Conflict Serializability & Graphs v Theorem 1: A schedule is conflict serializable iff its precedence graph is acyclic. v Theorem 2: 2PL ensures that the precedence graph will be acyclic! v Strict 2PL improves on this by avoiding cascading aborts, avoiding problems with “undo” in the presence of WW conflicts, and ensuring recoverable schedules.

8 8 Dynamic Databases v If we relax the assumption that the DB is a fixed collection of objects, even Strict 2PL will not ensure serializability: –T1 locks all pages containing sailor records with rating = 1, and finds oldest sailor (say, age = 71). –Next, T2 inserts a new sailor; rating = 1, age = 96. –T2 also deletes oldest sailor with rating = 2 (and, say, age = 80), and commits. –T1 now locks all pages containing sailor records with rating = 2, and finds oldest (say, age = 63). v No consistent DB state where T1 is “correct”!

9 9 The Problem v T1 implicitly assumes that it has locked the set of all sailor records with rating = 1. –Assumption only holds if no sailor records are added while T1 is executing! –The sailor with rating 1, age 96 is a phantom tuple –Need some mechanism to enforce this assumption. (Index locking, predicate locking, or table locking.) v Example shows that conflict serializability guarantees serializability only if the set of objects is fixed!

10 10 Summary of Concurrency Control v Concurrency control key to a DBMS. –More than just mutexes! v Transactions and the ACID properties: –C & I are handled by concurrency control. –A & D coming soon with logging & recovery. v Conflicts arise when two Xacts access the same object, and one of the Xacts is modifying it. v Serial execution is our model of correctness.

11 11 Summary, cont. v Serializability allows us to “simulate” serial execution with better performance. v 2PL: A simple mechanism to get serializability. –Strict 2PL also gives us recoverability, ACA v Lock manager module automates 2PL so that only the access methods worry about it. –Lock table is a big main-mem hash table v Deadlocks are possible, and typically a deadlock detector is used to solve the problem.

12 12 Summary, cont.: SQL-92 support


Download ppt "1 Concurrency Control II: Locking and Isolation Levels."

Similar presentations


Ads by Google