MYSQL事务-隔离级别
事务是什么?
事务简言之就是一组SQL执行要么全部成功,要么全部失败。MYSQL的事务在存储引擎层实现。
事务都有ACID特性:
[if !supportLists]· [endif]
原子性(Atomicity):一个事务必须被视为一个不可分割的单元;
[if !supportLists]· [endif]
[if !supportLists]· [endif]
一致性(Consistency):数据库总是从一种状态切换到另一种状态;
[if !supportLists]· [endif]
[if !supportLists]· [endif]
隔离性(Isolation):通常来说,事务在提交前对于其他事务不可见;
[if !supportLists]· [endif]
[if !supportLists]· [endif]
持久性(Durablity):一旦事务提交,所做修改永久保存数据库;
[if !supportLists]· [endif]
事务最常用的例子就是银行转账。假设polo需给lynn转账1000元,如下步骤:
[if !supportLists]· [endif]
确认polo账户余额高于1000元;
[if !supportLists]· [endif]
[if !supportLists]· [endif]
从polo的账户余额减去1000元;
[if !supportLists]· [endif]
[if !supportLists]· [endif]
将lynn的账户余额增加1000元;
[if !supportLists]· [endif]
SQL语句如下:
mysql> BEGIN;mysql> SELECT balance FROM bank_account WHERE uid=10001;mysql> UPDATE bank_account SET balance=balance-1000 WHERE uid=10001;mysql> UPDATE bank_account SET balance=balance+1000 WHERE uid=10002;mysql> COMMIT;
注:mysql启动事务可使用BEGIN或者START TRANSACTION;
上述三个步骤执行在一个事务中就能够保证数据的完整性,要么全部成功,要么全部失败。
MYSQL提供两种事务型引擎:Innodb和NDBCluster。默认采用自动提交模式,执行一条语句自动COMMIT。通过AUTOCOMMIT变量可启用或者禁用自动提交模式:
mysql> SHOW VARIABLES LIKE "AUTOCOMMIT";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+1 row in set (0.00 sec)
mysql> SET AUTOCOMMIT=1
AUTOCOMMIT=1表示开启默认提交,0表示关闭默认提交需要手动提交。
事务隔离级别
事务隔离性的解释:通常情况下,事务在提交之前对于其他事务不可见。
数据库有四种隔离级别,当然MYSQL也是如此。分别为:
[if !supportLists]· [endif]
READ UNCOMMITED(未提交读)
[if !supportLists]· [endif]
[if !supportLists]· [endif]
READ COMMITED(已提交读)
[if !supportLists]· [endif]
[if !supportLists]· [endif]
EPEATABLE READ(可重复读)
[if !supportLists]· [endif]
[if !supportLists]· [endif]
SEAIALIZABLE(可串行化)
[if !supportLists]· [endif]
本人理解 : 隔离级别就是决定一个事务的修改另一个事务什么情况下可见。
书本解释 : 隔离级别都规定了一个事务中所做修改,哪些在事务内和事务间是可见的。
上面两段理解的区别在于是否存在事务内可见性的规定。我在各个级别似乎没有看到
下面开始说明MYSQL的四种隔离级别,先准备一张学生表:
mysql> CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
只有id(主键自增)与name字段
[if !supportLists]· [endif]
READ UNCOMMITTED(未提交读)
[if !supportLists]· [endif]
事务中修改没有提交对其他事务也是可见的,俗称脏读。非常不建议使用。
[if !supportLists]· [endif]
实例演示
[if !supportLists]· [endif]
客户端A和B设置隔离级别为未提交读
[if !supportLists]· [endif]
mysql> SET SESSION TX_ISOLATION='READ-UNCOMMITTED';
[if !supportLists]· [endif]
客户端A与B开启事务并查询student
[if !supportLists]· [endif]
mysql> BEGIN;mysql> SELECT * FROM student;
Empty set (0.00 sec)
[if !supportLists]· [endif]
客户端A和B都是空数据
[if !supportLists]· [endif]
客服端B插入一条新的数据
[if !supportLists]· [endif]
mysql> INSERT INTO student(name) VALUES("polo");Query OK, 1 row affected (0.00 sec)
[if !supportLists]· [endif]
此时事务未提交,客服端A查看student表
[if !supportLists]· [endif]
$ SELECT * FROM student;
mysql> SELECT * FROM student;
+----+------+
| id | name |
+----+------+
| 1 | polo |
+----+------+1 row in set (0.00 sec)
[if !supportLists]· [endif]
客户端A看到B未提交的修改
[if !supportLists]· [endif]
客户端B执行回滚操作
[if !supportLists]· [endif]
mysql> ROLLBACK
[if !supportLists]· [endif]
成功之后,客户端A查看student表
[if !supportLists]· [endif]
mysql> SELECT * FROM student;Empty set (0.00 sec)
[if !supportLists]· [endif]
客户端A查看数据为空
[if !supportLists]· [endif]
以上可以看出未提交读隔离级别的危险性,对于一个没有提交事务所做修改对另一个事务是可见状态,容易造成脏读。非特殊情况不得使用此级别
[if !supportLists]· [endif]
[if !supportLists]· [endif]
READ COMMITTED(提交读)多数数据库系统默认为此级别(MYSQL不是)。已提交读级别即为一个事务只能已提交事务所做的修改,也就解决了未提交读的问题,即脏读的问题。
[if !supportLists]· [endif]
实例演示
[if !supportLists]· [endif]
客户端A和B设置隔离级别为已提交读
[if !supportLists]· [endif]
mysql> SET SESSION TX_ISOLATION='READ-COMMITTED';
[if !supportLists]· [endif]
客户端A与B开启事务并查询student
[if !supportLists]· [endif]
mysql> BEGIN;mysql> SELECT * FROM student;
Empty set (0.00 sec)
[if !supportLists]· [endif]
客户端A和B都为空
[if !supportLists]· [endif]
客服端B插入一条新的数据,不提交
[if !supportLists]· [endif]
mysql> INSERT INTO student (name) VALUES('polo');
[if !supportLists]· [endif]
客户端A查看student
[if !supportLists]· [endif]
mysql> SELECT * FROM student;Empty set (0.00 sec)
[if !supportLists]· [endif]
注意这里与上面不同了,在客户端B没有提交事务情况下无数据
[if !supportLists]· [endif]
下面客户端B提交事务
[if !supportLists]· [endif]
mysql> COMMIT;
[if !supportLists]· [endif]
客户端A再次查看student表。
[if !supportLists]· [endif]
mysql> SELECT * FROM student;
+----+------+
| id | name |
+----+------+
| 1 | polo |
+----+------+1 row in set (0.00 sec)
[if !supportLists]· [endif]
成功读取到客户
[if !supportLists]· [endif]
从上面的示例可以看出,提交读没有了未提交读的问题,但我们可以看到在客户端A的一个事务中执行两次同样的SELECT语句得到不同结果,因此已提交读又被称为不可重复读。同样筛选条件可能得到不同的结果。
[if !supportLists]· [endif]
[if !supportLists]· [endif]
REPEATABLE READ(可重复读)
[if !supportLists]· [endif]
如其名也,解决已提交读不可重复读取的问题。
[if !supportLists]· [endif]
示例演示
[if !supportLists]· [endif]
客户端A和B设置隔离级别为可重复读
[if !supportLists]· [endif]
mysql> SET SESSION tx_isolation='REPEATABLE-READ'
[if !supportLists]· [endif]
客户端A与B开启事务并查看
[if !supportLists]· [endif]
mysql> BEGIN;
mysql> SELECT * FROM student;
+----+------+
| id | name |
+----+------+
| 1 | polo |
+----+------+1 rows in set (0.00 sec)
[if !supportLists]· [endif]
客服端B更新polo为jeff,并提交事务
[if !supportLists]· [endif]
mysql> UPDATE student SET name='jeff' WHERE id=1;mysql> COMMIT
[if !supportLists]· [endif]
客户端A查看student表
[if !supportLists]· [endif]
mysql> SELECT * FROM student;
+----+------+
| id | name |
+----+------+
| 1 | jeff |
+----+------+1 rows in set (0.00 sec)
[if !supportLists]· [endif]
注意客户端A查看数据未变,没有不可重复读问题
[if !supportLists]· [endif]
客户端A提交事务,并查看student表
[if !supportLists]· [endif]
mysql> COMMIT;
mysql> SELECT * FROM student;
+----+------+
| id | name |
+----+------+
| 1 | polo |
+----+------+1 rows in set (0.00 sec)
[if !supportLists]· [endif]
上面实例可知,可重复读两次读取内容一样。数据库这级别并没有解决幻读的问题。但是MYSQL在可重复读基础上增加了MVCC机制解决了此问题,实例无法演示幻读效果。
[if !supportLists]· [endif]
那什么是幻读?首先,可重复读锁定范围为当前查询到的内容,如执行
[if !supportLists]· [endif]
mysql> SELECT * FROM student WHERE id>=1
[if !supportLists]· [endif]
锁定的即id>=1查到的行,为行级锁。如另一事务执行并默认提交以下语句
[if !supportLists]· [endif]
mysql> INSERT INTO student (name) VALUES ('peter');
[if !supportLists]· [endif]
新增的这行并没有被锁定,此时读取student
[if !supportLists]· [endif]
mysql> SELECT * FROM student WHERE id>=1;
+----+---------+| id | name |
+----+---------+
| 1 | polo || 2 | peter |
+----+---------+
2 rows in set (0.00 sec)
[if !supportLists]· [endif]
便出现了幻读
[if !supportLists]· [endif]
除了使用MYSQL的MVCC机制,还可以使用可串行化隔离级别解决此问题。
[if !supportLists]· [endif]
[if !supportLists]· [endif]
SEAIALIZABLE(可串行化)
[if !supportLists]· [endif]
可串行化是最高隔离级别,强制事务串行执行。执行串行了也就解决了一切的问题,这个级别只有在对数据一致性要求非常严格且没用并发的情况下使用
[if !supportLists]· [endif]
实例演示
[if !supportLists]· [endif]
客户端A和B设置隔离级别为可串行化
[if !supportLists]· [endif]
mysql> SET SESSION tx_isolation='SERIALIZABLE';
[if !supportLists]· [endif]
客户端A执行查询
[if !supportLists]· [endif]
mysql> SELECT * FROM student WHERE id<4;
+----+---------+| id | name |
+----+---------+
| 1 | polo || 2 | peter |
+----+---------+
2 rows in set (0.00 sec)
[if !supportLists]· [endif]
客户端B执行新增
[if !supportLists]· [endif]
mysql> INSERT INTO student (name) VALUES('tiger');
[if !supportLists]· [endif]
好的!效果出现了,此时我们会发现INSERT语句被阻塞执行,原因就是A执行了查询表student同时满足id<4,已被锁定。如果查询表student条件为id<3,则新增语句可正常执行。
[if !supportLists]· [endif]
隔离级别对照图
隔离级别 脏读 不可重复读 幻读 加锁读
未提交读 read uncommited YES YES YES NO
提交读 read commited NO YES YES NO
可重复读 repeatable read NO NO YES NO
线性化 seaializable NO NO NO YES