1cs236607. 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.

Slides:



Advertisements
Similar presentations
Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 4/13/2015Databases21.
Advertisements

Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
1 Integrity Ioan Despi Transactions: transaction concept, transaction state implementation of atomicity and durability concurrent executions serializability,
Lock-Based Concurrency Control
Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application.
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004.
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Transaction Management
Database Management Systems I Alex Coman, Winter 2006
Database Administration Part 1 Chapter Six CSCI260 Database Applications.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Managing Concurrency in Web Applications. DBI 2007 HUJI-CS 2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses.
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.
Managing Transaction and Lock Vu Tuyet Trinh Hanoi University of Technology 1.
1 IT420: Database Management and Organization Transactions 31 March 2006 Adina Crăiniceanu
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Transactions Sylvia Huang CS 157B. Transaction A transaction is a unit of program execution that accesses and possibly updates various data items. A transaction.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
DB Transactions CS143 Notes TRANSACTION: A sequence of SQL statements that are executed "together" as one unit:
Transactions1 Unit of work on a database. Transactions2 Transactions, concept Logical unit of work on the database –Examples Transfer money from bank.
TRANSACTIONS. Objectives Transaction Concept Transaction State Concurrent Executions Serializability Recoverability Implementation of Isolation Transaction.
ITEC 3220M Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220m.htm
Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life.
Concurrency and Transaction Processing. Concurrency models 1. Pessimistic –avoids conflicts by acquiring locks on data that is being read, so no other.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 136 Database Systems I SQL Modifications and Transactions.
11/7/2012ISC329 Isabelle Bichindaritz1 Transaction Management & Concurrency Control.
How transactions work A transaction groups a set of Transact-SQL statements so that they are treated as a unit. Either all statements in the group are.
Transactions and Locks A Quick Reference and Summary BIT 275.
XA Transactions.
Giovanni Chierico | May 2012 | Дубна Data Concurrency, Consistency and Integrity.
Transactions. Transaction: Informal Definition A transaction is a piece of code that accesses a shared database such that each transaction accesses shared.
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.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
JDBC Part II CS 124. More about JDBC Types Statement versus PreparedStatement Timeout NULL values Meta-data close() methods Exceptions Transactions JDBC.
JDBC (Java Database Connectivity)
Managing Concurrency in Web Applications
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
1 Advanced Database Concepts Transaction Management and Concurrency Control.
Module 11: Managing Transactions and Locks
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
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.
A Guide to MySQL 6. 2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command.
In this session, you will learn to: Implement triggers Implement transactions Objectives.
Basic Web Application Development Instructor: Matthew Schurr.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks
Managing Concurrency in Web Applications
Transaction Management and Concurrency Control
LAB: Web-scale Data Management on a Cloud
On transactions, and Atomic Operations
Batches, Transactions, & Errors
Transactions, Locking and Query Optimisation
Transactions Sylvia Huang CS 157B.
Chapter 10 Transaction Management and Concurrency Control
On transactions, and Atomic Operations
Batches, Transactions, & Errors
Lecture 13: Transactions in SQL
Objectives Define and describe transactions
Transactions and Concurrency
Chapter 14: Transactions
Lecture 11: Transactions in SQL
Presentation transcript:

1cs236607

2 Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect at Database accesses (read / write) Execution of application code (Servlets / JSP) Users activate the same application objects: Database Shared connection / connection pool Servlet / JSP object For each Servlet / JSP, only one object is created cs236607

3 Concurrency Hazards What are the potential problems? Inconsistency due to concurrent accesses For example, generating IDs, verifying uniqueness of names, presenting information while being updated Transactions: Want to consider a sequence of operations as an atomic action Until commitment, updates are partial (inconsistent) On failure, the origin state is restored (rollback) How do we disable the server from relying on partial information or such that may later be cancelled due to roll back? cs236607

4 Handling Concurrency For managing concurrency in the level of Java code, use known Java tools Synchronization, wait / notify, etc. For handling concurrency in database accesses, transactions are used Concurrent transactions use distinct connections But (generating) connections are expensive! Use thread pools Share connections whenever possible (e.g., simple reads) cs236607

5 Thumbnail Rules Some concurrency problems can be solved at both the code and database levels (what about client-side programming?) In principle, prefer letting the database handle concurrency Database research and vendors have put a lot of effort on handling concurrent transactions But you need to understand the exact model of transaction management that is implemented by your specific database engine Next, transaction management in MySQL cs236607

6

7 Transactions and JDBC Transaction: Transaction: A sequence of statements that must all succeed (or all fail) together e.g., updating several tables due to customer purchase Failure : System must reverse all previous actions Cannot leave DB in inconsistent state halfway through a transaction COMMIT = complete transaction ROLLBACK = cancel all actions cs236607

ACID cs Atomicity Consistency Isolation Durability

ACID Atomicity: transactions are atomic and indivisible Consistency: operations transform the database from one valid state to another valid state Isolation: transactions do not affect each other while they are running Durability: the changes to the database of a transaction that has committed are permanent cs

10 An Example Suppose that we want to transfer money from bank account 13 to account 72: PreparedStatement pstmt = con.prepareStatement("update BankAccount set amount = amount + ? where accountId = ?"); pstmt.setInt(1,-100); pstmt.setInt(2, 13); pstmt.executeUpdate(); pstmt.setInt(1, 100); pstmt.setInt(2, 72); pstmt.executeUpdate(); What happens if this update fails? cs236607

11 Transaction Life Cycle Through JDBC, transactions are not opened and closed explicitly A transaction starts on 1 st (successful) command After a connection is established After the previous transaction ends A transaction ends when COMMIT or ROLLBACK are applied Either explicitly or implicitly (next slides) Multiple transactions are obtained by using multiple connections to the database cs236607

12 Committing a Transaction How do we commit? Explicitly invoking Connection.commit() Implicitly After every query execution, if AutoCommit is true When the user normally disconnects (i.e., appropriately closes the connection) In some DBs: After invoking a DDL command (CREATE, DROP, RENAME, ALTER, …) cs236607

13 Automatic Commitment A Connection object has a boolean AutoCommit If AutoCommit is true (default), then every statement is automatically committed If AutoCommit is false, then each statement is added to an ongoing transaction Change using setAutoCommit(boolean) If AutoCommit is false, need to explicitly commit or rollback the transaction using Connection.commit() and Connection.rollback() cs236607

14 Rolling Back Rolling Back: Undoing any change to data within the current transaction The ROLLBACK command explicitly rolls back (and ends) the current transaction ROLLBACK is implicitly applied when the user abnormally disconnects (i.e., without appropriately closing the connection) cs236607

15 Fixed Example con.setAutoCommit(false); try { PreparedStatement pstmt = con.prepareStatement("update BankAccount set amount = amount + ? where accountId = ?"); pstmt.setInt(1,-100); pstmt.setInt(2, 13); pstmt.executeUpdate(); pstmt.setInt(1, 100); pstmt.setInt(2, 72); pstmt.executeUpdate(); con.commit(); catch (SQLException e) { con.rollback(); …; } cs236607

16cs236607

17 Isolation Issues How do different transactions interact? Does a running transaction see uncommitted changes? Does it see committed changes? Can two transactions update the same row? Next part … cs236607

18 Isolation Levels The isolation level determines the capabilities of a transaction to read/write data that is accessed by other transactions In MySQL, there are four levels of isolation: 1. READ UNCOMMITTED 2. READ COMMITTED 3. REPEATABLE READ (default) 4. SERIALIZABLE Each transaction determines its isolation level Connection.setTransactionIsolation(int level) cs236607

19 READ COMMITED & SERIALIZABLE SERIALIZABLE: During the whole transaction, statements read only the changes that were committed by the time the transaction begun (and the changes made by the transaction itself) REPEATABLE READ: each transaction gets to work in an isolated version of the table, where each row remains as it was when the transaction started cs236607

20 READ COMMITED & SERIALIZABLE READ COMMITTED: A statement reads the data that was committed by the time the statement (not the transaction) begun READ UNCOMMITTED : It is possible for transactions to read changes that other transactions have made before the changes have been committed (practically, no transactions) cs236607

21 Some Definitions Dirty reads: Dirty reads: A transaction reads data that is written by another, uncommitted transaction Non-repeatable reads: Non-repeatable reads: A transaction rereads data it previously read and finds that a committed transaction has modified or deleted that data Phantom reads: Phantom reads: A transaction re-executes a query returning a set of rows satisfying a search condition and finds that a committed transaction inserted additional rows that satisfy the condition cs236607

22 Comparison SERIALIZABLE REPEATABLE READ READ COMMITED READ UNCOMMITED Impossible Possible Dirty Reads Impossible Possible Non- repeatable Reads Impossible Possible (not likely) Possible Phantom Reads cs236607

23 Comparison SERIALIZABLE REPEATABLE READ READ COMMITED READ UNCOMMITED Impossible Possible Dirty Reads Impossible Possible Non- repeatable Reads Impossible Possible (not likely) Possible Phantom Reads cs236607

24 What Happens Here (1)? CREATE TABLE pairs (x INTEGER, y INTEGER); select * from pairs insert into pairs values(1,1) select * from pairs COMMIT 2. select * from pairs 3. insert into pairs values(1,1) 6. select * from pairs 8. COMMIT select * from pairs insert into pairs values(1,2) select * from pairs COMMIT 4. select * from pairs 5. insert into pairs values(1,2) 7. select * from pairs 9. COMMIT T.1: R. COMMITTED T.2: SERIALIZABLE cs236607

25 What Happens Here (2)? CREATE TABLE pairs (x INTEGER, y INTEGER); T.1: R. COMMITTED T.2: SERIALIZABLE insert into pairs values(1,1) COMMIT select * from pairs select * from pairs COMMIT 3. insert into pairs values(1,1) 4. COMMIT 5. select * from pairs 9. select * from pairs 10. COMMIT select * from pairs select * from pairs insert into pairs values(1,2) COMMIT 2. select * from pairs 6. select * from pairs 7. insert into pairs values(1,2) 8. COMMIT cs236607

26 What Happens Here (3)? CREATE TABLE pairs (x INTEGER, y INTEGER); insert into pairs values(1,1) COMMIT select * from pairs select * from pairs COMMIT 2. insert into pairs values(1,1) 4. COMMIT 5. select * from pairs 9. select * from pairs 10. COMMIT select * from pairs select * from pairs insert into pairs values(1,2) COMMIT 3. select * from pairs 6. select * from pairs 7. insert into pairs values(1,2) 8. COMMIT T.1: SERIALIZABLE T.2: SERIALIZABLE Is it equivalent to any truly serial execution of the transactions? cs236607

27cs236607

28 Locks MySQL controls concurrent access to data by deploying locks Each operation on the database (select, update, insert, create, drop, etc.) requires some lock to be obtained by the running transaction If a lock is not available, the transaction is blocked Locks are applied to either whole tables or to (sets of) specific rows cs236607

29 Table-Level Locks Two types of table locks Read (shared): Can be held by multiple transactions Write (exclusive): Other transactions cannot have this lock (shared or exclusive) cs236607

30 Locking a Table To lock the table mytbl, use the command Acquires the write (exclusive) lock Thus, no transaction can read or update the table This lock is also needed in ALTER TABLE & DROP TABLE This operation blocks until all locks on the table are released LOCK TABLES mytbl AS alias write cs236607

31 Concurrent Updates MySQL prevents updating a row that is being updated by an uncommitted transaction Locks are held until the transaction ends The second updating transaction is blocked until the lock is released (first one commits or rolls back) BEGIN UPDATE r END BEGIN UPDATE r END T1T1 T2T2 T 2 READ COMMITTED: Update applied when T 1 terminates T 2 SERIALIZABLE: T 1 commits: Update fails T 1 rolls back: Succeeds cs236607

32 What Happens Here (5)? CREATE TABLE pairs (x INTEGER, y INTEGER); insert into pairs values(1,1) COMMIT update pairs set y=3 where x= select * from pairs COMMIT insert into pairs values(1,1) COMMIT update pairs set y=3 where x= select * from pairs COMMIT update pairs set y=2 where x= select * from pairs COMMIT update pairs set y=2 where x= select * from pairs COMMIT T.1: R. COMMITTED T.2: SERIALIZABLE cs236607

33 What Happens Here (4)? CREATE TABLE pairs (x INTEGER, y INTEGER); insert into pairs values(1,1) COMMIT update pairs set y=3 where x=1??? insert into pairs values(1,1) COMMIT update pairs set y=3 where x=1??? update pairs set y=2 where x= select * from pairs COMMIT update pairs set y=2 where x= select * from pairs COMMIT T.1: SERIALIZABLE T.2: SERIALIZABLE cs236607

Links tutorial/ch10.html tutorial/ch10.html al-statements.html al-statements.html cs