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

Similar documents
Transaction Management: Introduction (Chap. 16)

Intro to Transaction Management

Intro to Transactions

Overview of Transaction Management

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

Overview. Introduction to Transaction Management ACID. Transactions

Transaction Management and Concurrency Control. Chapter 16, 17

Database Systems. Announcement

Transaction Management Overview

Concurrency Control & Recovery

Transaction Management & Concurrency Control. CS 377: Database Systems

Concurrency Control & Recovery

Final Review. CS634 May 11, Slides based on Database Management Systems 3 rd ed, Ramakrishnan and Gehrke

COURSE 1. Database Management Systems

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

Introduction to Data Management. Lecture #18 (Transactions)

TRANSACTION MANAGEMENT

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

Introduction to Data Management. Lecture #24 (Transactions)

Database Management System

Transaction Management: Concurrency Control

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

CSE 190D Database System Implementation

Lectures 8 & 9. Lectures 7 & 8: Transactions

Lecture 21. Lecture 21: Concurrency & Locking

CSE 444: Database Internals. Lectures 13 Transaction Schedules

CSC 261/461 Database Systems Lecture 24

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

Phantom Problem. Phantom Problem. Phantom Problem. Phantom Problem R1(X1),R1(X2),W2(X3),R1(X1),R1(X2),R1(X3) R1(X1),R1(X2),W2(X3),R1(X1),R1(X2),R1(X3)

CSE 444: Database Internals. Lectures Transactions

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

Transaction Management: Concurrency Control, part 2

Locking for B+ Trees. Transaction Management: Concurrency Control, part 2. Locking for B+ Trees (contd.) Locking vs. Latching

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

Databases - Transactions

Introduction to Data Management. Lecture #26 (Transactions, cont.)

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

Lock-based Concurrency Control

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

Transactions Processing (i)

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

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

Chapter 20 Introduction to Transaction Processing Concepts and Theory

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

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

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

ACID Properties. Transaction Management: Crash Recovery (Chap. 18), part 1. Motivation. Recovery Manager. Handling the Buffer Pool.

Database Management System

Transaction Management: Crash Recovery (Chap. 18), part 1

some sequential execution crash! Recovery Manager replacement MAIN MEMORY policy DISK

Chapter 22. Transaction Management

Implementing Isolation

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

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

Transactions & Update Correctness. April 11, 2018

Transaction Processing. Introduction to Databases CompSci 316 Fall 2018

Database Applications (15-415)

CS 5300 module6. Problem #1 (10 Points) a) Consider the three transactions T1, T2, and T3, and the schedules S1 and S2.

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

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

Intro to DB CHAPTER 15 TRANSACTION MNGMNT

CSE 344 MARCH 5 TH TRANSACTIONS

Introduction to Transaction Management

COSC344 Database Theory and Applications. Lecture 21 Transactions

Introduction to Data Management CSE 344

CSE 344 MARCH 9 TH TRANSACTIONS

Database Tuning and Physical Design: Execution of Transactions

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

Transactions and Isolation

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

Problems Caused by Failures

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

Database transactions

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

Database Systems CSE 414

CSE 344 MARCH 25 TH ISOLATION

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

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

Introduction to Data Management CSE 344

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

T ransaction Management 4/23/2018 1

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

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI

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

Concurrency Control CHAPTER 17 SINA MERAJI

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

Concurrency Control. R &G - Chapter 19

Why Transac'ons? Database systems are normally being accessed by many users or processes at the same 'me.

Database Systems CSE 414

Concurrency Control. Chapter 17. Comp 521 Files and Databases Fall

Integrity Constraints, Triggers, Transactions and Procedures

Transaction Management

Information Systems (Informationssysteme)

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

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

transaction - (another def) - the execution of a program that accesses or changes the contents of the database

Concurrency Control! Snapshot isolation" q How to ensure serializability and recoverability? " q Lock-Based Protocols" q Other Protocols"

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

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

Transcription:

What are Transactions? Transaction Management: Introduction (Chap. 16) CS634 Class 14, Mar. 23, 2016 So far, we looked at individual queries; in practice, a task consists of a sequence of actions E.g., Transfer $1000 from account A to account B Subtract $1000 from account A Subtract transfer fee from account A Credit $1000 to account B A transaction is the DBMS s view of a user program: Must be interpreted as unit of work : either entire transaction executes, or no part of it executes/has any effect on DBMS Two special final actions: COMMIT or ABORT Slides based on Management Systems 3 rd ed, Ramakrishnan and Gehrke 2 Concurrent Execution DBMS receives large numbers of concurrent requests Concurrent (or parallel) execution improves performance Two transactions are concurrent if they overlap in time. Disk accesses are frequent, and relatively slow; CPU can do a lot of work while waiting for the disk, or even SSD Goal is to increase/maximize system throughput Number of transactions executed per time unit Concurrency control Protocols that ensure things execute correctly in parallel Broad and difficult challenge that goes beyond DBMS realm OS, Distributed Programming, hardware scheduling (CPU registers), etc Our focus is DBMS, but some principles span beyond DBMS Major Example: the web app App server(s) server Web layer Multi-threaded Object layer JDBC Concurrent web requests from users Other apps 3 Web app in execution (CS636) To keep transactions executing concurrently, yet isolated from each other, each has own objects related to DB data Transaction Thread using objects Transaction Thread using objects Cache (rows) On disk Employee objects Employee rows Web app Transactions Each application action turns into a database transaction A well-designed app has a service API describing those actions A request execution calls the service API one or more times. Each service call represents an application action and contains a transaction Thus transactions are contained in request-response cycles This ensures that transactions are short-lived, good for performance But they still can run concurrently under high-enough load

The web app service API App server(s) server Web layer Multi-threaded Object layer JDBC Concurrent web requests from users Other apps 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 8 Roles of Transaction Manager Concurrency Control Ensuring correct execution in the presence of multiple transactions running in parallel 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 execution (Write-Ahead Log or WAL) Modeling Transactions User programs may carry out many operations Data-related computations Prompting user for input, handling web requests but the DBMS is only concerned about what data is read/written from/to the database A transaction is abstracted by a sequence of time-ordered read and write actions e.g., R(X), R(Y), W(X), W(Y) R=read, W=write, data element in parentheses Each individual action is indivisible, or atomic SQL UPDATE = R(X) W(X) 9 10 Important dataflow assumptions Transactions interact with one another as they run only via database read and write operations. No messages exchanged between transactions No use of shared memory between transactions Oracle, other DBs, enforce this Transactions may accept information from the environment when they start and return information to the environment when they finish by committing. The agent that starts a transaction will come to know whether it committed or aborted, and can act on that information. Thus it is possible for data to go from one transaction to the environment and then to another starting transaction, but note that these transactions are not concurrent. Scheduling Transactions Serial schedule: no interleaving of transactions Safe, but poor performance! Schedule equivalence: two schedules are equivalent if they lead to the same state of the DBMS (see footnote on pg. 525 that includes values returned to user in relevant state ) Serializable schedule: schedule that is equivalent to some serial execution of transactions But still allows interleaving/concurrency! 12

Serializable schedule example If execution is not serializable Non-serializable concurrent executions can show anomalies, i.e., clearly bad behavior Let s look at some examples Same effect as executing T1 completely, then T2 13 Concurrency: lost update anomaly Consider two transactions (in a really bad DB) where A = 100 T1: A = A + 100 T2: A = A + 100 T1 & T2 are concurrent, running same transaction program T1& T2 both read old value, 100, add 100, store 200 One of the updates has been lost! Consistency requirement: after execution, A should reflect all deposits (Money should not be created or destroyed) No guarantee that T1 will execute before T2 or vice-versa but the net effect must be equivalent to these two transactions running one-after-the-other in some order Concurrency: more complex case (1/3) Consider two transactions running different programs T1 performs an account transfer T2 performs credit of (6%) interest amount Consistency requirement: after execution, sum of accounts must be 106% the initial sum (before execution) No guarantee that T1 will execute before T2 or vice-versa but the net effect must be equivalent to these two transactions running one-after-the-other in some order 15 16 Concurrency: when things go wrong (2/3) Assume that initially there are $500 in both accounts Consider a possible interleaving or schedule Concurrency: when things go wrong (3/3) Consider another interleaving or schedule: After execution, A=636, B=430, A+B=1066 After execution, A=636, B=424, A+B=1060 CORRECT The DBMS view WRONG!!! T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B) 17 18

Concurrent Execution Anomalies Anomalies may occur in concurrent execution The notion of conflicts helps understand anomalies Is there a conflict when multiple READ operations are posted? No What if one of the operations is a WRITE? YES! WR, RW and WW conflicts 19 WR Conflicts Reading Uncommitted Data (Dirty Reads) T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B) The earlier example where interest is not properly credited is due to a WR conflict Value of A written by T1 is read by T2 before T1 completed all its changes 20 RW Conflicts Unrepeatable Reads T1: R(A), R(A), W(A), Commit T2: R(A), W(A), Commit WW Conflicts Overwriting Uncommitted Data T1: W(A), W(B), Commit T2: W(A), W(B), Commit Scenario: Let A (=1) be the number of copies of an item. T1 checks the number available. If the number is greater than 0, T1 places an order by decrementing the count In the meantime, T2 updated the value of the count (say, to zero) T1 will set the count to a negative value! Assume two employees must always have same salary T1 sets the salaries to $1000, T2 to $2000 There is a lost update, and the final salaries are $1000 and $2000 Lost update because the transaction that comes last in serial order should set both values. One got lost. 21 22 Scheduling Transactions: recall terminology Serial schedule: no interleaving of transactions Safe, but poor performance! Schedule equivalence: two schedules are equivalent if they lead to the same state of the DBMS (see footnote on pg. 525 that includes values returned to user in relevant state ) Serializable schedule: schedule that is equivalent to some serial execution of transactions But still allows interleaving/concurrency! Conflict Serializable Schedules Two schedules are conflict equivalent if: Involve the same actions of the same transactions Every pair of conflicting actions is ordered the same way Schedule S is conflict serializable if S is conflict equivalent to some serial schedule A conflict serializable schedule is serializable (to be shown in future classes) Some other schedules are also serializable 23

Why is serializability important? If each transaction preserves consistency, every serializable schedule preserves consistency For example, transactions that move money around should always preserve the total amount of money. If running with serializable transactions, we only need to check that each transaction program has this property, and we know that the system does. How to ensure serializable schedules? Use locking protocols (ensuring conflict serializability) DBMS inserts proper locking actions, user is oblivious to locking (except through its effect on performance, and deadlocks) There are other ways too, covered later. Strict Two-Phase Locking (Strict 2PL) Protocol steps Each transaction must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing. All locks held are released when the transaction completes (Non-strict) 2PL: Release locks anytime, but cannot acquire locks after releasing any lock. Strict 2PL allows only serializable schedules. It simplifies transaction aborts (Non-strict) 2PL also allows only serializable schedules, but involves more complex abort processing 25 28 Strict 2PL Example (red op is blocked) S(B) T2: S(A) R(A) X(B) S(B) T2: S(A) R(A) X(B) R(B) T2: S(A) R(A) X(B) R(B) S(B) W(B) C S(B) R(B) C T2: S(A) R(A) X(B) R(B) W(B) C Aborting Transactions When Ti is aborted, all its actions have to be undone if Tj reads an object last written by Ti, Tj must be aborted as well! cascading aborts can be avoided with 2PL by releasing locks only at commit (Strict 2PL) If Ti writes an object, Tj can read this only after Ti commits This also means the schedule is recoverable : transactions commit only after all transactions whose changes they read commit. In general, recoverable and serializable are separate properties of concurrency protocols, but Strict 2PL has both. Strict 2PL is recoverable, and cascading aborts are prevented At the cost of decreased concurrency No free lunch! Increased parallelism leads to locking protocol complexity 29 30 Deadlocks Cycle of transactions waiting for locks to be released by each other T1: X(A) W(A) S(B) [R(B) ] T2: X(B) W(B) S(A) [R(A) ] Two ways of dealing with deadlocks: Deadlock prevention Deadlock detection Locking Performance Lock-based schemes rely on two mechanisms Blocking Aborting Both blocking and aborting cause performance overhead Transactions may have to wait Transactions may need to be re-executed How does blocking affect throughput? First few transactions do not conflict no blocking Parallel execution, performance increase As more transactions execute, blocking occurs After a point, adding more transactions decreases throughput! 31

Locking Performance (2) Thrashing Improving Performance Locking the smallest-sized objects possible e.g., row set instead of table Reduce the time a lock is held for Release locks faster Reducing hot spots Careful review of application design Reduce contention Active Transaction Count Lock Management Lock and unlock requests are handled by the lock manager Lock table entry: Number of transactions currently holding a lock Type of lock held (shared or exclusive) Pointer to queue of lock requests Locking and unlocking have to be atomic operations 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 <savepoint name> Actions ROLLBACK TO SAVEPOINT <savepoint name> SAVEPOINT advantage vs. sequence of transactions Can roll back over multiple savepoints Lower overhead: no new transaction initiated (book, pg. 536) But transaction initiation is not an expensive action. Locks are still held on changes done before savepoint, when rollback to savepoint done. Locks would be released if a real commit is done. 36 Setting Transaction Properties in SQL Access Mode READ ONLY vs READ WRITE Isolation Level (decreasing level of concurrency) Level Dirty Read Unrepeatable Read Phantom READ UNCOMMITTED Possible Possible Possible READ COMMITTED No Possible Possible REPEATABLE READ No No Possible SERIALIZABLE No No No Isolation Levels in Practice s 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 37

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-thenwrite is safe even at RC Update T set A = A + 100 (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 40