Transaction Management: Concurrency Control CS634 Class 19, Apr 14, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.

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.
Transaction Management and Concurrency Control
Crash Recovery John Ortiz. Lecture 22Crash Recovery2 Review: The ACID properties  Atomicity: All actions in the transaction happen, or none happens 
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
Transaction Management: Crash Recovery, part 2 CS634 Class 21, Apr 23, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Concurrency Control Amol Deshpande CMSC424. Approach, Assumptions etc.. Approach  Guarantee conflict-serializability by allowing certain types of concurrency.
4/16/20151 PSU’s CS Concurrency Control and Recovery (only for DBs with updates…..!)- Review  Concurrency Control  Transaction  ACID  Isolation.
Topic 6.3: Transactions and Concurrency Control Hari Uday.
Serializable Isolation for Snapshot Databases Michael J. Cahill, Uwe Röhm, and Alan D. Fekete University of Sydney ACM Transactions on Database Systems.
Concurrency Control II. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Physical Design.
Concurrency Control Part 2 R&G - Chapter 17 The sequel was far better than the original! -- Nobody.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Transaction Management: Crash Recovery CS634 Class 20, Apr 16, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. –Because disk accesses are.
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.
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 Concurrency Control and Recovery Module 6, Lecture 1.
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.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Transaction. A transaction is an event which occurs on the database. Generally a transaction reads a value from the database or writes a value to the.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17 Modified by Donghui Zhang.
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.
Transaction Management: Concurrency Control CS634 Class 16, Apr 2, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
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.
Databases Illuminated
Lecture 12 Recoverability and failure. 2 Optimistic Techniques Based on assumption that conflict is rare and more efficient to let transactions proceed.
Concurrency Control R &G - Chapter 19. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses.
1 CS 430 Database Theory Winter 2005 Lecture 16: Inside a DBMS.
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 Concurrency Control II: Locking and Isolation Levels.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu.
Transaction Management: Concurrency Control CS634 Class 18, Apr 9, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
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.
1 Concurrency Control Chapter Conflict Serializable Schedules  Two schedules are conflict equivalent if:  Involve the same actions of the same.
XA Transactions.
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
Overview of Transaction Management
Transaction Management and Recovery, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
MULTIUSER DATABASES : Concurrency and Transaction Management.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
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
Transaction Management Overview
Transaction Management
Transaction Management Overview
Transaction Management Overview
On transactions, and Atomic Operations
Transaction Management
Transaction Management Overview
On transactions, and Atomic Operations
Transaction Management
Transaction Management Overview
CONCURRENCY Concurrency is the tendency for different tasks to happen at the same time in a system ( mostly interacting with each other ) .   Parallel.
Transaction Management Overview
Presentation transcript:

Transaction Management: Concurrency Control CS634 Class 19, Apr 14, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke

Multiple-Granularity Locks  Hard to decide what granularity to lock  tuples vs. pages vs. files  Shouldn’t have to decide!  Data containers are nested: Tuples Files Pages Database contains

New Lock Modes, Protocol  Allow transactions to lock at each level, but with a special protocol using new intention locks Before locking an item, must set intention locks on ancestors For unlock, go from specific to general (i.e., bottom-up). SIX mode: Like S & IX at the same time. -- ISIX -- IS IX      SX   S X      

Multiple Granularity Lock Protocol  Each transaction starts from the root of the hierarchy  To get S or IS lock on a node, must hold IS or IX on parent node  To get X or IX or SIX on a node, must hold IX or SIX on parent node.  Must release locks in bottom-up order

Examples  T1 scans R, and updates a few tuples:  T1 gets an SIX lock on R, then repeatedly gets an S lock on tuples of R, and occasionally upgrades to X on the tuples.  T2 uses an index to read only part of R:  T2 gets an IS lock on R, and repeatedly gets an S lock on tuples of R.  T3 reads all of R:  T3 gets an S lock on R.  OR, T3 could behave like T2; can use lock escalation to decide which. -- ISIX -- IS IX      SX   S X      

Transaction Support in SQL  A transaction is automatically started when user executes a statement or accesses the catalogs  Transaction is either committed (COMMIT) or aborted (ROLLBACK)  New in SQL-99: SAVEPOINT feature SAVEPOINT Actions … ROLLBACK TO SAVEPOINT  SAVEPOINT advantage vs sequence of transactions  Can roll back over multiple savepoints  Lower overhead: no new transaction initiated 6

Setting Transaction Properties in SQL  Access Mode  READ ONLY vs READ WRITE  Isolation Level (decreasing level of concurrency) 7 LevelDirty ReadUnrepeatable Read Phantom READ UNCOMMITTEDPossible READ COMMITTEDNoPossible REPEATABLE READNo Possible SERIALIZABLENo

Isolation Levels in Practice  Databases default to RC, read-committed, so many apps run that way, can have their read data changed, and phantoms  Web apps (JEE, anyway) have a hard time overriding RC, so most are running at RC  The 2PL locking scheme we studied was for RR, repeatable read: transaction takes long term read and write locks  Long term = until commit of that transaction

Read Committed (RC) Isolation  2PL can be modified for RC: take long-term write locks but not long term read locks  Reads are atomic as operations, but that’s it  Lost updates can happen in RC: system takes 2PC locks only for the write operations: R1(A)R2(A)W2(B)C2W1(B)C1 R1(A)R2(A)X2(B)W2(B)C2X1(B)W1(B)C1 (RC isolation)  Update statements are atomic, so that case of read-then- write is safe even at RC  Update T set A = A (safe at RC isolation)  Remember to use update when possible!

Syntax for SQL SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE SET TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY  Note:  READ UNCOMITTED cannot be READ WRITE 10

More on setting transaction properties Embedded SQL EXEC SQL SET TRANSACTION ISOLATION LEVEL SERIALIZABLE JDBC conn.setAutoCommit(false); conn.setTransactionIsolation (Connection.TRANSACTION_ISOLATION_SERIALIZABLE); 11

Aborting a Transaction  If a transaction Ti is aborted, all its actions have to be undone. Not only that, if Tj reads an object last written by Ti, Tj must be aborted as well!  Most systems avoid such cascading aborts by releasing a transaction’s locks only at commit time.  If Ti writes an object, Tj can read this only after Ti commits.  In order to undo the actions of an aborted transaction, the DBMS maintains a log in which every write is recorded. This mechanism is also used to recover from system crashes: all active Xacts at the time of the crash are undone when the system comes back up.

The Log  The following actions are recorded in the log:  Ti writes an object: the old value and the new value.  Log record must go to disk before the changed page!  Ti commits/aborts: a log record indicating this action.  Log records are chained together by Xact id, so it’s easy to undo a specific Xact.  Log is often duplexed and archived on stable storage.  All log related activities (and in fact, all CC related activities such as lock/unlock, dealing with deadlocks etc.) are handled transparently by the DBMS.

Snapshot Isolation (SI)  Multiversion Concurrency Control Mechanism (MVCC)  This means the database holds more than one value for a data item at the same time  Used in PostgreSQL (open source), Oracle, others  Readers never conflict with writers unlike traditional DBMS (e.g., IBM DB2)! Read-only transactions run fast.  Does not guarantee “real” serializability  But: ANSI “serializability” fulfilled, i.e., avoids anomalies in the ANSI table  Found in use at Microsoft in 1993, published as example of MVCC

Snapshot Isolation - Basic Idea:  Every transaction reads from its own snapshot (copy) of the database (will be created when the transaction starts, or reconstructed from the undo log).  Writes are collected into a writeset (WS), not visible to concurrent transactions.  Two transactions are considered to be concurrent if one starts (takes a snapshot) while the other is in progress.

First Committer Wins Rule of SI  At the commit time of a transaction its WS is compared to those of concurrent committed transactions.  If there is no conflict (overlapping), then the WS can be applied to stable storage and is visible to transactions that begin afterwards.  However, if there is a conflict with the WS of a concurrent, already committed transaction, then the transaction must be aborted.  That’s the “First Committer Wins Rule“  Actually Oracle uses first updater wins, basically same idea, but doesn’t require separate WS

Write Skew Anomaly of SI  In MVCC, data items need subscripts to say which version is being considered  Zero version: original database value  T1 writes new value of X, X 1  T2 writes new value of Y, Y 2  Write skew anomaly schedule: R 1 (X 0 ) R 2 (X 0 ) R 1 (Y 0 ) R 2 (Y 0,) W 1 (X 1 ) C 1 W 2 (Y 2 ) C 2  Writesets WS(T1) = {X}, WS(T2) = {Y}, do not overlap, so both commit.  So what’s wrong—where’s the anomaly?

Write Skew Anomaly of SI R 1 (X 0 ) R 2 (X 0 ) R 1 (Y 0 ) R 2 (Y 0,) W 1 (X 1 ) C 1 W 2 (Y 2 ) C 2  Scenario:  X = husband’s balance, orig 100,  Y = wife’s balance, orig 100.  Bank allows withdrawals up to combined balance  Rule: X + Y >= 0  Both withdraw 150, thinking OK, end up with -50 and -50.  Easy to make this happen in Oracle at “Serializable” isolation.  See conflicts, cycle in PG, can’t happen with full 2PL  Can happen with RC/locking

How can an Oracle app handle this?  If X+Y >= 0 is needed as a constraint, it can be “materialized” as sum in another column value.  Old program: R(X)R(X-spouse)W(X)C  New program: R(X)R(X-spouse) W(sum) W(X)C  So schedule will have W(sum) in both transactions, and sum will be in both Writesets, so second committer aborts.  Or, after the W(X), run a query for the sum and abort if it’s negative.

Oracle, Postgres: new failure to handle  Recall deadlock-abort handling: retry the aborted transaction  With SI, get "can't serialize access“  ORA-08177: can't serialize access for this transaction  Means another transaction won for a contended write  App handles this error like deadlock-abort: just retry transaction, up to a few times  This only happens when you set serializable isolation level

Other anomalies under SI  Oldest sailors example  Both concurrent transactions see original sailor data in snapshots, plus own updates  Updates are on different rows, so both commit  Neither sees the other’s update  So not serializable: one should see one update, other should see two updates.  Task Registry example:  Both concurrent transactions see original state with 6 hours available for Joe  Both insert new task for Joe  Inserts involve different rows, so both commit

Fixing the task registry  Following the idea of the simple write skew, we can materialize the constraint “workhours <= 8”  Add a workhours column to worker table  Old program:  if sum(hours-for-x)+newhours<=8  insert new task  New program:  if workhours-for-x + newhours <=8  { update worker set workhours = workhours + newhours…  insert new task  }

Fixing the Oldest sailor example  If the oldest sailor is important to the app, materialize it! Create table oldestsailor (rating int primary key, sid int)

Oracle Read Committed Isolation  READ COMMITTED is the default isolation level for both Oracle and PostgreSQL.  A new snapshot is taken for every issued SQL statement (every statement sees the latest committed values).  If a transaction T2 running in READ COMMITTED mode tries to update a row which was already updated by a concurrent transaction T1, then T2 gets blocked until T1 has either committed or aborted  Nearly same as 2PL/RC, though all reads occur effectively at the same time for the statement.

Transaction Management: Crash Recovery CS634 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke

ACID Properties Transaction Management must fulfill four requirements: 1. Atomicity: either all actions within a transaction are carried out, or none is  Only actions of committed transactions must be visible 2. Consistency: concurrent execution must leave DBMS in consistent state 3. Isolation: each transaction is protected from effects of other concurrent transactions  Net effect is that of some sequential execution 4. Durability: once a transaction commits, DBMS changes will persist  Conversely, if a transaction aborts/is aborted, there are no effects 26

Recovery Manager  Crash recovery  Ensure that atomicity is preserved if the system crashes while one or more transactions are still incomplete  Main idea is to keep a log of operations; every action is logged before its page updates reach disk (Write-Ahead Log or WAL)  The Recovery Manager guarantees Atomicity & Durability 27

Motivation  Atomicity:  Transactions may abort – must rollback their actions  Durability:  What if DBMS stops running – e.g., power failure? crash! Desired Behavior after system restarts: –T1, T2 & T3 should be durable –T4 & T5 should be aborted (effects not seen) T1 T2 T3 T4 T5

Assumptions  Concurrency control is in effect  Strict 2PL  Updates are happening “in place”  Data overwritten on (deleted from) the disk  A simple scheme is needed  A protocol that is too complex is difficult to implement  Performance is also an important issue