IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña

Slides:



Advertisements
Similar presentations
Lecture plan Transaction processing Concurrency control
Advertisements

Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
IDA / ADIT Lecture 10: Database recovery Jose M. Peña
Principles of Transaction Management. Outline Transaction concepts & protocols Performance impact of concurrency control Performance tuning.
IDA / ADIT Databasteknik Databaser och bioinformatik Transaction Fang Wei-Kleiner.
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
CS 728 Advanced Database Systems Chapter 21 Introduction to Protocols for Concurrency Control in Databases.
Database Systems, 8 th Edition Concurrency Control with Time Stamping Methods Assigns global unique time stamp to each transaction Produces explicit.
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 and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Concurrency control using transactions 1Transactions.
Transaction Management and Concurrency Control
Transaction Management
ICS (072)Concurrency Control1 Transaction Processing and Concurrency Control Dr. Muhammad Shafique Chapter March 2008.
1 Minggu 8, Pertemuan 15 Transaction Management Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
ACS-4902 R. McFadyen 1 Chapter 18 Database Concurrency Control Locking 18.1 Shared/Exclusive (aka Read/Write) Locks Lock Operations Read_lock(X) Write_lock(X)
1 Introduction to Transaction Processing (1)
Concurrency Control John Ortiz.
Transaction Processing
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
System Catalogue v Stores data that describes each database v meta-data: – conceptual, logical, physical schema – mapping between schemata – info for query.
Database Systems: Transaction Management
08_Transactions_LECTURE2 DBMSs should guarantee ACID properties (Atomicity, Consistency, Isolation, Durability). This is typically done by guaranteeing.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
The Concept of Transaction Processing A Transaction: logical unit of database processing that includes one or more access operations (read - retrieval,
Chapterb19 Transaction Management Transaction: An action, or series of actions, carried out by a single user or application program, which reads or updates.
Transaction Management, Concurrency Control and Recovery Chapter 20 1.
ICS (072)Concurrency Control Techniques1 Concurrency Control Techniques Chapter 18 Dr. Muhammad Shafique.
Chapter 11 Concurrency Control. Lock-Based Protocols  A lock is a mechanism to control concurrent access to a data item  Data items can be locked in.
Transactions CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley 2002)
Concurrency Control Techniques Chapter 18
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
1 Transactions Chapter Transactions A transaction is: a logical unit of work a sequence of steps to accomplish a single task Can have multiple.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Chapter 18 Concurrency Control Techniques Copyright © 2004 Pearson Education, Inc.
1 How can several users access and update the information at the same time? Real world results Model Database system Physical database Database management.
Transactions. What is it? Transaction - a logical unit of database processing Motivation - want consistent change of state in data Transactions developed.
II.I Selected Database Issues: 2 - Transaction ManagementSlide 1/20 1 II. Selected Database Issues Part 2: Transaction Management Lecture 4 Lecturer: Chris.
Chapter 16 Concurrency. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.16-2 Topics in this Chapter Three Concurrency Problems Locking Deadlock.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Lec 9 Concurrency Control Techniques.
Transactions and Concurrency Control Fall 2007 Himanshu Bajpai
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
1 CSE 480: Database Systems Lecture 24: Concurrency Control.
Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 14: Transactions.
1 Database Systems ( 資料庫系統 ) December 27, 2004 Chapter 17 By Hao-hua Chu ( 朱浩華 )
1 Lecture 4: Transaction Serialization and Concurrency Control Advanced Databases CG096 Nick Rossiter [Emma-Jane Phillips-Tait]
Transaction Management and Concurrent Control
NOEA/IT - FEN: Databases/Transactions1 Transactions ACID Concurrency Control.
Lecture 8 Transactions & Concurrency UFCE8K-15-M: Data Management.
Multidatabase Transaction Management COP5711. Multidatabase Transaction Management Outline Review - Transaction Processing Multidatabase Transaction Management.
1 Database Systems ( 資料庫系統 ) December 27/28, 2006 Lecture 13 Merry Christmas & New Year.
Concurrency Control The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems, 4th edition,
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
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:
DBMS Deadlock.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Enterprise Database Systems Transaction Processing Dr. Georgia Garani Dr. Theodoros Mitakos Technological Educational Institution of Larissa.
Enterprise Database Systems Transaction Processing
Transaction Management
Concurrency Control Techniques
Part- A Transaction Management
Transaction Properties
4. Concurrency control techniques
Chapter 10 Transaction Management and Concurrency Control
Lecture 21: Concurrency & Locking
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Transaction management
C. Faloutsos Transactions
Presentation transcript:

IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña

IDA / ADIT 2 How can several users access and update the database at the same time ? Real world Model Database system Physical database Database management system Processing of Queries/updates Access to stored data

IDA / ADIT 3 Concurrent processing Single user system: At most one user can use the system at each point in time. Multiple user system: Several users can use the system at the same time. o Multiple CPU: Parallel processing. o One CPU: Concurrent processing, interleaving. Hereinafter, we focus on multiple user systems with just one CPU.

IDA / ADIT 4 Transactions: Definition A transaction is a logical unit of database processing and consists of one or several operations. Simplified database operations in a transaction: o Read-item(X) o Write-item(X)

IDA / ADIT 5 Transactions: Example T1T2 Read-item(my-account) my-account := my-account Write-item(my-account) Read-item(other-account) other-account := other-account Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account)

IDA / ADIT 6 How to execute Read-item(X) Locate the block on disk that contains X. Copy the block to primary memory (a buffer). Copy X from the buffer to the program variable X.

IDA / ADIT 7 How to execute Write-item(X) 1.Locate the block on disk that contains X. 2.Copy the block to primary memory (a buffer). 3.Copy the value of the program variable X to the right place in the buffer. 4.Store the modified block on disk.

IDA / ADIT 8 Schedule: Definition A schedule defines the order in which the operations in a set of transactions are to be executed. Is any order equally good ?

IDA / ADIT 9 Schedule: Example T1T2 Read-item(my-account) my-account := my-account Write-item(my-account) Read-item(other-account) Other-account := other-account Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME

IDA / ADIT 10 Lost update problem T1T2 Read-item(my-account) my-account := my-account Write-item(my-account) Read-item(other-account) Other-account := other-account Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME

IDA / ADIT 11 Dirty read problem T1T2 Read-item(my-account) my-account := my-account Write-item(my-account) Read-item(other-account) FAIL Read-item(my-account) my-account := my-account Write-item(my-account) TIME

IDA / ADIT 12 Incorrect summary problem T1T2 Read-item(my-account1) my-account1 := my-account Write-item(my-account1) Read-item(my-account1) sum := sum + my-account1 TIME Read-item(my-account2) my-account2 := my-account Write-item(my-account2) Read-item(my-account2) sum := sum + my-account2sum := 0

IDA / ADIT 13 Unrepeatable read problem T1T2 Read-item(my-account) my-account:= my-account TIME Read-item(my-account) Write-item(my-account)

IDA / ADIT 14 Properties for transactions Properties for transactions ACID: Atomicity, Consistency preservation, Isolation, Durability A: A transaction is an atomic unit: It is either executed completely or not at all. C: A database that is in a consistent state before the execution of a transaction (i.e. it fulfills the conditions in the relational model and any other condition declared for the database), is also in a consistent state after the execution of the transaction. I: A transaction should act as if it is executed isolated from the other transactions. D: Changes in the database made by a committed transaction are permanent.

IDA / ADIT 15 Properties for transactions Properties for transactions Who ensures that the ACID property are satisfied ? o Atomicity: Recovery system. o Consistency preservation: Programmer + DBMS. o Isolation: Concurrency contol. o Durability: Recovery system.

IDA / ADIT 16 Serial schedule: Definition Serial schedule: Definition A schedule S is serial if the operations in any transaction T are executed directly after each other. A serial schedule satisfies the isolation property but it may highly inefficient, i.e. it may not maximize the CPU usage.

IDA / ADIT 17 Serial schedule: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) Read-item(other-account) other-account := other-account Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME

IDA / ADIT 18 Serial schedule: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) Read-item(other-account) other-account := other-account Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME

IDA / ADIT 19 Non-serial schedule: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) other-account := other-account Read-item(other-account) Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME

IDA / ADIT 20 Non-serial schedule: Example T1T2 Read-item(my-account) My-account := my-account Write-item(my-account) Read-item(other-account) Other-account := other-account Write-item(other-account) Read-item(my-account) My-account := my-account Write-item(my-account) TIME

IDA / ADIT 21 A serial schedule satisfies the isolation property but it may highly inefficient, i.e. it may not maximize the CPU usage. Solution: Find a non-serial schedule that is equivalent to a serial schedule. Equivalent = They put the database in the same final state for any initial state of the database. Equivalent = Conflict equivalent, i.e. they execute the conflicting operations in the same order, e.g. Read-item(X) after Write-item(X), etc. A schedule is serializable if there is a conflict equivalent serial schedule. Serializable schedule: Definition

IDA / ADIT 22 Serializable schedule: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) other-account := other-account Read-item(other-account) Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME

IDA / ADIT 23 Serializable schedule: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) other-account := other-account Read-item(other-account) Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME No conflict on the resource

IDA / ADIT 24 Serializable schedule: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) other-account := other-account Read-item(other-account) Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME No conflict on the resource

IDA / ADIT 25 Non-serializable schedule: Example T1T2 Read-item(my-account) My-account := my-account Write-item(my-account) Read-item(other-account) Other-account := other-account Write-item(other-account) Read-item(my-account) My-account := my-account Write-item(my-account) TIME Conflict!

IDA / ADIT 26 Conflict: Definition Conflict: Definition Two operations are in conflict if o they belong to different transactions, o they access (read/write) the same data item X, and o one of the operations is a Write-item(X).

IDA / ADIT 27 Conflict equivalence: Definition Conflict equivalence: Definition A schedule is serializable if there is a conflict equivalent serial schedule. Two schedules and are conflict equivalent if the order of any two conflicting operations is the same in both schedules. In a (conflict) serializable schedule it is possible to reorder the operations and get a serial schedule.

IDA / ADIT 28 Serializable schedule: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) other-account := other-account Read-item(other-account) Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME

IDA / ADIT 29 Non-serializable schedule: Example T1T2 Read-item(my-account) My-account := my-account Write-item(my-account) Read-item(other-account) Other-account := other-account Write-item(other-account) Read-item(my-account) My-account := my-account Write-item(my-account) TIME

IDA / ADIT 30 Checking if a schedule is serializable Checking if a schedule is serializable Create the following directed graph: 1. Create a node for each transaction in the schedule. 2. If transation T j executes a Read-item(X) after transaction T i executes a Write-item(X), then create an arch T i  T j. 3. If transation T j executes a Write-item(X) after transaction T i executes a Read-item(X), then create an arch T i  T j. 4. If transation T j executes a Write-item(X) after transaction T i executes a Write-item(X), then create an arch T i  T j. The schedule is serializable if and only if the graph above does not contain any directed cycle.

IDA / ADIT 31 Serializable schedule: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) other-account := other-account Read-item(other-account) Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME

IDA / ADIT 32 Non-serializable schedule: Example T1T2 Read-item(my-account) My-account := my-account Write-item(my-account) Read-item(other-account) Other-account := other-account Write-item(other-account) Read-item(my-account) My-account := my-account Write-item(my-account) TIME

IDA / ADIT 33 (Non-)Serializable schedule: Example T1 R(A) T2 W(C) T3 W(A) T4 R(A) W(B) R(C) W(A) W(D)

IDA / ADIT 34 (Non-)Serializable schedule: Example T1 R(A) T2 T3 W(A) T4 R(A) W(A)

IDA / ADIT 35 Can we make sure that we only get serializable schedules? Yes. Key question

IDA / ADIT 36 Locks: Definition Locks: Definition Locks control access to data. Every data item X has a lock in one of three possible states: o Read-locked (shared). o Write-locked (exclusive). o Unlocked. Read-lock(X): o If X is unlocked or read-locked, then it read-locks it, else it waits until it becomes unlocked or read-locked. Write-lock(X): o If X is unlocked, then it write-locks it, else it waits until X becomes unlocked. Unlock(X): o If X is write-locked, then it unlocks X. o If X is read-locked only once, then it unlocks X.

IDA / ADIT 37 Locks: Usage Locks: Usage 1.A transaction T must issue a Read-lock(X) or Write-lock(X) before executing a Read-item(X). 2.A transaction T must issue a Write-lock(X) before executing a Write-item(X). 3.A transaction T must issue a Unlock(X) after all the Read-item(X) and Write-item(X) in T have been executed. 4.A transaction T must not issue a Read-lock(X) or Write- lock(X) if it already has a read or write lock on X.

IDA / ADIT 38 Two-phase locking protocol: Definition A transaction follows the two-phase locking (2PL) protocol if all Read-lock() and Write-lock() operations come before the first Unlock() operation in the transaction. A transaction that follows the 2PL protocol has an expansion phase and a shrinking phase. If all the transactions in a schedule follow the 2PL protocol, then the schedule is serializable.

IDA / ADIT 39 Two-phase locking protocol: Example T1 Read-item(my-account1) Write-lock(my-account2) Unlock(my-account1) Read-item(my-account2) my-account2 := my-account Write-item(my-account2) Read-lock(my-account1) Unlock(my-account2) T2 Read-item(my-account1) Write-lock(my-account2) Unlock(my-account1) Read-item(my-account2) my-account2 := my-account Write-item(my-account2) Read-lock(my-account1) Unlock(my-account2)

IDA / ADIT 40 Two-phase locking protocol: Example T1 Read-item(my-account1) Write-lock(my-account2) Unlock(my-account1) Read-item(my-account2) my-account2 := my-account Write-item(my-account2) Read-lock(my-account1) Unlock(my-account2) T2 Read-item(my-account1) Write-lock(my-account2) Unlock(my-account1) Read-item(my-account2) my-account2 := my-account Write-item(my-account2) Read-lock(my-account1) Unlock(my-account2)

IDA / ADIT 41 Two-phase locking protocol: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) other-account := other-account Read-item(other-account) Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME

IDA / ADIT 42 Two-phase locking protocol: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) other-account := other-account Read-item(other-account) Write-item(other-account) Read-item(my-account) my-account := my-account Write-item(my-account) TIME Write-lock(my-account) Write-lock(other-account)???

IDA / ADIT 43 Two-phase locking protocol: Example T1 T2 Read-item(my-account) my-account := my-account Write-item(my-account) other-account := other-account Read-item(other-account) Write-item(other-account) TIME Write-lock(my-account) Read-item(my-account) my-account := my-account Write-item(my-account) Wite-lock(my-account) Write-lock(other-account) Unlock(my-account) Unlock(other-account) Unlock(my-account)

IDA / ADIT 44 Two-phase locking protocol: Example T1T2 Read-item(my-account) My-account := my-account Write-item(my-account) Read-item(other-account) Other-account := other-account Write-item(other-account) Read-item(my-account) My-account := my-account Write-item(my-account) TIME

IDA / ADIT 45 Two-phase locking protocol: Example T1 R(A) T2 W(C) T3 W(A) T4 R(A) W(B) R(C) W(A) W(D)

IDA / ADIT 46 Two-phase locking protocol: Example T1 T2 T3 read(x) x:=x+1 write(x) read(x) x:=x+1 write(x) read(x) x:=x+1 write(x) read(y) y:=y+1 write(y) read(y) y:=y+1 write(y)

IDA / ADIT 47 Two-phase locking protocol: Example T1 T2 T3 write(x) write(y)

IDA / ADIT 48 Deadlock Deadlock Two or more transactions wait for each other to unlock some data item. Deadlock prevention: o Conservative 2PL protocol: Wait until you can lock all the data to be used beforehand, o wait-die, o wound-wait, o no waiting, o cautious waiting, etc. Deadlock detection: o Wait-for graph, o timeouts, etc.

IDA / ADIT 49 Deadlock T1 Write-lock(my-account1) Write-lock(my-account2) T2 Write-lock(my-account2) Write-lock(my-account1) TIME

IDA / ADIT 50 Starvation Starvation A transaction is not executed for an indefinite period of time while other transactions are executed normally. Starvation prevention: o First-come-first-served waiting scheme, or o wait-die, o wound-wait, etc.