Introduction to Data Management CSE 344

Similar documents
Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

Database Systems CSE 414

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

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

CSE 344 MARCH 5 TH TRANSACTIONS

Announcements. Motivating Example. Transaction ROLLBACK. Motivating Example. CSE 444: Database Internals. Lab 2 extended until Monday

CSE 444: Database Internals. Lectures 13 Transaction Schedules

Database Systems CSE 414

Announcements. Transaction. Motivating Example. Motivating Example. Transactions. CSE 444: Database Internals

CSE 344 MARCH 21 ST TRANSACTIONS

Database Systems CSE 414

CSE 344 MARCH 9 TH TRANSACTIONS

CSE 444: Database Internals. Lectures Transactions

Motivating Example. Motivating Example. Transaction ROLLBACK. Transactions. CSE 444: Database Internals

Introduction to Data Management CSE 414

CSE 344 MARCH 25 TH ISOLATION

L i (A) = transaction T i acquires lock for element A. U i (A) = transaction T i releases lock for element A

Database Management Systems CSEP 544. Lecture 9: Transactions and Recovery

CSE544 Transac-ons: Concurrency Control. Lectures #5-6 Thursday, January 20, 2011 Tuesday, January 25, 2011

Database Systems CSE 414

Lecture 7: Transactions Concurrency Control

Transaction Processing: Concurrency Control ACID. Transaction in SQL. CPS 216 Advanced Database Systems. (Implicit beginning of transaction)

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

Lecture 5: Transactions Concurrency Control. Wednesday October 26 th, 2011

Transaction Management & Concurrency Control. CS 377: Database Systems

Database Management Systems

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

Transaction Processing. Introduction to Databases CompSci 316 Fall 2018

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

CS122 Lecture 15 Winter Term,

Concurrency control 12/1/17

Chapter 22. Transaction Management

Example: Transfer Euro 50 from A to B

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

Database design and implementation CMPSCI 645. Lectures 18: Transactions and Concurrency

Introduction to Database Systems CSE 444. Transactions. Lecture 14: Transactions in SQL. Why Do We Need Transactions. Dirty Reads.

Topics in Reliable Distributed Systems

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

Lecture 5: Transactions in SQL. Tuesday, February 6, 2007

CSC 261/461 Database Systems Lecture 24

Page 1. Goals of Today s Lecture" Two Key Questions" Goals of Transaction Scheduling"

Transaction Management. Chapter 14

CHAPTER: TRANSACTIONS

Lecture 21. Lecture 21: Concurrency & Locking

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

Transactions and Concurrency Control

Lecture 14: Transactions in SQL. Friday, April 27, 2007

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

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

Database Management Systems 2010/11

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

Introduction to Data Management CSE 344

Database Systems. Announcement

Lecture 20 Transactions

Concurrency Control & Recovery

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

CS 5614: Transaction Processing 121. Transaction = Unit of Work Recall ACID Properties (from Module 1)

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

Defining properties of transactions

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

DB2 Lecture 10 Concurrency Control

Overview. Introduction to Transaction Management ACID. Transactions

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

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

CSE 530A ACID. Washington University Fall 2013

Page 1. Goals of Todayʼs Lecture" Two Key Questions" Goals of Transaction Scheduling"

TRANSACTION MANAGEMENT

Overview of Transaction Management

The transaction. Defining properties of transactions. Failures in complex systems propagate. Concurrency Control, Locking, and Recovery

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

Lectures 8 & 9. Lectures 7 & 8: Transactions

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

Introduction to Transaction Management

Transaction Concept. Chapter 15: Transactions. Example of Fund Transfer. ACID Properties. Example of Fund Transfer (Cont.)

Transaction Concept. Two main issues to deal with:

Database transactions

Transactions and Concurrency Control. Dr. Philip Cannata

Roadmap of This Lecture

Chapter 6 Distributed Concurrency Control

Chapter 13: Transactions

Database System Concepts

CS322: Database Systems Transactions

Advanced Databases (SE487) Prince Sultan University College of Computer and Information Sciences. Dr. Anis Koubaa. Spring 2014

Distributed Data Management Transactions

Distributed Data Analytics Transactions

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

Chapter 14: Transactions

Intro to Transactions

SQL: Transactions. Introduction to Databases CompSci 316 Fall 2017

Introduction to Data Management. Lecture #24 (Transactions)

Introduction to Transaction Management

Chapter 14: Transactions

Transaction Overview and Concurrency Control

Database System Concepts

SQL: Transactions. Announcements (October 2) Transactions. CPS 116 Introduction to Database Systems. Project milestone #1 due in 1½ weeks

COURSE 1. Database Management Systems

Chapter 9: Transactions

In This Lecture. Transactions and Recovery. Transactions. Transactions. Isolation and Durability. Atomicity and Consistency. Transactions Recovery

Transcription:

Introduction to Data Management CSE 344 Lecture 21: More Transactions CSE 344 Fall 2015 1

Announcements Webquiz 7 is due before Thanksgiving HW7: Some Java programming required Plus connection to SQL Azure Due Tuesday shortly after Thanksgiving CSE 344 Fall 2015 2

Outline Serial and Serializable Schedules (18.1) Conflict Serializability (18.2) Locks (18.3) CSE 344 Fall 2015 3

Review: Transactions Problem: An application must perform several writes and reads to the database, as a unit Solution: multiple actions of the application are bundled into one unit called a Transaction Turing awards to database researchers Charles Bachman 1973 for CODASYL Edgar Codd 1981 for relational databases Jim Gray 1998 for transactions CSE 344 Fall 2015 4

Review: TXNs in SQL BEGIN TRANSACTION [SQL statements] COMMIT or ROLLBACK (=ABORT) [single SQL statement] If BEGIN missing, then TXN consists of a single instruction CSE 344 Fall 2015 5

Review: ACID Atomic State shows either all the effects of txn, or none of them Consistent Txn moves from a state where integrity holds, to another where integrity holds Isolated Effect of txns is the same as txns running one after another (i.e., looks like batch mode) Durable Once a txn has committed, its effects remain in the database CSE 344 Fall 2015 6

Isolation: The Problem Multiple transactions are running concurrently T 1, T 2, They read/write some common elements A 1, A 2, How can we prevent unwanted interference? The SCHEDULER is responsible for that CSE 344 Fall 2015 7

Schedules A schedule is a sequence of interleaved actions from all transactions CSE 344 Fall 2015 8

Serial Schedule A serial schedule is one in which transactions are executed one after the other, in some sequential order Fact: nothing can go wrong if the system executes transactions serially But database systems don t do that because we need better performance CSE 344 Fall 2015 9

Example A and B are elements in the database t and s are variables in txn source code 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) t := t+100 s := s*2 WRITE(B,t) WRITE(B,s) CSE 344 Fall 2015 10

A Serial Schedule Time T1 READ(A, t) t := t+100 WRITE(A, t) READ(B, t) t := t+100 WRITE(B,t) T2 READ(A,s) s := s*2 WRITE(A,s) READ(B,s) s := s*2 WRITE(B,s) CSE 344 Fall 2015 11

Another Serial Schedule Time T1 T2 READ(A,s) s := s*2 WRITE(A,s) READ(B,s) s := s*2 WRITE(B,s) READ(A, t) t := t+100 WRITE(A, t) READ(B, t) t := t+100 WRITE(B,t) CSE 344 Fall 2015 12

Serializable Schedule A schedule is serializable if it is equivalent to a serial schedule CSE 344 Fall 2015 13

A Serializable Schedule T1 READ(A, t) t := t+100 WRITE(A, t) READ(B, t) t := t+100 WRITE(B,t) This is a serializable schedule. This is NOT a serial schedule T2 READ(A,s) s := s*2 WRITE(A,s) READ(B,s) s := s*2 WRITE(B,s) CSE 344 Fall 2015 14

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

How do We Know if a Schedule is Serializable? Notation T 1 : r 1 (A); w 1 (A); r 1 (B); w 1 (B) T 2 : r 2 (A); w 2 (A); r 2 (B); w 2 (B) Key Idea: Focus on conflicting operations CSE 344 Fall 2015 16

Conflicts Write-Read WR Read-Write RW Write-Write WW CSE 344 Fall 2015 17

Conflict Serializability Conflicts: Two actions by same transaction T i : r i (X); w i (Y) Two writes by T i, T j to same element w i (X); w j (X) w i (X); r j (X) Read/write by T i, T j to same element r i (X); w j (X) CSE 344 Fall 2015 18

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 Every conflict-serializable schedule is serializable A serializable schedule may not necessarily be conflict-serializable CSE 344 Fall 2015 19

Conflict Serializability Example: r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B) CSE 344 Fall 2015 20

Example: Conflict Serializability r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 1 (B); w 1 (B); r 2 (A); w 2 (A); r 2 (B); w 2 (B) CSE 344 Fall 2015 21

Example: Conflict Serializability r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 1 (B); w 1 (B); r 2 (A); w 2 (A); r 2 (B); w 2 (B) CSE 344 Fall 2015 22

Example: Conflict Serializability r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 2 (A); r 1 (B); w 2 (A); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 1 (B); w 1 (B); r 2 (A); w 2 (A); r 2 (B); w 2 (B) CSE 344 Fall 2015 23

Example: Conflict Serializability r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 2 (A); r 1 (B); w 2 (A); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 1 (B); r 2 (A); w 2 (A); w 1 (B); r 2 (B); w 2 (B). r 1 (A); w 1 (A); r 1 (B); w 1 (B); r 2 (A); w 2 (A); r 2 (B); w 2 (B) CSE 344 Fall 2015 24

Testing for Conflict-Serializability Precedence graph: A node for each transaction T i, An edge from T i to T j whenever an action in T i conflicts with, and comes before an action in T j The schedule is serializable iff the precedence graph is acyclic CSE 344 Fall 2015 25

Example 1 r 2 (A); r 1 (B); w 2 (A); r 3 (A); w 1 (B); w 3 (A); r 2 (B); w 2 (B) 1 2 3 CSE 344 Fall 2015 26

Example 1 r 2 (A); r 1 (B); w 2 (A); r 3 (A); w 1 (B); w 3 (A); r 2 (B); w 2 (B) B A 1 2 3 This schedule is conflict-serializable CSE 344 Fall 2015 27

Example 2 r 2 (A); r 1 (B); w 2 (A); r 2 (B); r 3 (A); w 1 (B); w 3 (A); w 2 (B) 1 2 3 CSE 344 Fall 2015 28

Example 2 r 2 (A); r 1 (B); w 2 (A); r 2 (B); r 3 (A); w 1 (B); w 3 (A); w 2 (B) B 1 A 2 B 3 This schedule is NOT conflict-serializable CSE 344 Fall 2015 29

Scheduler Scheduler = is the module that schedules the transaction s actions, ensuring serializability Also called Concurrency Control Manager We discuss next how a scheduler may be implemented CSE 344 Fall 2015 30

Implementing a Scheduler Major differences between database vendors Locking Scheduler Aka pessimistic concurrency control SQLite, SQL Server, DB2 Multiversion Concurrency Control (MVCC) Aka optimistic concurrency control Postgres, Oracle We discuss only locking in 344 CSE 344 Fall 2015 31

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) By using locks scheduler CSE 344 ensures Fall 2015 conflict-serializability 32

What Data Elements are Locked? Major differences between vendors: Lock on the entire database SQLite Lock on individual records SQL Server, DB2, etc CSE 344 Fall 2015 33

Let s Study SQLite First SQLite is very simple More info: http://www.sqlite.org/atomiccommit.html Lock types READ LOCK (to read) RESERVED LOCK (to write) PENDING LOCK (wants to commit) EXCLUSIVE LOCK (to commit) CSE 344 Fall 2015 34

SQLite Step 1: when a transaction begins Acquire a READ LOCK (aka "SHARED" lock) All these transactions may read happily They all read data from the database file If the transaction commits without writing anything, then it simply releases the lock CSE 344 Fall 2015 35

SQLite Step 2: when one transaction wants to write Acquire a RESERVED LOCK May coexists with many READ LOCKs Writer TXN may write; these updates are only in main memory; others don't see the updates Reader TXN continue to read from the file New readers accepted No other TXN is allowed a RESERVED LOCK CSE 344 Fall 2015 36

SQLite Step 3: when writer transaction wants to commit, it needs exclusive lock, which can t coexists with read locks Acquire a PENDING LOCK May coexists with old READ LOCKs No new READ LOCKS are accepted Wait for all read locks to be released Why not write to disk right now? CSE 344 Fall 2015 37

SQLite Step 4: when all read locks have been released Acquire the EXCLUSIVE LOCK Nobody can touch the database now All updates are written permanently to the database file Release the lock and COMMIT CSE 344 Fall 2015 38

SQLite begin transaction first write commit requested no more read locks None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE LOCK commit commit executed CSE 344 Fall 2015 39

SQLite Demo create table r(a int, b int); insert into r values (1,10); insert into r values (2,20); insert into r values (3,30); CSE 344 Fall 2015 40

Demonstrating Locking in SQLite T1: begin transaction; select * from r; -- T1 has a READ LOCK T2: begin transaction; select * from r; -- T2 has a READ LOCK CSE 344 Fall 2015 41

Demonstrating Locking in SQLite T1: update r set b=11 where a=1; -- T1 has a RESERVED LOCK T2: update r set b=21 where a=2; -- T2 asked for a RESERVED LOCK: DENIED CSE 344 Fall 2015 42

Demonstrating Locking in SQLite T3: begin transaction; select * from r; commit; -- everything works fine, could obtain READ LOCK CSE 344 Fall 2015 43

Demonstrating Locking in SQLite T1: commit; -- SQL error: database is locked -- T1 asked for PENDING LOCK -- GRANTED -- T1 asked for EXCLUSIVE LOCK -- DENIED CSE 344 Fall 2015 44

Demonstrating Locking in SQLite T3': begin transaction; select * from r; -- T3 asked for READ LOCK-- DENIED (due to T1) T2: commit; -- releases the last READ LOCK; T1 can commit CSE 344 Fall 2015 45

Appendix / Review: Some Famous Anomalies What could go wrong if we didn t have concurrency control: Dirty reads (including inconsistent reads) Unrepeatable reads Lost updates Many other things can go wrong too CSE 344 Fall 2015 46

Dirty Reads Write-Read Conflict T 1 : WRITE(A) T 2 : READ(A) T 1 : ABORT CSE 344 Fall 2015 47

Inconsistent Read Write-Read Conflict T 1 : A := 20; B := 20; T 1 : WRITE(A) T 1 : WRITE(B) T 2 : READ(A); T 2 : READ(B); CSE 344 Fall 2015 48

Unrepeatable Read Read-Write Conflict T 1 : WRITE(A) T 2 : READ(A); T 2 : READ(A); CSE 344 Fall 2015 49

Lost Update Write-Write Conflict T 1 : READ(A) T 1 : A := A+5 T 1 : WRITE(A) T 2 : READ(A); T 2 : A := A*1.3 T 2 : WRITE(A); CSE 344 Fall 2015 50