Presentation is loading. Please wait.

Presentation is loading. Please wait.

LAB: Web-scale Data Management on a Cloud

Similar presentations


Presentation on theme: "LAB: Web-scale Data Management on a Cloud"— Presentation transcript:

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

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

3 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

4 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

5 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 | | | | 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

6 Start (ad-hoc) Transaction
To disable autocommit mode for a single series of statements, use START TRANSACTION mysql> START TRANSACTION; mysql> > FROM table1 WHERE type=1; mysql> UPDATE table > SET 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

7 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 row in set (0.00 sec) mysql> COMMIT; Query OK, 0 rows affected (0.00 sec)

8 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 row in set (0.00 sec) mysql> COMMIT; Query OK, 0 rows affected (0.00 sec)

9 Isolation Levels Syntax
SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE } mysql> SELECT | | | REPEATABLE-READ | row in set (0.00 sec)

10 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 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)

11 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 row in set (0.00 sec) Empty set (0.00 sec) mysql> COMMIT; Query OK, 0 rows affected (0 sec)

12 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

13 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 = > WHERE id = 1; mysql> SELECT * -> FROM users mysql> ROLLBACK;

14 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 = > WHERE id = 1; mysql> COMMIT; mysql> SELECT * -> FROM users -> WHERE id = 1;

15 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;

16 Isolation Levels vs Read Phenomena
Dirty Read Nonrepeatable Read Phantom Uncommitted Possible Committed Not possible Repeatable (but unlikely) Serializable

17 Statements That Cannot Be Rolled Back
Data definition language (DDL) statements create or drop databases, create, drop, or alter tables Stored routines

18 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 ) Statements that implicitly use or modify tables in the mysql database. CREATE USER, DROP USER, and RENAME USER (~ MySQL ) Transaction-control and locking statements BEGIN, LOCK TABLES, SET autocommit = 1, START TRANSACTION, UNLOCK TABLES Data loading statements LOAD MASTER DATA, LOAD DATA INFILE

19 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

20 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 | | | | | rows in set (0.00 sec) mysql> rollback to s1; Query OK, 0 rows affected (0.00 sec) 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)

21 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.

22 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

23 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)

24 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 > 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

25 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

26 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 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

27 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

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


Download ppt "LAB: Web-scale Data Management on a Cloud"

Similar presentations


Ads by Google