事务的基本要素
- 原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做,不可能停滞在中间环节。事务执行过程中出错,会回滚到事务开始前的状态,所有的操作就像没有发生一样。也就是说事务是一个不可分割的整体,就像化学中学过的原子,是物质构成的基本单位。
- 一致性(Consistency):事务开始前和结束后,数据库的完整性约束没有被破坏 。比如A向B转账,不可能A扣了钱,B却没收到。
- 隔离性(Isolation):同一时间,只允许一个事务请求同一数据,不同的事务之间彼此没有任何干扰。比如A正在从一张银行卡中取钱,在A取钱的过程结束前,B不能向这张卡转账。
- 持久性(Durability):事务完成后,事务对数据库的所有更新将被保存到数据库,不能回滚。
MySQL的支持
MySQL只在InnoDB中支持事务
四种隔离级别
- 未提交读(uncommitted read)
- 提交后读(committed read)
- 可重复读(repeatable read)
- 串行化(serializable read)
隔离说明
隔离级别 | 脏读 | 不可重复度 | 幻读 |
---|---|---|---|
读未提交(read-uncommitted) | 是 | 是 | 是 |
读已提交(read-committed) | 否 | 是 | 是 |
可重复读(repeatable-read) | 否 | 否 | 是 |
可串行化(serializable) | 否 | 否 | 否 |
- 脏读(duty read):读到没有提交的事务提交的数据
- 不可重复读(unrepeatable read):在一个事务内,读到的同一条数据可能不一样
- 幻读(dream read):事务A 按照一定条件进行数据读取, 期间事务B 插入了相同搜索条件的新数据,事务A再次按照原先条件进行读取时,发现了事务B 新插入的数据 称为幻读
实验
环境:Win7+MySQL5.7
mysql> desc test;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.09 sec)
mysql> SELECT * FROM test;
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | asdasd123123adasd |
+----+-------------------+
1 row in set (0.00 sec)
读未提交
事务A
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test where id =1;
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | asdasd123123adasd |
+----+-------------------+
1 row in set (0.00 sec)
事务B
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update test set name="test_b" where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
事务A
mysql> select * from test where id = 1;
+----+--------+
| id | name |
+----+--------+
| 1 | test_b |
+----+--------+
1 row in set (0.00 sec)
结论
- 事务A在事务中两次查询数据不一样
- 事务B还未提交,事务A的结果已经变化
- 未提交读是脏读
读后提交
事务A
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test where id = 1;
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | asdasd123123adasd |
+----+-------------------+
1 row in set (0.00 sec)
事务B
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update test set name="test_b" where id = 1;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
事务A
mysql> select * from test where id = 1;
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | asdasd123123adasd |
+----+-------------------+
1 row in set (0.00 sec)
事务B
mysql> commit;
Query OK, 0 rows affected (0.07 sec)
事务A
mysql> select * from test where id = 1;
+----+--------+
| id | name |
+----+--------+
| 1 | test_b |
+----+--------+
1 row in set (0.00 sec)
结论
- 事务A在事务B执行更新语句后查询结果一致
- 事务A在事务B提交后查询结果改变
- 提交后读是不可重复读的
可重复读
事务A
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test where id = 1;
+----+--------+
| id | name |
+----+--------+
| 1 | test_a |
+----+--------+
1 row in set (0.00 sec)
事务B
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update test set name="test_b" where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
事务A
mysql> select * from test where id = 1;
+----+--------+
| id | name |
+----+--------+
| 1 | test_a |
+----+--------+
1 row in set (0.00 sec)
事务B
mysql> commit;
Query OK, 0 rows affected (0.06 sec)
事务A
mysql> select * from test where id = 1;
+----+--------+
| id | name |
+----+--------+
| 1 | test_a |
+----+--------+
1 row in set (0.00 sec)
结论
- 事务A中的查询结果在事务B提交前后都不会变化
- 可重复读-可重复读
幻读问题
![](https://img.haomeiwen.com/i15329462/2c5dfc6a289eb30b.png)
上图能出现幻读
总结
隔离级别中没有试验系列化,个人觉着意义不大,上述试验能说明,隔离级别已经能深刻的说明问题,加油
网友评论