Database Management System

Similar documents
TRANSACTION MANAGEMENT

Intro to Transaction Management

Transaction Management and Concurrency Control. Chapter 16, 17

Introduction to Transaction Management

Overview of Transaction Management

Transaction Management & Concurrency Control. CS 377: Database Systems

Transaction Management: Introduction (Chap. 16)

Transaction Management Overview. Transactions. Concurrency in a DBMS. Chapter 16

Database Systems. Announcement

Intro to Transactions

Transaction Management: Concurrency Control

Introduction to Data Management. Lecture #24 (Transactions)

What are Transactions? Transaction Management: Introduction (Chap. 16) Major Example: the web app. Concurrent Execution. Web app in execution (CS636)

Concurrency Control & Recovery

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

Lectures 8 & 9. Lectures 7 & 8: Transactions

Introduction to Data Management. Lecture #18 (Transactions)

CSC 261/461 Database Systems Lecture 21 and 22. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

Goal of Concurrency Control. Concurrency Control. Example. Solution 1. Solution 2. Solution 3

Concurrency Control & Recovery

CPSC 421 Database Management Systems. Lecture 19: Physical Database Design Concurrency Control and Recovery

Databases - Transactions

Introduction to Transaction Management

Transaction Management Overview

COURSE 1. Database Management Systems

Overview. Introduction to Transaction Management ACID. Transactions

Lecture 12. Lecture 12: The IO Model & External Sorting

Lecture 21. Lecture 21: Concurrency & Locking

Transactions & Update Correctness. April 11, 2018

Slides Courtesy of R. Ramakrishnan and J. Gehrke 2. v Concurrent execution of queries for improved performance.

CSE 344 MARCH 5 TH TRANSACTIONS

CSC 261/461 Database Systems Lecture 24

Intro to DB CHAPTER 15 TRANSACTION MNGMNT

CMPT 354: Database System I. Lecture 11. Transaction Management

Transactions. Transaction. Execution of a user program in a DBMS.

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Last Class. Last Class. Faloutsos/Pavlo CMU /615

Including Aborts in Serializability. Conflict Serializable Schedules. Recall Conflicts. Conflict Equivalent

Review. Review. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Lecture #21: Concurrency Control (R&G ch.

Databases - Transactions II. (GF Royle, N Spadaccini ) Databases - Transactions II 1 / 22

Chapter 22. Transaction Management

Distributed Transaction Management. Distributed Database System

Introduction to Data Management CSE 344

Database Management System

Introduction to Data Management CSE 344

Conflict Equivalent. Conflict Serializability. Example 1. Precedence Graph Test Every conflict serializable schedule is serializable

Transactions. 1. Transactions. Goals for this lecture. Today s Lecture

DB2 Lecture 10 Concurrency Control

Page 1. CS194-3/CS16x Introduction to Systems. Lecture 8. Database concurrency control, Serializability, conflict serializability, 2PL and strict 2PL

References. Transaction Management. Database Administration and Tuning 2012/2013. Chpt 14 Silberchatz Chpt 16 Raghu

Database Management Systems 2010/11

CSE 444: Database Internals. Lectures 13 Transaction Schedules

Transaction Processing Concepts and Theory. Truong Tuan Anh CSE-HCMUT

CSE 444: Database Internals. Lectures Transactions

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

Page 1. Goals of Today s Lecture. The ACID properties of Transactions. Transactions

Transactions. ACID Properties of Transactions. Atomicity - all or nothing property - Fully performed or not at all

CS352 Lecture - The Transaction Concept

Problems Caused by Failures

Concurrency Control. Chapter 17. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

T ransaction Management 4/23/2018 1

COSC344 Database Theory and Applications. Lecture 21 Transactions

CS 5614: (Big) Data Management Systems. B. Aditya Prakash Lecture #6: Transac/ons 1: Intro. to ACID

Transactions Processing (i)

Transactions and Concurrency Control

doc. RNDr. Tomáš Skopal, Ph.D.

h p:// Authors: Tomáš Skopal, Irena Holubová Lecturer: Mar n Svoboda, mar

Transaction Processing. Introduction to Databases CompSci 316 Fall 2018

Database Management System

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #17: Transac0ons 1: Intro. to ACID

Concurrency Control 1

Concurrency Control. R &G - Chapter 19

CSE 344 MARCH 25 TH ISOLATION

Transactions. Concurrency. Consistency ACID. Modeling transactions. Transactions and Concurrency Control

Transaction Processing: Concurrency Control. Announcements (April 26) Transactions. CPS 216 Advanced Database Systems

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #17: Transac0ons 2: 2PL and Deadlocks

Lock-based Concurrency Control

Page 1. Goals for Today" What is a Database " Key Concept: Structured Data" CS162 Operating Systems and Systems Programming Lecture 13.

Roadmap of This Lecture

Database System Concepts

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 17-1

CHAPTER: TRANSACTIONS

Introduction to Transaction Processing Concepts and Theory

CSE 344 MARCH 9 TH TRANSACTIONS

Database Technology. Topic 8: Introduction to Transaction Processing

Transactions. Lecture 8. Transactions. ACID Properties. Transaction Concept. Example of Fund Transfer. Example of Fund Transfer (Cont.

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI

Transactions. Juliana Freire. Some slides adapted from L. Delcambre, R. Ramakrishnan, G. Lindstrom, J. Ullman and Silberschatz, Korth and Sudarshan

Database Management Systems

Lock Granularity and Consistency Levels (Lecture 7, cs262a) Ali Ghodsi and Ion Stoica, UC Berkeley February 7, 2018

DATABASE DESIGN I - 1DL300

TRANSACTION PROCESSING CONCEPTS

) Intel)(TX)memory):) Transac'onal) Synchroniza'on) Extensions)(TSX))) Transac'ons)

TRANSACTION PROPERTIES

5/17/17. Announcements. Review: Transactions. Outline. Review: TXNs in SQL. Review: ACID. Database Systems CSE 414.

CSE 344 MARCH 21 ST TRANSACTIONS

CS5412: TRANSACTIONS (I)

Concurrency. Consider two ATMs running in parallel. We need a concurrency manager. r1[x] x:=x-250 r2[x] x:=x-250 w[x] commit w[x] commit

CAS CS 460/660 Introduction to Database Systems. Transactions and Concurrency Control 1.1

Database Systems CSE 414

CPS352 Lecture - The Transaction Concept

Transcription:

Database Management System Lecture 9 Transaction, Concurrency Control * Some materials adapted from R. Ramakrishnan, J. Gehrke and Shawn Bowers

Basic Database Architecture Database Management System 2

Concurrency Control and Recovery Transactions A way to define a single all or nothing set of SQL actions Based on a set of properties (the ACID properties) Enable concurrency The basis for crash recovery Database Management System 3

Transactions A transaction is a set of SQL statements (that modify a database) chosen by a user Transfer $100 from one account to another (MySQL): START TRANSACTION; SELECT @A1 := balance FROM Account WHERE acctno=500; UPDATE Account SET balance := @A1+100 WHERE acctno=500; SELECT @A2 := balance FROM Account WHERE acctno=501; UPDATE Account SET balance := @A2-100 WHERE acctno=501; COMMIT; BEGIN TRANSACTION READ balance from account 500 ADD $100 to balance of account 500 WRITE new balance to account 500 READ balance in account 501 VERIFY balance to see if it contains at least $100 ABORT if balance is less than $100 SUBTRACT $100 from balance of account 501 WRITE new balance to account 501 COMMIT TRANSACTION Database Management System 4

Transactions User (application developer) must Begin transaction Read, write, and modify statements intermixed with other programming language statements (e.g., verify) Plus either Commit to indicate successful completion or Abort to indicate that the the transaction should be rolled back... i.e., erase previous steps of transaction To ensure database stays in a consistent/correct state The DBMS and programmer must guarantee 4 properties of transactions These are called the ACID properties Database Management System 5

ACID Properties Atomicity Consistency Isolation Durability Database Management System 6

ACID Properties Atomicity A transaction happens in its entirety or not at all What if the OS crashed half-way through the transaction... after $100 was removed from the first account? The recovery manager of the DBMS must assure that the $100 is deposited back to the first account Often called roll back Database Management System 7

ACID Properties Consistency If the DB starts in a consistent state, the transaction will transform it into a consistent state The notion of consistency is specific to the application constraints (defined by the user) Thus the programmer must ensure transactions are consistent The DBMS ensures the transaction is atomic E.g., what if the transaction only deposited $100? Probably not consistent according to our example application Database Management System 8

ACID Properties Isolation Each transaction is isolated from other transactions... DB state is as if each transaction executed by itself What if an another transaction computed the total bank balance after $100 was removed from the first account? The concurrency control subsystem must ensure that all transactions run in isolation (i.e., don t mess up other transactions) unless the programmer chooses a less strict level of isolation similar to concurrency control in operating systems Database Management System 9

ACID Properties Durability If a transaction commits, its changes to the DB state persist (changes are permanent) What if after the commit the OS crashed before the deposit was written to disk? The recovery manager must assure that the deposit was at least logged (e.g., to make the DB consistent) Database Management System 10

Concurrency Why is concurrency important? Better utilization of resources E.g., while one user/transaction is reading the disk, another can be using the CPU or reading another disk Results in better throughput and response time Many applications require it (for performance) Database Management System 11

Concurrency We ll look at concurrency in terms of isolation of transactions Isolation is a problem when multiple transactions are running, using the same data, and operations are interleaved Isolation ensured by the concurrency control subsystem This should be familiar if you ve taken the OS class... Database Management System 12

Serial Schedules Consider these transactions: Deposit to A and withdraw from B T1: BEGIN A = A + 100; B = B 100; END Compute the balance of A and B T2: BEGIN C = A + B; END Apply interest to A and B T3: BEGIN A = 1.06 * A; B = 1.06 * A; END A schedule is an interleaving of the actions of the transactions so that each transaction s order is preserved A schedule of transactions is serial if its transactions occur consecutively, one after another Database Management System 13

Serial Schedules Which of these is a schedule? Which is serial? S1 T1 A=A+100 B=B-100 S3 T1 A=A+100 B=B-100 Serial Schedule! T3 A=1.06*A B=1.06*B Non-serial Schedule! T2 C=A+B S2 T1 A=A+100 B=B-100 S4 T1 B=B-100 A=A+100 Non-serial Schedule! T3 Not a Schedule! A=1.06*A B=1.06*B T3 C=A+B Database Management System 14

Allowable Concurrency What is wrong with S3? It does not give the same result as any serial schedule The DBMS should Allow serial schedules like S1 Forbid interleaved schedules like S3 But what about S2? Note that it gives the same result as S1! The DBMS should also allow this schedule! If the DBMS only allows serial schedules, then it becomes a batch system (where is the concurrency?) Database Management System 15

Serializable Schedules A schedule is serializable if its effect on the DB is the same as the effect on some serial schedule Serial schedules are always serializable S2 is serializable, but S3 is not Serializability is the same as the isolation condition The goal of the concurrency control subsystem is to ensure serializability Database Management System 16

Schedules as Reads and Writes An expression A = 1.06 * A means Read A from disk Set A equal to 1.06 *A Write A to disk Only the read and write to disk matter to the DBMS! We ll use the notation... R(A) for Read A W(A) for Write A Database Management System 17

Schedules as Reads and Writes These are equal (from DBMS/concurrency perspective) S2 S2 T1 T3 T1 T3 A=A+100 B=B-100 A=1.06*A B=1.06*B R(A), W(A) R(B), W(B) R(A), W(A) R(B), W(B) Database Management System 18

Conflict Serializability S2 has a special structure that makes it possible to show that it is serializable... Two actions are nonconflicting if they are in different transactions and either they Access different data times (resources) Or both are reads If nonconflicting actions are commuted then the new schedule gives the same result Database Management System 19

Conflict Serializability wo schedules are conflict equivalent if One can be transformed into the other by commuting (swapping) nonconflicting actions A schedule is conflict serializable if it is conflict equivalent to at least one serial schedule Thus, every conflict serializable schedule is serializable! (... but not necessarily the other way around) Database Management System 20

Serializability Nonconflicting shown in Red S2 S2 T1 T3 T1 T3 R(A), W(A) R(B), W(B) R(A), W(A) R(B), W(B) commute R(A), W(A) R(B), W(B) R(A), W(A) R(B), W(B) Ais used in T3, B in T1 This is a serial schedule! This means S2 is conflict serializable (... and thus is serializable) Database Management System 21

Precedence Graphs Verifying conflict serializability is tedious There is an easier way! T1 T2 Precedence graphs One node per transaction Edge from T i to T j if an action in T i occurs before an action in T j and the actions conflict Theorem A schedule is conflict serializable if an only if its precedence graph is acyclic Database Management System 22

Exercise With a partner, draw the precedence graphs for the previous schedules (S1-S3) You ll first have to convert them to R s and W s S1 T1 A=A+100 R(A) B=B-100 W(A) R(B) W(B) T3 A=1.06*A B=1.06*B R(A) W(A) R(B) W(B) S3 T1 A=A+100 R(A) W(A) B=B-100 R(B) W(B) SERIAL NOT SERIAL NOT SERIAL T2 C=A+B R(A) W(A) R(B) W(B) S4 T1 B=B-100 R(A) A=A+100 W(A) R(B) W(B) T3 C=A+B R(A) R(B) W(C) Database Management System 23

Exercise With a partner, draw the precedence graphs for the previous schedules (S1-S3) S5 T1 T2 T3 R(A) W(A) R(B) W(B) W(A) R(B) S6 T1 T2 T3 R(A) W(A) W(B) W(A) S6 is actually serializable: whichever transaction writes A last wins Serializable but not Conflict Serializable T1 T2 T1 T2 T2 Not Serializable T2 Serializable, but.. Database Management System 24

Relationships Serializable Conflict Serializable Serial Acyclic Precedence Graph Database Management System 25

Serializability in Practice Precedence graphs give us a simple way to prove that a schedule is (conflict) serializable But they do not work for all schedules In theory, this could be used by a DBMS to check for serializability... In practice A DBMS is not presented with schedules... it only sees a stream of transactions Instead, locking is used to achieve isolation Database Management System 26

Locking Transactions must obtain a lock before reading or updating (writing) data Two kinds of locks: Shared (S) locks Exclusive (X) locks To read a record you MUST get an S lock To write (modify/delete) a record you MUST get an X lock Lock information is maintained by a lock manager Database Management System 27

How Locks Work If an object has an S (shared) lock new transactions can obtain S (shared) locks but not X (exclusive) locks If an object has an X lock no other transaction can obtain any lock on that object If a transaction cannot obtain a lock It is blocked (i.e., waits in a queue) Database Management System 28

Strict Two Phase Locking Protocol (Strict 2PL) In Strict 2PL Transaction T obtains (S and X) locks gradually, as needed T holds all locks until end of transaction (commit/abort) This guarantees serializability! Still permits interleaved schedules But can lead to deadlock # of Locks held by a transaction T 4 All locks are released 3 at the end, upon commit or abort 2 1 0 time Database Management System 29

Strict Two Phase Locking Protocol (Strict 2PL) Examples S6 T1 T2 T3 R(A) W(A) W(A) W(A) S6 (S2PL) T1 T2 T3 X(A) R(A) W(A) Commit X(A) W(A) Commit X(A) W(A) Commit S7 (S2PL) T1 X(B) X(B) X(C) W(C) Commit T2 X(A) W(A) X(C) W(C) Commit Database Management System 30

Strict Two Phase Locking Protocol (Strict 2PL) Yet another example T1:W(A),W(B) T2:W(B),W(A) S7 (S2PL) T1 X(A) W(A) T2 X(B) W(B) DEADLOCK!! Waiting for X(B) Waiting for X(A) Database Management System 31

Deadlocks in DBMS What is a Deadlock? A cycle of transactions, e.g.,t1... Tn where each Ti is waiting for Ti-1 to release a lock! Causes these transactions to sleep/wait forever Deadlocks can occur in strict 2PL Solutions Always access resources in the same order (impractical) The DBMS will typically detect deadlocks... and then abort the transaction (it thinks) has used the least resources Database Management System 32