March 21st – Transactions

Slides:



Advertisements
Similar presentations
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Advertisements

Transactions (Chapter ). What is it? Transaction - a logical unit of database processing Motivation - want consistent change of state in data Transactions.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 16 – Intro. to Transactions.
Chapter 7 Transactions 7.1 Transaction Concept 7.2 Transaction State 7.3 Implementation of Atomicity and Durability 7.4 Concurrent Executions 7.5 Serializability.
Transactions or Concurrency Control. Introduction A program which operates on a DB performs 2 kinds of operations: –Access to the Database (Read/Write)
Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)
Concurrency. Correctness Principle A transaction is atomic -- all or none property. If it executes partly, an invalid state is likely to result. A transaction,
Database Management Systems I Alex Coman, Winter 2006
Concurrency Control 18.1 – 18.2 Chiu Luk CS257 Database Systems Principles Spring 2009.
Transactions Amol Deshpande CMSC424. Today Project stuff… Summer Internships 
Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control.
CS4432: Database Systems II Transaction Management Motivation 1.
Transactions Sylvia Huang CS 157B. Transaction A transaction is a unit of program execution that accesses and possibly updates various data items. A transaction.
Transactions1 Unit of work on a database. Transactions2 Transactions, concept Logical unit of work on the database –Examples Transfer money from bank.
CS411 Database Systems Kazuhiro Minami 14: Concurrency Control.
TRANSACTIONS. Objectives Transaction Concept Transaction State Concurrent Executions Serializability Recoverability Implementation of Isolation Transaction.
Introduction to Data Management CSE 344 Lecture 23: Transactions CSE Winter
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
Transactions. What is it? Transaction - a logical unit of database processing Motivation - want consistent change of state in data Transactions developed.
Chapter 15: Transactions Loc Hoang CS 157B. Definition n A transaction is a discrete unit of work that must be completely processed or not processed at.
Chapter 14 Transactions Yonsei University 1 st Semester, 2015 Sanghyun Park.
15.1 Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data items. E.g. transaction to transfer.
©Silberschatz, Korth and Sudarshan14.1Database System Concepts - 6 th Edition Chapter 14: Transactions Transaction Concept Transaction State Concurrent.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 14: Transactions.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 16 – Intro. to Transactions.
1 Transaction Processing Case Study. 2 Interaksi Proses There is table Sells(shop,beverage,price), and suppose that Joe’s Shop sells only Juice for $2.50.
Jinze Liu. ACID Atomicity: TX’s are either completely done or not done at all Consistency: TX’s should leave the database in a consistent state Isolation:
Advanced Database CS-426 Week 6 – Transaction. Transactions and Recovery Transactions A transaction is an action, or a series of actions, carried out.
1 Concurrency Control. 2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –While.
CS 440 Database Management Systems
Concurrency Control.
Transaction Management and Concurrency
Chapter 14: Transactions
1 Introduction to Transaction Processing (1)
CS216: Data-Intensive Computing Systems
Database Management System
Part- A Transaction Management
Transaction Management
Cse 344 March 2nd – E/r diagrams.
Introduction to Data Management CSE 344
Transactions.
Cse 344 March 25th – Isolation.
Transactions Properties.
Transactions.
Chapter 15: Transactions
March 7th – Transactions
Transaction Management
March 9th – Transactions
Transactions Sylvia Huang CS 157B.
Lecture 21: Concurrency & Locking
Chapter 14: Transactions
CS162 Operating Systems and Systems Programming Review (II)
Conflict-Serializability (section 18.2 of Concurrency Control)
Conflicts.
Lecture 21: Intro to Transactions & Logging III
Module 15: Transactions.
Lecture 13: Transactions in SQL
Transaction Management Overview
Chapter 14: Transactions
Transaction management
Transaction Management
Chapter 14: Transactions
CPSC-608 Database Systems
Transaction Management Overview
Introduction to Database Systems CSE 444 Lectures 17-18: Concurrency Control November 5-7, 2007.
CPSC-608 Database Systems
UNIT -IV Transaction.
Lecture 18: Concurrency Control
Lecture 11: Transactions in SQL
Presentation transcript:

March 21st – Transactions Cse 344 March 21st – Transactions

Administrivia HW7 Due Wednesday OQ6 Due Wednesday, May 23rd 11:00 HW8 Out ”Wednesday” Will be up today or tomorrow Transactions Due next Friday

Class Overview Unit 1: Intro Unit 2: Relational Data Models and Query Languages Unit 3: Non-relational data Unit 4: RDMBS internals and query optimization Unit 5: Parallel query processing Unit 6: DBMS usability, conceptual design Unit 7: Transactions Locking and schedules Writing DB applications For each item, say why we are learning about it.

Transactions We use database transactions everyday Bank $$$ transfers Online shopping Signing up for classes For this class, a transaction is a series of DB queries Read / Write / Update / Delete / Insert Unit of work issued by a user that is independent from others

Challenges Want to execute many apps concurrently All these apps read and write data to the same DB Simple solution: only serve one app at a time What’s the problem? Want: multiple operations to be executed atomically over the same DBMS

What can go wrong? Manager: balance budgets among projects Remove $10k from project A Add $7k to project B Add $3k to project C CEO: check company’s total balance SELECT SUM(money) FROM budget; This is called a dirty / inconsistent read aka a WRITE-READ conflict

What can go wrong? App 1: SELECT inventory FROM products WHERE pid = 1 App 2: UPDATE products SET inventory = 0 WHERE pid = 1 App 1: SELECT inventory * price FROM products WHERE pid = 1 This is known as an unrepeatable read aka READ-WRITE conflict

Account 1 = $100 Account 2 = $100 Total = $200 What can go wrong? Account 1 = $100 Account 2 = $100 Total = $200 App 1: Set Account 1 = $200 Set Account 2 = $0 App 2: Set Account 2 = $200 Set Account 1 = $0 At the end: Total = $200 App 1: Set Account 1 = $200 App 2: Set Account 2 = $200 App 1: Set Account 2 = $0 App 2: Set Account 1 = $0 At the end: Total = $0 This is called the lost update aka WRITE-WRITE conflict

What can go wrong? Lesson: Paying for Tuition (Underwater Basket Weaving) Fill up form with your mailing address Put in debit card number (because you don’t trust the gov’t) Click submit Screen shows money deducted from your account [Your browser crashes] Lesson: Changes to the database should be ALL or NOTHING

If BEGIN… missing, then TXN consists of a single instruction Transactions Collection of statements that are executed atomically (logically speaking) BEGIN TRANSACTION [SQL statements] COMMIT or ROLLBACK (=ABORT) [single SQL statement] If BEGIN… missing, then TXN consists of a single instruction

Know your transactions: ACID Atomic State shows either all the effects of txn, or none of them Consistent Txn moves from a DBMS state where integrity holds, to another where integrity holds remember integrity constraints? 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

Atomic Definition: A transaction is ATOMIC if all its updates must happen or not at all. Example: move $100 from A to B UPDATE accounts SET bal = bal – 100 WHERE acct = A; UPDATE accounts SET bal = bal + 100 WHERE acct = B; BEGIN TRANSACTION; UPDATE accounts SET bal = bal – 100 WHERE acct = A; UPDATE accounts SET bal = bal + 100 WHERE acct = B; COMMIT;

Isolated Definition: An execution ensures that transactions are isolated, if the effect of each transaction is as if it were the only transaction running on the system.

Consistent Recall: integrity constraints govern how values in tables are related to each other Can be enforced by the DBMS, or ensured by the app How consistency is achieved by the app: App programmer ensures that txns only takes a consistent DB state to another consistent state DB makes sure that txns are executed atomically Can defer checking the validity of constraints until the end of a transaction

Durable A transaction is durable if its effects continue to exist after the transaction and even after the program has terminated How? By writing to disk! More in 444

Rollback transactions If the app gets to a state where it cannot complete the transaction successfully, execute ROLLBACK The DB returns to the state prior to the transaction What are examples of such program states?

ACID Atomic Consistent Isolated Durable Again: by default each statement is its own txn Unless auto-commit is off then each statement starts a new txn

A schedule is a sequence of interleaved actions from all transactions schedules A schedule is a sequence of interleaved actions from all transactions

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 (up to what we have learned so far) But DBMS don’t do that because we want better overall system performance Review: serially (up to everything that we have discussed so far) it doesn’t guarantee D in acid

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

Example of a (Serial) Schedule T1 T2 READ(A, t) t := t+100 WRITE(A, t) READ(B, t) WRITE(B,t) READ(A,s) s := s*2 WRITE(A,s) READ(B,s) WRITE(B,s) Time Schedule is serial if it reflects a serial execution. In this example, two serial schedules are possible.

Another Serial Schedule READ(A,s) s := s*2 WRITE(A,s) READ(B,s) WRITE(B,s) READ(A, t) t := t+100 WRITE(A, t) READ(B, t) WRITE(B,t) Time Schedule is serial if it reflects a serial execution. In this example, two serial schedules are possible.

Review: Serializable Schedule A schedule is serializable if it is equivalent to a serial schedule Reminder: why do we want interleaving? For performance.

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

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

How do We Know if a Schedule is Serializable? Notation: T1: r1(A); w1(A); r1(B); w1(B) T2: r2(A); w2(A); r2(B); w2(B) Key Idea: Focus on conflicting operations

Write-Read – WR Read-Write – RW Write-Write – WW Read-Read? Conflicts Write-Read – WR Read-Write – RW Write-Write – WW Read-Read?

Conflict Serializability Conflicts: (i.e., swapping will change program behavior) Two actions by same transaction Ti: ri(X); wi(Y) Two writes by Ti, Tj to same element wi(X); wj(X) Conflict: pair of consecutive actions in schedule s.t. if swapped, then behavior changes. Conflict serializability is a stronger condition than serializability (although have to be careful because of things like phantom problem) wi(X); rj(X) Read/write by Ti, Tj to same element ri(X); wj(X)

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 The converse is not true (why?) Example of serializable but not conflict-serializable: W1(X, 0); W2(X, 0); R1(X); R2(X) is the same as the serial schedule W1(X, 0); R1(X); W2(X, 0); R2(X)

Conflict Serializability Example: r1(A); w1(A); r2(A); w2(A); r1(B); w1(B); r2(B); w2(B)

Conflict Serializability Example: r1(A); w1(A); r2(A); w2(A); r1(B); w1(B); r2(B); w2(B) r1(A); w1(A); r1(B); w1(B); r2(A); w2(A); r2(B); w2(B)

Conflict Serializability Example: r1(A); w1(A); r2(A); w2(A); r1(B); w1(B); r2(B); w2(B) r1(A); w1(A); r1(B); w1(B); r2(A); w2(A); r2(B); w2(B)

Conflict Serializability Example: r1(A); w1(A); r2(A); w2(A); r1(B); w1(B); r2(B); w2(B) r1(A); w1(A); r2(A); r1(B); w2(A); w1(B); r2(B); w2(B) r1(A); w1(A); r1(B); w1(B); r2(A); w2(A); r2(B); w2(B)

Conflict Serializability Example: r1(A); w1(A); r2(A); w2(A); r1(B); w1(B); r2(B); w2(B) r1(A); w1(A); r2(A); r1(B); w2(A); w1(B); r2(B); w2(B) r1(A); w1(A); r1(B); r2(A); w2(A); w1(B); r2(B); w2(B) …. r1(A); w1(A); r1(B); w1(B); r2(A); w2(A); r2(B); w2(B)

Testing for Conflict-Serializability Precedence graph: A node for each transaction Ti, An edge from Ti to Tj whenever an action in Ti conflicts with, and comes before an action in Tj The schedule is conflict-serializable iff the precedence graph is acyclic

Example 1 r2(A); r1(B); w2(A); r3(A); w1(B); w3(A); r2(B); w2(B) 1 2 3 Arrow: two transactions, same element, at least one action is a write 1 2 3

Example 1 r2(A); r1(B); w2(A); r3(A); w1(B); w3(A); r2(B); w2(B) B A 1 Arrow: two transactions, same element, at least one action is a write 1 2 3 This schedule is conflict-serializable

Example 2 r2(A); r1(B); w2(A); r2(B); r3(A); w1(B); w3(A); w2(B) 1 2 3

Example 2 r2(A); r1(B); w2(A); r2(B); r3(A); w1(B); w3(A); w2(B) B A 1 This schedule is NOT conflict-serializable

Scheduler Scheduler = the module that schedules the transaction’s actions, ensuring serializability Also called Concurrency Control Manager We discuss next how a scheduler may be implemented