READ UNCOMMITTED 读取未提交的内容
在READ UNCOMMITTED隔离级,所有的事务都可以“看到”未提交事务的执行结果。在这种级别上,可能会产生很多问题,除非用户真的知道自己在做什么,并有很好的理由这样做。本隔离级很少用于实际应用,因为它的性能也不比其他级别好多少,而别的级别还有其他更多的优点。读取未提交数据,也被称为“脏读”(Dirty Read)
READ COMMITTED (读取提交内容)
大多数数据库系统的默认隔离级是READ COMMITED(但这不是MySql默认的)。它满足了隔离的早先简单定义:一个事务在开始时,只能“看见”已经提交事务所做的改变,一个事务从开始到提交前,所做的任何数据改变都是不可见的,除非已经提交。这种隔离级别也支持所谓的“不可重复读”(NonrepeatableRead)。这意味着用户运行同一语句两次,看到的结果是不同的。
REPEATABLE READ(可重读)
REPEATABLE READ 隔离级解决了READ UNCOMMITTED隔离导致的问题。他确保同一事务的多个实例在并发读取数据时,看“看到同样的”数据行。不过理论上。这会导致另一个棘手的问题:幻读(Phantom Read)。简单来说,幻读指当前用户读取某一范围的数据行时,另一个事务又在该范围内插入了新行,当用户再读取该范围内的数据行时,看发现有新的“幻影”(Phantom)行。InnoDB和Falcon存储引擎通过多个版本并发控制(Multiversion Conccurency Control)机制解决了幻读问题。REPEATABLE READ 是MySql的默认事务隔离级别。InnoDB和Falcon存储引擎都遵循这种设置
SERIALIZABLE (可串行化)
SERIALIZABLE是最高级别的隔离级别,他通过强制的事务排序,使之不可能相互冲突,从而解决幻读问题。简言之,SERIALIZABLE是在每个读的数据行上加锁。在这个隔离级别,可导致大量的超时(Timeout)现象和锁竞争(Lock Contention)现象。
然后说说修改事务隔离级别的方法:
1.全局修改,修改my.ini配置文件,在最后加上
- #可选参数有:READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE.
- [mysqld]
- transaction-isolation = REPEATABLE-READ
这里全局默认是REPEATABLE-READ,其实MySQL本来默认也是这个级别
2.对当前session修改,在登录mysql客户端后,执行命令:
- -- 修改当前会话的隔离级别
- SET SESSION TRANSACTION ISOLATION LEVEL read UNCOMMITTED;
3、查询数据库的隔离级别
- SELECT @@TX_ISOLATION;
要记住mysql有一个autocommit参数,默认是on,他的作用是每一条单独的查询都是一个事务,并且自动开始,自动提交(执行完以后就自动结束了,如果你要适用select for update,而不手动调用 start transaction,这个for update的行锁机制等于没用,因为行锁在自动提交后就释放了),所以事务隔离级别和锁机制即使你不显式调用start transaction,这种机制在单独的一条查询语句中也是适用的,分析锁的运作的时候一定要注意这一点
再来说说锁机制:
共享锁:由读表操作加上的锁,加锁后其他用户只能获取该表或行的共享锁,不能获取排它锁,也就是说只能读不能写
排它锁:由写表操作加上的锁,加锁后其他用户不能获取该表或行的任何锁,典型是mysql事务中的
锁的范围:
行锁: 对某行记录加上锁
表锁: 对整个表加上锁
这样组合起来就有,行级共享锁,表级共享锁,行级排他锁,表级排他锁
- start transaction;
- select * from user where userId = 1 for update;
执行完这句以后
1)当其他事务想要获取共享锁,比如事务隔离级别为SERIALIZABLE的事务,执行
- select * from user;
将会被挂起,因为SERIALIZABLE的select语句需要获取共享锁
2)当其他事务执行
- select * from user where userId = 1 for update;
- update user set userAge = 100 where userId = 1;
也会被挂起,因为for update会获取这一行数据的排它锁,需要等到前一个事务释放该排它锁才可以继续进行
下面来说说不同的事务隔离级别的实例效果,例子使用InnoDB,开启两个客户端A,B,在A中修改事务隔离级别,在B中开启事务并修改数据,然后在A中的事务查看B的事务修改效果:
1.READ-UNCOMMITTED(读取未提交内容)级别
1、A修改事务级别并开始事务,对user表做一次查询
- mysql> select @@TX_ISOLATION;
- +------------------+
- | @@TX_ISOLATION |
- +------------------+
- | READ-UNCOMMITTED |
- +------------------+
- 1 row in set (0.00 sec)
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
2、B更新一条记录
- mysql> start transaction;
- Query OK, 0 rows affected (0.00 sec)
- mysql> update user set age = 10 where id = 3;
- Query OK, 1 row affected (0.03 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 10 |
- +----+------+
- 3 rows in set (0.00 sec)
3、此时B事务还未提交,A在事务内做一次查询,发现查询结果已经改变
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 10 |
- +----+------+
- 3 rows in set (0.00 sec)
4、B进行事务回滚
- mysql> rollback;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
5、A再做一次查询,查询结果又变回去了
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
6、A表对user表数据进行修改
- mysql> update user set age=10 where id = 3;
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
7、B表重新开始事务后,对user表记录进行修改,修改被挂起,直至超时,但是对另一条数据的修改成功,说明A的修改对user表的数据行加行共享锁(因为可以使用select)
- mysql> update user set age = 10 where id = 3;
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- mysql> update user set age = 10 where id = 2;
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from user where id = 3;
- +----+------+
- | id | age |
- +----+------+
- | 3 | 10 |
- +----+------+
- 1 row in set (0.00 sec)
可以看出READ-UNCOMMITTED隔离级别,当两个事务同时进行时,即使事务没有提交,所做的修改也会对事务内的查询做出影响,这种级别显然很不安全。但是在表对某行进行修改时,会对该行加上行共享锁
2. READ-COMMITTED(读取提交内容)
1、设置A的事务隔离级别,并进入事务做一次查询
- mysql> SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
- Query OK, 0 rows affected (0.00 sec)
- mysql> SELECT @@TX_ISOLATION;
- +----------------+
- | @@TX_ISOLATION |
- +----------------+
- | READ-COMMITTED |
- +----------------+
- 1 row in set (0.00 sec)
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
2、B开始事务,并对记录进行修改
- mysql> start transaction;
- Query OK, 0 rows affected (0.00 sec)
- mysql> update user set age = 10 where id = 3;
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 10 |
- +----+------+
- 3 rows in set (0.00 sec)
3、A再对user表进行查询,发现记录没有受到影响
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
4、B提交事务
- mysql> commit;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 10 |
- +----+------+
- 3 rows in set (0.00 sec)
5、A再对user表查询,发现记录被修改
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 10 |
- +----+------+
- 3 rows in set (0.00 sec)
6、A对user表进行修改
- mysql> start transaction;
- Query OK, 0 rows affected (0.00 sec)
- mysql> update user set age=100 where id = 3;
- Query OK, 0 rows affected (0.00 sec)
- Rows matched: 1 Changed: 0 Warnings: 0
7、B重新开始事务,并对user表同一条进行修改,发现修改被挂起,直到超时,但对另一条记录修改,却是成功,说明A的修改对user表加上了行共享锁(因为可以select)
- mysql> start transaction;
- Query OK, 0 rows affected (0.00 sec)
- mysql> update user set age=20 where id = 3;
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- mysql> update user set age=20 where id = 2;
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 20 |
- | 3 | 100 |
- +----+------+
- 3 rows in set (0.00 sec)
READ-COMMITTED事务隔离级别,只有在事务提交后,才会对另一个事务产生影响,并且在对表进行修改时,会对表数据行加上行共享锁
3. REPEATABLE-READ(可重读)
1、A设置事务隔离级别,进入事务后查询一次
- mysql> SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
- Query OK, 0 rows affected (0.00 sec)
- mysql> SELECT @@TX_ISOLATION;
- +-----------------+
- | @@TX_ISOLATION |
- +-----------------+
- | REPEATABLE-READ |
- +-----------------+
- 1 row in set (0.00 sec)
- mysql> start transaction;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
2、B开始事务,并对user表进行修改
- mysql> start transaction;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
- mysql> update user set age=10 where id=3;
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 10 |
- +----+------+
- 3 rows in set (0.00 sec)
3、A查看user表数据,数据未发生改变
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
4、B提交事务
- mysql> commit;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 10 |
- +----+------+
- 3 rows in set (0.00 sec)
5、A再进行一次查询,结果还是没有变化
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
6、A提交事务后,再查看结果,结果已经更新
- mysql> commit;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 10 |
- +----+------+
- 3 rows in set (0.00 sec)
7、A重新开始事务,并对user表进行修改
- mysql> start transaction;
- Query OK, 0 rows affected (0.00 sec)
- mysql> update user set age = 100 where id = 3;
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
8、B表重新开始事务,并对user表进行修改,修改被挂起,直到超时,对另一条记录修改却成功,说明A对表进行修改时加了行共享锁(可以select)
- mysql> update user set age = 100 where id = 3;
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- mysql> update user set age = 100 where id = 2;
- Query OK, 1 row affected (0.02 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 100 |
- | 3 | 10 |
- +----+------+
- 3 rows in set (0.00 sec)
REPEATABLE-READ事务隔离级别,当两个事务同时进行时,其中一个事务修改数据对另一个事务不会造成影响,即使修改的事务已经提交也不会对另一个事务造成影响。在事务中对某条记录修改,会对记录加上行共享锁,直到事务结束才会释放。
4.SERIERLIZED(可串行化)
1、修改A的事务隔离级别,并作一次查询
- mysql> SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;
- Query OK, 0 rows affected (0.00 sec)
- mysql> SELECT @@TX_ISOLATION;
- +----------------+
- | @@TX_ISOLATION |
- +----------------+
- | SERIALIZABLE |
- +----------------+
- 1 row in set (0.00 sec)
- mysql> start transaction;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
2、B对表进行查询,正常得出结果,可知对user表的查询是可以进行的
- mysql> select * from user;
- +----+------+
- | id | age |
- +----+------+
- | 1 | 1 |
- | 2 | 2 |
- | 3 | 3 |
- +----+------+
- 3 rows in set (0.00 sec)
3、B开始事务,并对记录做修改,因为A事务未提交,所以B的修改处于等待状态,等待A事务结束,最后超时,说明A在对user表做查询操作后,对表加上了共享锁
- mysql> start transaction;
- Query OK, 0 rows affected (0.00 sec)
- mysql> update user set age = 10 where id = 3;
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
SERIALIZABLE事务隔离级别最严厉,在进行查询时就会对表或行加上共享锁,其他事务对该表将只能进行读操作,而不能进行写操作。