Managing Concurrency in Web Applications

Slides:



Advertisements
Similar presentations
Optimistic Methods for Concurrency Control By : H.T. Kung & John T. Robinson Presenters: Munawer Saeed.
Advertisements

Transactions - Concurrent access & System failures - Properties of Transactions - Isolation Levels 4/13/2015Databases21.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
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.
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.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Transaction Management and Concurrency Control
10 1 Chapter 10 Transaction Management and Concurrency Control Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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
1 Transaction Management Database recovery Concurrency control.
TRANSACTIONS. Definition One or more SQL statements that operate as a single unit. Each statement in the unit is completely interdependent. If one statement.
Chapter 9 Transaction Management and Concurrency Control
9 Chapter 9 Transaction Management and Concurrency Control Hachim Haddouti.
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.
08_Transactions_LECTURE2 DBMSs should guarantee ACID properties (Atomicity, Consistency, Isolation, Durability). This is typically done by guaranteeing.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Chapterb19 Transaction Management Transaction: An action, or series of actions, carried out by a single user or application program, which reads or updates.
TRANSACTIONS. Objectives Transaction Concept Transaction State Concurrent Executions Serializability Recoverability Implementation of Isolation Transaction.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
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 Server accesses data on behalf of client – series of operations is a transaction – transactions are atomic Several clients may invoke transactions.
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.
Transactions and Locks A Quick Reference and Summary BIT 275.
II.I Selected Database Issues: 2 - Transaction ManagementSlide 1/20 1 II. Selected Database Issues Part 2: Transaction Management Lecture 4 Lecturer: Chris.
The Relational Model1 Transaction Processing Units of Work.
Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness.
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.
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.
Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
1 Advanced Database Concepts Transaction Management and Concurrency Control.
Module 11: Managing Transactions and Locks
Transaction Management and Concurrent Control
9 1 Chapter 9_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
10 Transaction Management and Concurrency Control MIS 304 Winter 2005.
3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.
18 September 2008CIS 340 # 1 Last Covered (almost)(almost) Variety of middleware mechanisms Gain? Enable n-tier architectures while not necessarily using.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Transactions and Concurrency Control. 2 What is a Transaction?  Any action that reads from and/or writes to a database may consist of  Simple SELECT.
SYSTEMS IMPLEMENTATION TECHNIQUES TRANSACTION PROCESSING DATABASE RECOVERY DATABASE SECURITY CONCURRENCY CONTROL.
Managing Concurrency in Web Applications
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
LAB: Web-scale Data Management on a Cloud
Concurrency Control.
Transaction Management
On transactions, and Atomic Operations
Transaction Management
Transactions, Locking and Query Optimisation
Chapter 10 Transaction Management and Concurrency Control
On transactions, and Atomic Operations
Chapter 15 : Concurrency Control
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Transactions and Concurrency
Transaction Management
Advanced Topics: Indexes & Transactions
Transactions, Properties of Transactions
Presentation transcript:

Managing Concurrency in Web Applications cs236607

Concurrent Access to Web Applications 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

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 What are possible solutions? Transactions: 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 rollback? cs236607

Handling Concurrency For managing concurrency on 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 connections (and their generation) are expensive! Use thread pools Share connections whenever possible (e.g., simple reads) cs236607

Different Applications Have Different Requirements Consider the following three examples: britanica.com – many users view it but only a few users change it wikipedia.com – many users view it and many users change it A page showing access trends (statistics) for a popular web site– many users change the data but only a few view it How do we guarantee correctness and efficiency in each scenario? cs236607

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 We will examine transaction management in MySQL cs236607

ACID Atomicity Consistency Isolation Durability cs236607

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 cs236607

Transaction Management via JDBC cs236607

Transactions and JDBC 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

What happens if this update fails? 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); What happens if this update fails? cs236607

Transaction Life Cycle Through JDBC, transactions are not opened and closed explicitly A transaction starts on 1st (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

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

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

Rollback 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

Improved 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

Transactions in MySQL cs236607

Storage Engines In MySQL, different storage engines can be used MyISAM Efficient Has built-in full-text search Does not support transactions and uses table-level locking InnoDB Supports transactions (ACID compliant) Other advanced features (e.g., referential integrity) cs236607

MyISAM Indexes .MYD .MYI PRIMARY KEY INDEX cs236607

MyISAM Indexes .MYD .MYI PRIMARY KEY INDEX cs236607

MyISAM Indexes .MYD .MYI PRIMARY KEY INDEX cs236607

InnoDB Indexes PRIMARY KEY INDEX PKV = Primary Key Value cs236607 PKV

MyISAM vs. InnoDB MyISAM uses index file (.MYI) and data file (.MYD) All indexes are “created equal” Lookup using KEY vs. PRIMARY KEY has same efficiency Secondary keys contain pointers to the data file InnoDB uses PRIMARY KEY “clustered index” The data rows are stored in the index with the PRIMARY KEY Secondary keys contain PRIMARY KEY values (PKVs) to reference the row InnoDB can be faster, for simple PK lookups! MyISAM is usually faster for sequential accesses, updates, index lookups cs236607

cs236607

Setting the Storage Engine CREATE TABLE t (i INT) ENGINE = INNODB; SET storage_engine=MYISAM; ALTER TABLE t ENGINE = MYISAM; cs236607

cs236607

InnoDB Model InnoDB combines multi versioning with 2-phase locking (2PL) to achieve atomicity and isolation The goal: serializability – transactions are executed in parallel, however, their effect, on the database and one on the other, is as if they were executed one after the other cs236607

Theoretical Background Schedule A schedule is a list of actions(read/write tuples) of different transactions A serial schedule is a schedule where for each two transactions, all the actions of one transaction appear before all the actions of the other transaction (non-interleaved) cs236607

Theoretical Background View Equivalence Two schedules S1 and S2 are equivalent if the following hold: If in one schedule a transaction Ti reads an initial value for an object X, then Ti reads the same value for X in the other schedule If in one schedule the transaction Ti reads for an object X the value written by the transaction Tj, then in the other schedule Ti reads for X the value written by Tj If in one schedule the transaction Ti is the final transaction to write the value for an object X, then it is the final transaction to write the value for X in the other transaction Ti cs236607

Theoretical Background View Serializable A schedule is view serializable if it is equivalent to a serial schedule Deciding whether a schedule is view serializable is hard (the time complexity of the test is NP-hard) Instead, we apply a simpler test of detecting conflicting operations cs236607

Conflicting Operations Theoretical Background Conflicting Operations Two schedules may not be view equivalent because of conflicting actions: The actions are being done by two different transactions They are done on the same tuple At least one of the actions is a write operation In other words, Two different transactions T1 and T2 write the same tuple t A transaction T1 write a tuple t and a different transaction T2 reads the same tuple t, or vice versa cs236607

Conflict Serializable Theoretical Background Conflict Serializable Two schedules are conflict equivalent if for every two conflicting actions, their order in one schedule is equal to their order in the other schedule A schedule is conflict serializable if it is conflict equivalent to a serial schedule If a schedule is conflict serializable then it is view serializable cs236607

Locks MySQL controls concurrent access to data by deploying locks An operation on the database (select, update, insert, create, drop, etc.) may require 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

Lock Types READ Locks – Shared WRITE Locks – Exclusive MyISAM – Table-level locks InnoDB – Row-level locks Explicitly locking tables is possible READ Locks – Shared WRITE Locks – Exclusive cs236607

SELECT … LOCK IN SHARE MODE One client locks the rows of the table for reading SELECT … LOCK IN SHARE MODE A second client requests an exclusive lock on the rows SELECT … FOR UPDATE cs236607

Theoretical Background Two Phase Locking (2PL) Each transaction has two phases: 1. Acquiring locks 2. Releasing locks A schedule in which all the transactions satisfy 2PL is conflict serializable (and hence, also view serializable) cs236607

Multi Versioning Each transaction has a “snapshot” of the database A time stamp is assigned to transactions and to objects When reading an object, the transaction reads the latest version among the versions that were written by transactions that precede it When writing, a transaction cannot write to an object if the object has already been read by a later transaction (i.e., by a transaction with a greater time stamp) cs236607

Transaction Isolation cs236607

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? cs236607

Isolation Levels Connection.setTransactionIsolation(int level) 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: READ UNCOMMITTED READ COMMITTED REPEATABLE READ (default) SERIALIZABLE Each transaction determines its isolation level Connection.setTransactionIsolation(int level) cs236607

READ COMMITED & SERIALIZABLE SERIALIZABLE: Every reading of tuples puts shared locks on the scanned tuples and on the gaps between them (locking index entries) 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. 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) cs236607

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 the transaction to read changes that other transactions have made before the changes have been committed (practically, no isolation for read operations) cs236607

Some Definitions Dirty reads: A transaction reads data that is written by another, uncommitted transaction Non-repeatable reads: A transaction rereads data it previously read and finds that a committed transaction has modified or deleted that data 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

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

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

1. CREATE TABLE pairs (x INTEGER, y INTEGER); What Happens Here (1)? 1. CREATE TABLE pairs (x INTEGER, y INTEGER); 2. select * from pairs 3. insert into pairs values(1,1) 6. select * from pairs 8. 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

cs236607

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

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

What Happens Here (4)? 2. insert into pairs values(1,1) 4. COMMIT 1. CREATE TABLE pairs (x INTEGER, y INTEGER); 2. insert into pairs values(1,1) 4. COMMIT 5. select * from pairs 9. select * from pairs 10. COMMIT 3. select * from pairs 6. select * from pairs 7. insert into pairs values(1,2) 8. COMMIT T.2: REPEATABLE READ Only after the commit, T2 sees (1,1) T.1: REPEATABLE READ cs236607

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

Table-Level Locks cs236607

LOCK TABLES tbl-name AS alias WRITE 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 tbl-name AS alias WRITE cs236607

Table Locking in MySQL WRITE locks are exclusive READ locks are shared Writes (table updates) are given higher priority than reads (table retrievals) in order to avoid starvation mysql> LOCK TABLES my_table WRITE, temp_table WRITE; mysql> INSERT INTO my_table SELECT * FROM temp_table; mysql> DELETE FROM temp_table; mysql> UNLOCK TABLES; cs236607

Comparison Advantages of row-level locking: Fewer conflicts between transactions Smaller chances for rollbacks Possible to lock a single row for a long time Disadvantages of row-level locking: Requires more memory than table-level locks Slower when used on a large part of the table because it must acquire many more locks e.g., usually slower for GROUP BY operations and scans of the entire table cs236607

Lock Types TABLES There are four types of locks X – exclusive lock (for write actions) S – shared lock (for read actions) IX – an intension to acquire an exclusive lock IS – an intension to acquire a shared lock Lock X S IX IS Conflict TABLES ROWS cs236607

Updates by Concurrent Transactions cs236607

Write Locks in Updates In all four isolation levels, an exclusive (write) lock should be acquired in the following cases: On a row that should be deleted, prior to the deletion On an inserted row, immediately after the insertion On an updated row, prior to the update cs236607

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 Update can be applied only when T1 terminates (Rollback or Commit) T1 T2 cs236607

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

1. CREATE TABLE pairs (x INTEGER, y INTEGER); What Happens Here (7)? 1. CREATE TABLE pairs (x INTEGER, y INTEGER); 2. insert into pairs values(1,1) 3. COMMIT 5. update pairs set y=3 where x=1 “a blind write” 4. update pairs set y=2 where x=1 6. select * from pairs 7. COMMIT T.2: SERIALIZABLE T.1: SERIALIZABLE cs236607

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

Deadlocks A deadlock prevention mechanism Checks each operation and allows only operations that do not cause a deadlock (looks for cycles in the dependency graph) In case of a deadlock, terminates the “smallest” transaction (heuristically decides which one is the smallest) Prevents starvation (livelock) by terminating transactions that wait for a lock for more that some predefined waiting time cs236607

Links http://dev.mysql.com/doc/refman/5.1/en/sql-syntax-transactions.html http://dev.mysql.com/doc/refman/5.1/en/storage-engines.html cs236607