LAB: Web-scale Data Management on a Cloud

Slides:



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

1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Accessing data Transactions. Agenda Questions from last class? Transactions concurrency Locking rollback.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
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
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
9 Copyright © 2009, Oracle. All rights reserved. Managing Data Concurrency.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
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
Transactions and Locks Lesson 22. Skills Matrix Transaction A transaction is a series of steps that perform a logical unit of work. Transactions must.
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
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
9 Copyright © 2007, Oracle. All rights reserved. Managing Data and Concurrency.
Concurrency and Transaction Processing. Concurrency models 1. Pessimistic –avoids conflicts by acquiring locks on data that is being read, so no other.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
1 Transactions Chapter Transactions A transaction is: a logical unit of work a sequence of steps to accomplish a single task Can have multiple.
1 IT420: Database Management and Organization Session Control Managing Multi-user Databases 24 March 2006 Adina Crăiniceanu
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 136 Database Systems I SQL Modifications and Transactions.
Module 11 Creating Highly Concurrent SQL Server® 2008 R2 Applications.
8 Copyright © 2005, Oracle. All rights reserved. Managing Data.
Transactions and Locks A Quick Reference and Summary BIT 275.
XA Transactions.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
Managing Concurrency in Web Applications
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Module 11: Managing Transactions and Locks
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
©Bob Godfrey, 2002, 2005 Lecture 17: Transaction Integrity and Concurrency BSA206 Database Management Systems.
3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.
Module 14: Managing Transactions and Locks. Overview Introducing Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
In this session, you will learn to: Implement triggers Implement transactions Objectives.
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
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Implicit Commit Statements That Cause an Implicit Commit The statements listed in this section (and any synonyms for them) implicitly end any transaction.
Managing Concurrency in Web Applications
Transaction Management and Concurrency Control
Introduction to Oracle9i: SQL
Isolation Levels Understanding Transaction Temper Tantrums
Transaction Properties
On transactions, and Atomic Operations
Batches, Transactions, & Errors
මොඩියුල විශ්ලේෂණය Transactions කළමනාකරණය.
Transactions, Locking and Query Optimisation
Chapter 10 Transaction Management and Concurrency Control
The PROCESS of Queries John Deardurff
Understanding Transaction Isolation Levels
On transactions, and Atomic Operations
The PROCESS of Queries John Deardurff
Batches, Transactions, & Errors
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Lecture 13: Transactions in SQL
Objectives Define and describe transactions
Transactions and Concurrency
About Wolf DBA for over 19 years At RDX for nearly 9
Isolation Levels Understanding Transaction Temper Tantrums
-Transactions in SQL -Constraints and Triggers
DB Concurrency ITEC 340 Database I Dr. Ian Barland
Lecture 11: Transactions in SQL
Presentation transcript:

LAB: Web-scale Data Management on a Cloud Lab 13. Transactions & Locks 2011/06/10

What is a Transaction? One execution of a user program A sequence of read & write operations ACID properties Atomic, durability, consistency, isolation

Transaction Syntax START TRANSACTION [WITH CONSISTENT SNAPSHOT] | BEGIN [WORK] COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE] ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE] SET autocommit = { 0 | 1 } SAVEPOINT identifier ROLLBACK [WORK] TO [SAVEPOINT] identifier RELEASE SAVEPOINT identifier http://dev.mysql.com/doc/refman/5.0/en/commit.html

Transaction-Safe Tables in MySQL InnoDB Supports transactions, row-level locking, and foreign keys BDB (BerkeleyDB) Supports transactions and page-level locking NDBCLUSTER Clustered, fault-tolerant, memory-based tables

Autocommit (Session) Variable By default, autocommit = 1; As soon as you execute a statement that updates (modifies) a table, MySQL stores the update on disk to make it permanent mysql> SELECT @@autocommit; +--------------+ | @@autocommit | +--------------+ | 1 | +--------------+ 1 row in set (0.00 sec) Disable autocommit mode mysql> SET autocommit = 0; Changes to tables are not made permanent immediately You must use COMMIT or ROLLBACK

Start (ad-hoc) Transaction To disable autocommit mode for a single series of statements, use START TRANSACTION mysql> START TRANSACTION; mysql> SELECT @A:=SUM(salary) -> FROM table1 WHERE type=1; mysql> UPDATE table2 -> SET summary=@A WHERE type=1; mysql> COMMIT; Autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK Autocommit mode then reverts to its previous state

Transaction Example T1 T2 mysql> START TRANSACTION; mysql> SELECT * FROM table1; Empty set (0.00 sec) mysql> INSERT INTO table1 VALUES (1); Query OK, 1 row affected (0.00 sec) +-------+ | attr1 | | 1 | 1 row in set (0.00 sec) mysql> COMMIT; Query OK, 0 rows affected (0.00 sec)

Transaction Example T1 T2 mysql> START TRANSACTION; mysql> INSERT INTO table1 VALUES (1); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM table1; +-------+ | attr1 | | 1 | 1 row in set (0.00 sec) mysql> COMMIT; Query OK, 0 rows affected (0.00 sec)

Isolation Levels Syntax SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE } mysql> SELECT @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set (0.00 sec) http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html

T1 (REPEATABLE READ) T2 T3 (SERIALIZABLE) mysql> START TRANSACTION; mysql> INSERT INTO table1 VALUES (1); Query OK, 1 row affected (0 sec) mysql> SELECT * FROM table1; +-------+ | attr1 | | 1 | 1 row in set (0.00 sec) Empty set (0.00 sec) ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> COMMIT; Query OK, 0 rows affected (0 sec)

T4 (READ COMMITTED) T5 T6 (READ UNCOMMITTED) mysql> START TRANSACTION; mysql> INSERT INTO table1 VALUES (1); Query OK, 1 row affected (0 sec) mysql> SELECT * FROM table1; +-------+ | attr1 | | 1 | 1 row in set (0.00 sec) Empty set (0.00 sec) mysql> COMMIT; Query OK, 0 rows affected (0 sec)

Isolation Levels Serializable Repeatable read Read Committed Lock-based concurrency control Non-lock based concurrency control Detect write collision, allow only one transaction to commit Snapshot isolation Repeatable read Phantom reads Read Committed Non-repeatable read Read Uncommitted Dirty read http://en.wikipedia.org/wiki/Isolation_(database_systems)

Read Phenomena Dirty read This occurs when a transaction is allowed to read data from a row that has been modified by another running transaction and not yet committed T1 T2 mysql> SELECT * -> FROM users -> WHERE id = 1; mysql> UPDATE users -> SET age = 21 -> WHERE id = 1; mysql> SELECT * -> FROM users mysql> ROLLBACK;

Read Phenomena Non-repeatable reads This occurs when read locks are not acquired when performing a SELECT, or when the acquired locks on affected rows are released as soon as the SELECT operation is performed T1 T2 mysql> SELECT * -> FROM users -> WHERE id = 1; mysql> UPDATE users -> SET age = 21 -> WHERE id = 1; mysql> COMMIT; mysql> SELECT * -> FROM users -> WHERE id = 1;

Read Phenomena Phantom reads This occurs when, in the course of a transaction, two identical queries are executed, and the collection of rows returned by the second query is different from the first T1 T2 mysql> SELECT * -> FROM users -> WHERE age BETWEEN 10 AND 30; mysql> INSERT INTO users -> VALUES (3,'Bob',27); mysql> COMMIT; mysql> SELECT * -> FROM users -> WHERE age BETWEEN 10 AND 30;

Isolation Levels vs Read Phenomena Dirty Read Nonrepeatable Read Phantom Uncommitted Possible Committed Not possible Repeatable (but unlikely) Serializable http://ftp.ku.ac.th/pub/mirror/mysql/books/mysqlpress/mysql-tutorial/ch10.html

Statements That Cannot Be Rolled Back Data definition language (DDL) statements create or drop databases, create, drop, or alter tables Stored routines http://dev.mysql.com/doc/refman/5.0/en/cannot-roll-back.html

Statements That Cause an Implicit Commit Data definition language (DDL) statements that define or modify database objects ALTER TABLE, CREATE INDEX, DROP INDEX, DROP TABLE, RENAME TABLE CREATE TABLE, CREATE DATABASE DROP DATABASE, and TRUNCATE TABLE (~ MySQL 5.0.8) ALTER PROCEDURE, CREATE PROCEDURE, and DROP PROCEDURE (~ MySQL 5.0.13) … Statements that implicitly use or modify tables in the mysql database. CREATE USER, DROP USER, and RENAME USER (~ MySQL 5.0.15) Transaction-control and locking statements BEGIN, LOCK TABLES, SET autocommit = 1, START TRANSACTION, UNLOCK TABLES Data loading statements LOAD MASTER DATA, LOAD DATA INFILE http://dev.mysql.com/doc/refman/5.0/en/implicit-commit.html

Savepoint Syntax SAVEPOINT identifier ROLLBACK [WORK] TO [SAVEPOINT] identifier RELEASE SAVEPOINT identifier SAVEPOINT statement sets a named transaction savepoint with a name of identifier ROLLBACK TO SAVEPOINT statement rolls back a transaction to the named savepoint without terminating the transaction RELEASE SAVEPOINT statement removes the named savepoint from the set of savepoints of the current transaction http://dev.mysql.com/doc/refman/5.0/en/savepoint.html

Savepoint T1 mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO table1 VALUES (1); Query OK, 1 row affected (0.00 sec) mysql> SAVEPOINT s1; Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO table1 VALUES (2); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM table1; +-------+ | attr1 | +-------+ | 1 | | 2 | +-------+ 2 rows in set (0.00 sec) mysql> rollback to s1; Query OK, 0 rows affected (0.00 sec) +-------+ 1 row in set (0.00 sec) mysql> rollback; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM table1; Empty set (0.00 sec)

What is a Lock? A small bookkeeping object associated with a DB object Shared lock several transactions can have shared locks on the same DB object. Exclusive lock only one transaction can have an exclusive lock on a DB object.

Lock Syntax LOCK TABLES tbl_name [[AS] alias ] lock_type [, tbl_name [[AS] alias ] lock_type ] ... lock_type: READ [LOCAL] | [LOW_PRIORITY] WRITE UNLOCK TABLES http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html

Lock & Unlock Tables LOCK TABLES explicitly acquires table locks for the current client session UNLOCK TABLES explicitly releases any table locks held by the current session. A table lock protects only against inappropriate reads or writes by other session For view locking, LOCK TABLES adds all base tables used in the view and locks them automatically mysql> LOCK TABLES table1 READ; Query OK, 0 rows affected (0.00 sec) mysql> LOCK TABLES table2 WRITE; Query OK, 0 rows affected (0.00 sec) mysql> UNLOCK TABLES; Query OK, 0 rows affected (0.00 sec)

LOCK TABLES and Triggers If you lock a table explicitly with LOCK TABLES, any tables used in triggers are also locked implicitly mysql> LOCK TABLES t1 WRITE, t2 READ; mysql> CREATE TRIGGER t1_a_ins AFTER INSERT ON t1 FOR EACH ROW -> BEGIN -> UPDATE t4 SET count = count+1 -> WHERE id = NEW.id AND -> EXISTS (SELECT a FROM t3); -> INSERT INTO t2 VALUES(1, 2); -> END; t1 is locked for writing explicitly, t2 is locked for writing explicitly, t3 is locked for reading implicitly, t4 is locked for writing implicitly http://dev.mysql.com/doc/refman/5.0/en/lock-tables-and-triggers.html

Rules for Lock Acquisition READ lock The session that holds the lock can read the table (but not write it) Multiple sessions can acquire a READ lock for the table at the same time Other sessions can read the table without explicitly acquiring a READ lock WRITE lock The session that holds the lock can read and write the table Only the session that holds the lock can access the table. No other session can access it until the lock is released Lock requests for the table by other sessions block while the WRITE lock is held If the LOCK TABLES statement must wait due to locks held by other sessions on any of the tables, it blocks until all locks can be acquired A session that requires locks must acquire all the locks that it needs in a single LOCK TABLES statement While the locks thus obtained are held, the session can access only the locked tables

SESSION 1 SESSION 2 mysql> LOCK TABLES table1 READ; mysql> INSERT INTO table1 VALUES (1); (... pending ...) mysql> UNLOCK TABLES; Query OK, 1 row affected (1 min 37.82 sec) SESSION 1 SESSION 2 mysql> LOCK TABLES table1 READ; mysql> LOCK TABLES table1 WRITE; (... pending ...) mysql> UNLOCK TABLES; Query OK, 0 rows affected (20.01 sec) SESSION 1 SESSION 2 mysql> LOCK TABLES table1 WRITE; mysql> LOCK TABLES table1 READ; (or WRITE) (... pending ...) mysql> UNLOCK TABLES; Query OK, 0 rows affected (38.36 sec) SESSION 1 mysql> LOCK TABLES table1 READ; mysql> SELECT * FROM table2; ERROR 1100 (HY000): Table ‘table2' was not locked with LOCK TABLES

Rules for Lock Release A session can release its locks explicitly with UNLOCK TABLES If a session issues a LOCK TABLES statement to acquire a lock while already holding locks, its existing locks are released implicitly before the new locks are granted If a session begins a transaction (for example, with START TRANSACTION), an implicit UNLOCK TABLES is performed, which causes existing locks to be released

Exercise 자신의 데이터베이스에 다음과 같은 테이블(users)을 만드세요. MySQL의 두 세션을 여세요. Isolation level을 변경하여 dirty reads, non-repeatable reads, phantom reads 현상을 보여주세요. id name age 1 Joe 20 2 Jill 25