Lecture 18: Concurrency Control

Slides:



Advertisements
Similar presentations
Database Systems (資料庫系統)
Advertisements

1 Concurrency Control Chapter Conflict Serializable Schedules  Two actions are in conflict if  they operate on the same DB item,  they belong.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Concurrency Control II
Cs4432concurrency control1 CS4432: Database Systems II Lecture #23 Concurrency Control Professor Elke A. Rundensteiner.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17 Sections
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
CS4432: Database Systems II Lecture #26 Concurrency Control and Recovery Professor Elke A. Rundensteiner.
Quick Review of Apr 29 material
ICS 421 Spring 2010 Transactions & Concurrency Control (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Concurrency Control R&G - Chapter 17 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
Concurrency Control. Example Schedules Constraint: The sum of A+B must be the same Before: After: T1 read(A) A = A -50 write(A) read(B)
Transaction Processing: Concurrency and Serializability 10/4/05.
Transaction Management
Transactions or Concurrency Control. Introduction A program which operates on a DB performs 2 kinds of operations: –Access to the Database (Read/Write)
Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)
Concurrency Control John Ortiz.
Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control.
1 Lecture 5: Transactions Concurrency Control Wednesday October 26 th, 2011 Dan Suciu -- CSEP544 Fall 2011.
Academic Year 2014 Spring Academic Year 2014 Spring.
Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,
Introduction to Data Management CSE 344 Lecture 23: Transactions CSE Winter
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
Transactions. What is it? Transaction - a logical unit of database processing Motivation - want consistent change of state in data Transactions developed.
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
1 Database Systems ( 資料庫系統 ) December 27, 2004 Chapter 17 By Hao-hua Chu ( 朱浩華 )
CSE544: Transactions Concurrency Control Wednesday, 4/26/2006.
Switch off your Mobiles Phones or Change Profile to Silent Mode.
Jinze Liu. ACID Atomicity: TX’s are either completely done or not done at all Consistency: TX’s should leave the database in a consistent state Isolation:
CS 440 Database Management Systems Concurrency Control 1.
CS 540 Database Management Systems Concurrency Control 1.
CS 440 Database Management Systems
Concurrency Control.
Relational Database Systems 4
Database Systems II Concurrency Control
Part- A Transaction Management
Transaction Management
Introduction to Data Management CSE 344
Cse 344 March 25th – Isolation.
March 21st – Transactions
April 4, 2011 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 18 Transactions April 4, 2011 Ion.
March 7th – Transactions
Concurrency Control 11/22/2018.
CIS 720 Concurrency Control.
Transaction Management
March 9th – Transactions
Locks and Locking Mode ( )
Lecture 21: Concurrency & Locking
Yan Huang - CSCI5330 Database Implementation – Concurrency Control
CS162 Operating Systems and Systems Programming Review (II)
Notes 09: Transaction Processing
Basic Two Phase Locking Protocol
6.830 Lecture 12 Transactions: Isolation
6.830 Lecture 14 Two-phase Locking Recap Optimistic Concurrency Control 10/28/2015.
Conflicts.
Atomic Commit and Concurrency Control
Transaction management
Concurrency Control R&G - Chapter 17
Lecture 5: Transactions in SQL
Transaction Management
Temple University – CIS Dept. CIS661 – Principles of Data Management
CPSC-608 Database Systems
Introduction to Database Systems CSE 444 Lectures 17-18: Concurrency Control November 5-7, 2007.
Locks.
CONCURRENCY Concurrency is the tendency for different tasks to happen at the same time in a system ( mostly interacting with each other ) .   Parallel.
Database Systems (資料庫系統)
Database Systems (資料庫系統)
Presentation transcript:

Lecture 18: Concurrency Control Wednesday, November 10, 2004

Outline Serial and Serializable Schedules (18.1) Conflict Serializability (18.2) Locks (18.3) Multiple lock modes (18.4) The tree protocol (18.7)

The Problem Multiple transactions are running concurrently T1, T2, … They read/write some common elements A1, A2, … How can we prevent unwanted interference ? The SCHEDULER is responsible for that

Three Famous Anomalies What can go wrong if we don’t do anything: T1: WRITE(A) T2: READ(A); T1: ABORT Dirty read T1: READ(A) T2: READ(A); T1: A := A+5 T2: A := A*1.3 T1: WRITE(A) T2: WRITE(A); Lost update T1: A := 20; B := 50; T1: WRITE(A) T2: READ(A); T2: READ(B); T1: WRITE(B) Inconsistent read What else may go wrong ?

Schedules Given multiple transactions A schedule is a sequence of interleaved actions from all transactions

Example T1 T2 READ(A, t) READ(A, s) t := t+100 s := s*2 WRITE(A, t) WRITE(A,s) READ(B, t) READ(B,s) WRITE(B,t) WRITE(B,s)

A Serial Schedule T1 T2 READ(A, t) t := t+100 WRITE(A, t) READ(B, t) WRITE(B,t) READ(A,s) s := s*2 WRITE(A,s) READ(B,s) WRITE(B,s)

Serializable Schedule A schedule is serializable if it is equivalent to a serial schedule

A Serializable Schedule T1 T2 READ(A, t) t := t+100 WRITE(A, t) READ(A,s) s := s*2 WRITE(A,s) READ(B, t) WRITE(B,t) READ(B,s) WRITE(B,s) Notice: this is NOT a serial schedule

A Non-Serializable Schedule T1 T2 READ(A, t) t := t+100 WRITE(A, t) READ(A,s) s := s*2 WRITE(A,s) READ(B,s) WRITE(B,s) READ(B, t) WRITE(B,t)

Ignoring Details Sometimes transactions’ actions may commute accidentally because of specific updates Serializability is undecidable ! The scheduler shouldn’t look at the transactions’ details Assume worst case updates, only care about reads r(A) and writes w(A)

Notation T1: r1(A); w1(A); r1(B); w1(B) T2: r2(A); w2(A); r2(B); w2(B)

Conflict Serializability Two actions by the same transaction Ti: ri(X); wi(Y) Two writes by Ti, Tj to the same element wi(X); wj(X) Read and write by Ti, Tj to same element wi(X); rj(X) ri(X); wj(X)

Conflict Serializability A schedule is conflict serializable if it can be transformed into a serial schedule by a series of swappings of adjacent non-conflicting actions Example: r1(A); w1(A); r2(A); w2(A); r1(B); w1(B); r2(B); w2(B) r1(A); w1(A); r1(B); w1(B); r2(A); w2(A); r2(B); w2(B)

Conflict Serializability Any conflict serializable schedule is also a serializable schedule (why ?) The converse is not true, even under the “worst case update” assumption Lost write w1(Y); w2(Y); w2(X); w1(X); w3(X); Equivalent, but can’t swap w1(Y); w1(X); w2(Y); w2(X); w3(X);

The Precedence Graph Test Is a schedule conflict-serializable ? Simple test: Build a graph of all transactions Ti Edge from Ti to Tj if Ti makes an action that conflicts with one of Tj and comes first The test: if the graph has no cycles, then it is conflict serializable !

Example 1 r2(A); r1(B); w2(A); r3(A); w1(B); w3(A); r2(B); w2(B) B A 1 This schedule is conflict-serializable

Example 2 r2(A); r1(B); w2(A); r2(B); r3(A); w1(B); w3(A); w2(B) B A 1 This schedule is NOT conflict-serializable

Scheduler The scheduler is the module that schedules the transaction’s actions, ensuring serializability How ? Three techniques: Locks Time stamps Validation

Locking Scheduler Simple idea: Each element has a unique lock Each transaction must first acquire the lock before reading/writing that element If the lock is taken by another transaction, then wait The transaction must release the lock(s)

Notation li(A) = transaction Ti acquires lock for element A ui(A) = transaction Ti releases lock for element A

Example The scheduler has ensured a conflict-serializable schedule T1 L1(A); READ(A, t) t := t+100 WRITE(A, t); U1(A); L1(B) L2(A); READ(A,s) s := s*2 WRITE(A,s); U2(A); L2(B); DENIED… READ(B, t) WRITE(B,t); U1(B); …GRANTED; READ(B,s) WRITE(B,s); U2(B); The scheduler has ensured a conflict-serializable schedule

Example Locks did not enforce conflict-serializability !!! T1 T2 L1(A); READ(A, t) t := t+100 WRITE(A, t); U1(A); L2(A); READ(A,s) s := s*2 WRITE(A,s); U2(A); L2(B); READ(B,s) WRITE(B,s); U2(B); L1(B); READ(B, t) WRITE(B,t); U1(B); Locks did not enforce conflict-serializability !!!

Two Phase Locking (2PL) The 2PL rule: In every transaction, all lock requests must preceed all unlock requests This ensures conflict serializability ! (why?)

Example: 2PL transactcions L1(A); L1(B); READ(A, t) t := t+100 WRITE(A, t); U1(A) L2(A); READ(A,s) s := s*2 WRITE(A,s); L2(B); DENIED… READ(B, t) WRITE(B,t); U1(B); …GRANTED; READ(B,s) WRITE(B,s); U2(A); U2(B); Now it is conflict-serializable

Deadlock Trasaction T1 waits for a lock held by T2; But T2 waits for a lock held by T3; While T3 waits for . . . . . . . . . .and T73 waits for a lock held by T1 !! Could be avoided, by ordering all elements (see book); or deadlock detection plus rollback

Lock Modes S = Shared lock (for READ) X = exclusive lock (for WRITE) U = update lock Initially like S Later may be upgraded to X I = increment lock (for A := A + something) Increment operations commute READ CHAPTER 18.4 !

The Locking Scheduler Taks 1: add lock/unlock requests to transactions Examine all READ(A) or WRITE(A) actions Add appropriate lock requests Ensure 2PL !

The Locking Scheduler Task 2: execute the locks accordingly Lock table: a big, critical data structure in a DBMS ! When a lock is requested, check the lock table Grant, or add the transaction to the element’s wait list When a lock is released, re-activate a transaction from its wait list When a transaction aborts, release all its locks Check for deadlocks occasionally

The Tree Protocol An alternative to 2PL, for tree structures E.g. B-trees (the indexes of choice in databases)

The Tree Protocol Rules: The first lock may be any node of the tree Subsequently, a lock on a node A may only be acquired if the transaction holds a lock on its parent B Nodes can be unlocked in any order (no 2PL necessary) The tree protocol is NOT 2PL, yet ensures conflict-serializability !