Mysql

Mysql InnoDB事物隔离机制

2018-05-06  本文已影响22人  君子世无双

1. 事务(Transaction)及其ACID属性

事务是由一组SQL语句组成的逻辑处理单元,事务具有4属性,通常称为事务的ACID属性。

2. 默认是可重复读的(REPEATABLE READ)

3. 提供四个隔离级别,可以使用--transaction-isolation为所有连接指定事物隔离级别

 transaction-isolation = {READ-UNCOMMITTED | READ-COMMITTED | REPEATABLE-READ | SERIALIZABLE}

4. 理论知识图谱

隔离级别 脏读(Dirty Read) 不可重复读(NonRepeatable Read) 幻读(Phantom Read)
未提交读(Read uncommitted)
已提交读(Read committed)
可重复读(Repeated Read)
可串行化(Serializable)

4. 隔离级别介绍

5.脏读,不可重复读, 幻读介绍

session1:
MariaDB [blog]> select @@session.tx_isolation;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| REPEATABLE-READ        |
+------------------------+
1 row in set (0.00 sec)
MariaDB [blog]> start transaction;
1 row in set (0.00 sec)
insert into articles(title, content) values("a", "zzz");
1 row in set (0.00 sec)

-----------------------------------------------------------------------------
session2:
MariaDB [blog]> set session transaction isolation level read uncommitted;
MariaDB [blog]> select * from articles;
+----+-------+---------+---------------------+---------------------+
| id | title | content | created_at          | updated_at          |
+----+-------+---------+---------------------+---------------------+
|  1 | a     | zzz     | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+----+-------+---------+---------------------+---------------------+
1 row in set (0.00 sec)
MariaDB [blog]> set session transaction isolation level REPEATABLE READ;
Query OK, 0 rows affected (0.00 sec)
MariaDB [blog]> select * from articles;
Empty set (0.00 sec)
session1:
MariaDB [blog]> select @@session.tx_isolation;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| REPEATABLE-READ        |
+------------------------+
1 row in set (0.00 sec)
-----------------------------------------------------------------------------
session2:
MariaDB [blog]> select @@session.tx_isolation;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| READ-UNCOMMITTED       |
+------------------------+
1 row in set (0.00 sec)
MariaDB [blog]> start transaction;
Query OK, 0 rows affected (0.00 sec)

MariaDB [blog]> select * from articles where id = 2;
+----+-------+---------+---------------------+---------------------+
| id | title | content | created_at          | updated_at          |
+----+-------+---------+---------------------+---------------------+
|  2 | z     | zzz     | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+----+-------+---------+---------------------+---------------------+
1 row in set (0.00 sec)
-----------------------------------------------------------------------------
session1:
MariaDB [blog]> update articles set title = "b" where id = 2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
-----------------------------------------------------------------------------
session2:
MariaDB [blog]> select * from articles where id = 2;
+----+-------+---------+---------------------+---------------------+
| id | title | content | created_at          | updated_at          |
+----+-------+---------+---------------------+---------------------+
|  2 | b     | zzz     | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+----+-------+---------+---------------------+---------------------+
1 row in set (0.00 sec)
-----------------------------------------------------------------------------
session1:
MariaDB [blog]> start transaction;
Query OK, 0 rows affected (0.00 sec)
MariaDB [blog]> select * from articles where id = 2;
+----+-------+---------+---------------------+---------------------+
| id | title | content | created_at          | updated_at          |
+----+-------+---------+---------------------+---------------------+
|  2 | b     | zzz     | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+----+-------+---------+---------------------+---------------------+
1 row in set (0.00 sec)
-----------------------------------------------------------------------------
session2:
MariaDB [blog]> update articles set title = 'a' where id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
-----------------------------------------------------------------------------
session1:
MariaDB [blog]> select * from articles where id = 2;
+----+-------+---------+---------------------+---------------------+
| id | title | content | created_at          | updated_at          |
+----+-------+---------+---------------------+---------------------+
|  2 | b     | zzz     | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+----+-------+---------+---------------------+---------------------+
1 row in set (0.00 sec)
session1:
MariaDB [blog]> select @@session.tx_isolation;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| REPEATABLE-READ        |
+------------------------+
1 row in set (0.00 sec)
MariaDB [blog]> start transaction;
Query OK, 0 rows affected (0.00 sec)
-----------------------------------------------------------------------------
session2:
MariaDB [blog]> set session transaction isolation level read uncommitted;
Query OK, 0 rows affected (0.00 sec)
MariaDB [blog]> start transaction;
Query OK, 0 rows affected (0.00 sec)
MariaDB [blog]> update articles set title = 'zzz';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
MariaDB [blog]> select * from articles;
+----+-------+---------+---------------------+---------------------+
| id | title | content | created_at          | updated_at          |
+----+-------+---------+---------------------+---------------------+
|  2 | zzz   | zzz     | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+----+-------+---------+---------------------+---------------------+
1 row in set (0.00 sec)
-----------------------------------------------------------------------------
session1:
MariaDB [blog]> insert into articles(title) values("as");
Query OK, 1 row affected, 2 warnings (0.01 sec)
-----------------------------------------------------------------------------
session2:
MariaDB [blog]> select * from articles;
+----+-------+---------+---------------------+---------------------+
| id | title | content | created_at          | updated_at          |
+----+-------+---------+---------------------+---------------------+
|  2 | zzz   | zzz     | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
|  9 | as    | NULL    | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+----+-------+---------+---------------------+---------------------+
2 rows in set (0.00 sec)
上一篇 下一篇

猜你喜欢

热点阅读