Transaction Management and Concurre cy Overview R & G Chapter 16-17 There are three side effects of acid. Enhanced long term memory, decreased short term.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Crash Recovery John Ortiz. Lecture 22Crash Recovery2 Review: The ACID properties  Atomicity: All actions in the transaction happen, or none happens 
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
Topic 6.3: Transactions and Concurrency Control Hari Uday.
CS 440 Database Management Systems Lecture 10: Transaction Management - Recovery 1.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Introduction to Database Systems1 Logging and Recovery CC Lecture 2.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. –Because disk accesses are.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 16 – Intro. to Transactions.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
Final Exam Review Last Lecture R&G - All Chapters Covered The end crowns all, And that old common arbitrator, Time, Will one day end it. William Shakespeare.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
Quick Review of May 1 material Concurrent Execution and Serializability –inconsistent concurrent schedules –transaction conflicts serializable == conflict.
1 Concurrency Control and Recovery Module 6, Lecture 1.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
1 Transaction Management Overview Yanlei Diao UMass Amherst March 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Final Exam Review Last Lecture R&G - All Chapters Covered The end crowns all, And that old common arbitrator, Time, Will one day end it. William Shakespeare.
CS194-3/CS16x Introduction to Systems Lecture 8 Database concurrency control, Serializability, conflict serializability, 2PL and strict 2PL September.
1 Lecture 08: Transaction management overview
Transaction Processing
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#20: Overview of Transaction Management.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Transaction Management Overview Chapter Transactions  A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Lecture 21 Ramakrishnan - Chapter 18.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Concurrency Control R &G - Chapter 19. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses.
Database Systems/COMP4910/Spring05/Melikyan1 Transaction Management Overview Unit 2 Chapter 16.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Concurrency Control in Database Operating Systems.
1 Transaction Processing Chapter Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data.
1 Concurrency Control II: Locking and Isolation Levels.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 136 Database Systems I SQL Modifications and Transactions.
Introduction to Database Systems1. 2 Basic Definitions Mini-world Some part of the real world about which data is stored in a database. Data Known facts.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#20: Overview of Transaction Management.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
Overview of Transaction Management
Transaction Management and Recovery, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
1 Database Systems ( 資料庫系統 ) December 27/28, 2006 Lecture 13 Merry Christmas & New Year.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 16 – Intro. to Transactions.
CS 440 Database Management Systems Concurrency Control 1.
MULTIUSER DATABASES : Concurrency and Transaction Management.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Database Systems ( 資料庫系統 ) December 20, 2004 Chapter 16 By Hao-hua Chu ( 朱浩華 )
1 Concurrency Control. 2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –While.
Transaction Management Overview
Database Systems (資料庫系統)
Transaction Management Overview
Transaction Management
Transaction Management Overview
Transaction Management Overview
Transaction Management Overview
Transaction Management Overview
Transaction Management
Transaction Management Overview
Chapter 10 Transaction Management and Concurrency Control
Transaction Management
Database Management systems Subject Code: 10CS54 Prepared By:
Transaction Management Overview
Transaction Management Overview
Transaction Management Overview
Presentation transcript:

Transaction Management and Concurre cy Overview R & G Chapter There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget the third. - Timothy Leary

Administrivia Homework 3 due today by end of class period Homework 4 available on class website –Due date: April 10 (after Spring Break) Midterm 2 is next class! Thu 3/22 –In class, covers lectures –Review will be held Wed 3/ pm 306 Soda Hall

Query Compiler query Execution Engine Logging/Recovery LOCK TABLE Concurrency Control Storage Manager BUFFER POOL BUFFERS Buffer Manager Schema Manager Data Definition DBMS: a set of cooperating software modules Transaction Manager transaction Components of a DBMS We are here

Correctness criteria: The ACID properties A A tomicity: All actions in the Xact happen, or none happen. C C onsistency: If each Xact is consistent, and the DB starts consistent, it ends up consistent. I I solation: Execution of one Xact is isolated from that of other Xacts. D D urability: If a Xact commits, its effects persist. We are here

Review - Definitions Transaction - a sequence of read and write operations (read(A), write(B), …) –DBMS’s abstract view of a user program Serial schedule: Schedule that does not interleave the actions of different transactions. Equivalent schedules: For any database state, the effect of executing the first schedule is identical to the effect of executing the second schedule. Serializable schedule: A schedule that is equivalent to some serial execution of the transactions. (Note: If each transaction preserves consistency, every serializable schedule preserves consistency. )

Reading Uncommitted Data (“WR”, dirty reads): –T2 reads a value A that T1 wrote but didn’t commit –e.g: T1 moves $100 from account B to account A T2 adds 6% interest to account A $ $ = $2066 total after T1 & T2 Anomalies with interleaved execution T1: R(A), W(A), R(B), W(B), Abort T2:R(A), W(A), C A=A+100=1100 A=1100 A=1100*1.06 =1166 B=1000 B= =900 = = > Bank is out $106 because T2 read T1’s A value before it was done! A=?B=1000 A=1166B=1000 A=1000 ->A=1166

Unrepeatable Reads (“RW” Conflicts): –T1 reads a value A that is then written by T2 –e.g. T1 and T2 place orders for books, and A represents the quantity in stock (1) Anomalies with interleaved execution T1:R(A), R(A), W(A) T2:R(A), W(A), C A=1 A=1A=0A=0 A=0A=-1 Hopefully T1 gets an error if there is an integrity constraint!

Overwriting Uncommitted Data (“WW”, lost update): –T2 overwrites a write by T1 –e.g. T1 assigns seat A to passenger 1 and seat B to passenger 2 T2 assigns seat A to passenger 3 and seat B to passenger 4 Anomalies with interleaved execution T1:W(A), W(B), C T2:W(A), W(B), C A=1 A=3B=4 A=3B=4 B=2 A=3B=2 -> Passenger 1’s partner is sitting with passenger 3!

How to prevent anomalies? Locks! Database allows “objects” to be locked –“object” might be entire database, file, page, tuple Two kinds of locks: –“Shared” or “Read” Lock No one else is allowed to write the object if you have this –“Exclusive” or “Write” Lock No one else is allowed to read or write the object

Locks are not enough If lock/unlock objects right away, anomalies are still possible –e.g. The unrepeatable read example T1:R(A), R(A), W(A) T2:R(A), W(A), C T1 obtain ShareLock(A) T2 obtain ExclusiveLockL(A) T1 release ShareLock(A) T2 release ExclusiveLock(A) T1 obtain ShareLock(A)

Locks are not enough Idea: Two Phase Locking –In a transaction, only acquire locks in one phase only release locks in a second phase once one lock has been released, can never acquire another lock during transaction Time #locks T1:R(A), R(A), W(A), …. T2: R(A),W(A), C T1 obtain ExclusiveLock(A) T2 waits to obtain ExclusiveLock(A) T1 release lock(A) T2 obtains ExclusiveLock(A)

Lock-Based Concurrency Control Two-phase Locking (2PL) Protocol: – Each Xact must obtain: a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing. – If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. – System can obtain these locks automatically – Two phases: acquiring locks, and releasing them No lock is ever acquired after one has been released “Growing phase” followed by “shrinking phase”. Lock Manager keeps track of request for locks and grants locks on database objects when they become available.

Strict 2PL 2PL allows only serializable schedules but is subjected to cascading aborts. Example: rollback of T1 requires rollback of T2! To avoid Cascading aborts, use Strict 2PL T2 read T1’s value of A, so it must also be aborted. T1 obtain ExclusiveLock(A) T1 release ExclusiveLock(A) T2 obtains ExclusiveLock(A) Abort T1: R(A), W(A), T2:R(A), W(A), R(B), W(B)

Strict 2PL (cont) Advantage: no other transaction even reads anything you write until you commit. –e.g a transaction will only read committed data. Disadvantage: transactions end up waiting. –e.g. inserts may lock a whole table Why? Because of Phantom problem Strict Two-phase Locking (Strict 2PL) Protocol: – – Same as 2PL, except: – – All locks held by a transaction are released only when the transaction completes #locksvs

The Phantom Problem Even Strict 2PL (on individual rows) will not assure serializability: Consider T1 – “Find oldest sailor with Rating = 1” T1 sees 2 different answers, even though it did no updates itself. TimeT1T2 1 Obtain Share Lock on all existing Sailors with rating = 1 2 Compute oldest sailor (age = 71) 3 Obtain Exclusive Lock on new Sailor tuple 4 Insert Sailor age 96 rating 1 5 Commit 6 Compute oldest sailor (age = 96)

Transactions in SQL A new transaction is started with first SQL statement issued by a user SELECT S.SID, R.BID FROM SAILORS S, RESERVES R WHERE S.SID = R.SID All statements from that point on appear in the same transaction UPDATE SAILORS SET RATING = RATING - 1 UPDATE RESERVES SET DATE = DATE+1 A user can use commands COMMIT or ROLLBACK to explicitly end a transaction and start a new one COMMIT Any new statements that follow will occur in a new transaction

Transactions in SQL Because of performance, some anomalies might be acceptable for some applications –Internet apps in particular! SQL 92 supports different “Isolation Levels” for a transaction (Lost Update not allowed at any level) No Serializable MaybeNo Repeatable Reads Maybe NoRead Committed Maybe Read Uncommitted Phantom Problem Unrepeatable Read Dirty Read Isolation Level

Aborting a Transaction (i.e., Rollback) If an xact Ti aborted, all actions must be undone. To undo actions of an aborted transaction, DBMS maintains log which records every write. Log is also used to recover from system crashes (which can occur as either hardware or software failure) –All active Xacts at time of crash are aborted when system comes back up.

The Database Log XID 1: REDO TID 1 REDO TID 1 REDO TID 2 REDO TID 2 COMMIT XID 2: REDO TID 3 REDO TID 3 … Buffer Data Page Log Data Pages Log Updates are logged in the database log. Log consists of “records” that are written sequentially. – –Typically chained together by Xact id – –Log is often archived on stable storage. – –And backed up to even more stable storage (like tape!)

The Database Log Write-Ahead Logging protocol –Log record must go to disk before the changed page! –All log records for a transaction (including its commit record) must be written to disk before the transaction is considered “Committed”. XID 1: REDO TID 1 REDO TID 1 REDO TID 2 REDO TID 2 COMMIT XID 2: REDO TID 3 REDO TID 3 … Buffer Data Page Log Data Pages Log Update TID 1 Update TID 1 Update TID 2 Update TID SQL: COMMIT XID1

The Log Log records are either UNDO or REDO records –Depends on the Buffer manager UNDO required if: Buffer mgr allows uncommitted data to overwrite stable version of committed data (STEAL buffer management). REDO required if: xact can commit before all its updates are on disk (NO FORCE buffer management). The following actions are recorded in the log: – if Ti writes an object, write a log record with: If UNDO required need “before image” IF REDO required need “after image”. – Ti commits/aborts: a log record indicating this action.

How Professor Roth made some traders very unhappy at market open… XID 1: REDO TID 1 REDO TID 1 REDO TID 2 REDO TID 2 COMMIT XID 2: REDO TID 3 REDO TID 3 … Log Log2 Log on disk can fill up quickly for active DBMS DBMS duplexes between two logs – –Writes DB log to one – –While archiving the other – –When DB log is full, reverse Must STOP all DBMS activity while you reverse Log1Log

How Professor Roth made some traders very unhappy at market open… XID 1: REDO TID 1 REDO TID 1 REDO TID 2 REDO TID 2 COMMIT XID 2: REDO TID 3 REDO TID 3 … Log Log2 Stock DB at market open is VERY busy We wanted to make sure there was an empty log just before market open I wrote a script to ‘switch’ logs 5 mins before market open If (not already writing a log) Then switch logs Write the old log to tape Except I forgot a comma, so it was more like this: Switch logs; write the old log to tape...and because the DB was writing out its log, the DB hung until it was done writing to tape! Log1 I’m waiting …

(Review) Goal: The ACID properties A A tomicity: All actions in the Xact happen, or none happen. C C onsistency: If each Xact is consistent, and the DB starts consistent, it ends up consistent. I I solation: Execution of one Xact is isolated from that of other Xacts. D D urability: If a Xact commits, its effects persist. What happens if system crashes between commit and flushing modified data to disk ?

Durability - Recovering From a Crash Three phases: – Analysis: Scan the log (forward from the most recent checkpoint) to identify all Xacts that were active at the time of the crash. – Redo: Redo updates as needed to ensure that all logged updates are in fact carried out and written to disk. – Undo: Undo writes of all Xacts that were active at the crash, working backwards in the log. At the end – all committed updates and only those updates are reflected in the database. Some care must be taken to handle the case of a crash occurring during the recovery process! D

Summary Concurrency control and recovery are among the most important functions provided by a DBMS. Concurrency control is automatic – System automatically inserts lock/unlock requests and schedules actions of different Xacts – Property ensured: resulting execution is equivalent to executing the Xacts one after the other in some order. Write-ahead logging (WAL) and the recovery protocol are used to: 1. undo the actions of aborted transactions, and 2. restore the system to a consistent state after a crash.