开发工作中我们会使用到事务,那你们知道事务又分哪几种吗?
MYSQL标准定义了4类隔离级别,用来限定事务内外的哪些改变是可见的,哪些是不可见的。
低的隔离级一般支持更高的并发处理,并拥有更低的系统开销。
隔离级别由低到高:Read Uncommitted Read Uncommitted(读取未提交内容)
在该隔离级别,所有事务都可以看到其他未提交(commit)事务的执行结果。
本隔离级别很少用于实际应用,因为它的性能也不比其他级别好多少。
读取未提交的数据,也被称之为脏读(Dirty Read)。[窗口A]:<br>
<br>
mysql> set GLOBAL tx_isolation='READ-UNCOMMITTED';<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
mysql> quit;<br>
Bye<br>
<br>
[root@vagrant-centos65 ~]# mysql -uroot -pxxxx(重新登录)<br>
<br>
mysql> SELECT @@tx_isolation;<br>
+------------------+<br>
| @@tx_isolation |<br>
+------------------+<br>
| READ-UNCOMMITTED |<br>
+------------------+<br>
1 row in set (0.00 sec)<br>
<br>
mysql> use test;<br>
Database changed<br>
mysql> begin;<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
mysql> select * from user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 1 | a |<br>
| 2 | b |<br>
+----+------+<br>
2 rows in set (0.00 sec)<br>
<br>
[窗口B]:<br>
mysql> select @@tx_isolation;<br>
+------------------+<br>
| @@tx_isolation |<br>
+------------------+<br>
| READ-UNCOMMITTED |<br>
+------------------+<br>
1 row in set (0.00 sec)<br>
<br>
mysql> begin;<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
mysql> insert into test.user values (3, 'c');<br>
Query OK, 1 row affected (0.00 sec)<br>
<br>
mysql> select * from user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 1 | a |<br>
| 2 | b |<br>
| 3 | c |<br>
+----+------+<br>
3 rows in set (0.00 sec)<br>
<br>
//目前为止,窗口B并未commit;<br>
<br>
[窗口A]:<br>
mysql> select * from user ;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 1 | a |<br>
| 2 | b |<br>
| 3 | c |<br>
+----+------+<br>
3 rows in set (0.00 sec)
Read Committed(读取提交内容)
这是大多数数据库系统的默认隔离级别(但不是MySQL默认的)。
它满足了隔离的简单定义:一个事务只能看见已经提交事务所做的改变。
这种隔离级别 也支持所谓的不可重复读(NonrepeatableRead),因为同一事务的其他实例在该实例处理其间可能会有新的commit,所以同一 select 可能返回不同结果。[窗口A]:<br>
<br>
mysql> SET GLOBAL tx_isolation='READ-COMMITTED';<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
mysql> quit;<br>
Bye<br>
<br>
[root@vagrant-centos65 ~]# mysql -uroot -pxxxx(重新登录)<br>
<br>
mysql> SELECT @@tx_isolation;<br>
+----------------+<br>
| @@tx_isolation |<br>
+----------------+<br>
| READ-COMMITTED |<br>
+----------------+<br>
1 row in set (0.00 sec)<br>
<br>
mysql> begin;<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
mysql> select * from test.user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 1 | a |<br>
| 2 | b |<br>
+----+------+<br>
2 rows in set (0.00 sec)<br>
<br>
<br>
[窗口B]:<br>
<br>
mysql> SELECT @@tx_isolation;<br>
+----------------+<br>
| @@tx_isolation |<br>
+----------------+<br>
| READ-COMMITTED |<br>
+----------------+<br>
1 row in set (0.00 sec)<br>
<br>
mysql> begin;<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
mysql> select * from test.user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 1 | a |<br>
| 2 | b |<br>
+----+------+<br>
2 rows in set (0.00 sec)<br>
<br>
mysql> delete from test.user where id=1;<br>
Query OK, 1 row affected (0.00 sec)<br>
<br>
mysql> select * from test.user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 2 | b |<br>
+----+------+<br>
1 row in set (0.00 sec)<br>
<br>
[窗口A]:<br>
<br>
mysql> select * from test.user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 1 | a |<br>
| 2 | b |<br>
+----+------+<br>
2 rows in set (0.00 sec)<br>
<br>
[窗口B]:<br>
<br>
mysql> commit;<br>
Query OK, 0 rows affected (0.02 sec)<br>
<br>
[窗口A]:<br>
<br>
mysql> select * from test.user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 2 | b |<br>
+----+------+<br>
1 row in set (0.00 sec)
Repeatable Read(可重读)
这是MySQL的默认事务隔离级别,它确保同一事务的多个实例在并发读取数据时,会看到同样的数据行。
不过理论上,这会导致另一个棘手的问题:幻读 (Phantom Read)。
简单的说,幻读指当用户读取某一范围的数据行时,另一个事务又在该范围内插入了新行,当用户再读取该范围的数据行时,会发现有新的“幻影” 行。
InnoDB和Falcon存储引擎通过多版本并发控制(MVCC,Multiversion Concurrency Control)机制解决了该问题。[窗口A]:<br>
<br>
mysql> SET GLOBAL tx_isolation='REPEATABLE-READ';<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
mysql> quit;<br>
Bye<br>
<br>
[root@vagrant-centos65 ~]# mysql -uroot -pxxxx(重新登录)<br>
<br>
mysql> SELECT @@tx_isolation;<br>
+-----------------+<br>
| @@tx_isolation |<br>
+-----------------+<br>
| REPEATABLE-READ |<br>
+-----------------+<br>
1 row in set (0.00 sec)<br>
<br>
mysql> begin;<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
[窗口B]:<br>
<br>
mysql> quit;<br>
Bye<br>
<br>
[root@vagrant-centos65 ~]# mysql -uroot -pxxxx(重新登录)<br>
<br>
mysql> SELECT @@tx_isolation;<br>
+-----------------+<br>
| @@tx_isolation |<br>
+-----------------+<br>
| REPEATABLE-READ |<br>
+-----------------+<br>
1 row in set (0.00 sec)<br>
<br>
mysql> insert into test.user values (4, 'd');<br>
Query OK, 1 row affected (0.00 sec)<br>
<br>
mysql> select * from test.user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 2 | b |<br>
| 4 | d |<br>
+----+------+<br>
2 rows in set (0.00 sec)<br>
<br>
[窗口A]:<br>
<br>
mysql> select * from test.user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 2 | b |<br>
+----+------+<br>
1 rows in set (0.00 sec)<br>
<br>
mysql> commit;<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
mysql> select * from test.user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 2 | b |<br>
| 4 | d |<br>
+----+------+<br>
2 rows in set (0.00 sec)
Serializable(序列化执行)
这是最高的隔离级别,它通过强制事务排序,使之不可能相互冲突,从而解决幻读问题。
简言之,它是在每个读的数据行上加上共享锁。在这个级别,可能导致大量的超时现象和锁竞争。 [窗口A]:<br>
<br>
mysql> SET GLOBAL tx_isolation='SERIALIZABLE';<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
mysql> quit;<br>
Bye<br>
<br>
[root@vagrant-centos65 ~]# mysql -uroot -pxxxx(重新登录)<br>
<br>
mysql> SELECT @@tx_isolation;<br>
+----------------+<br>
| @@tx_isolation |<br>
+----------------+<br>
| SERIALIZABLE |<br>
+----------------+<br>
1 row in set (0.00 sec)<br>
<br>
mysql> select * from test.user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 2 | b |<br>
| 4 | d |<br>
+----+------+<br>
2 rows in set (0.00 sec)<br>
<br>
mysql> begin;<br>
Query OK, 0 rows affected (0.00 sec)<br>
<br>
mysql> insert into test.user values (5, 'e');<br>
Query OK, 1 row affected (0.00 sec)<br>
<br>
[窗口B]:<br>
<br>
mysql> quit;<br>
Bye<br>
<br>
[root@vagrant-centos65 ~]# mysql -uroot -pxxxx(重新登录)<br>
<br>
mysql> SELECT @@tx_isolation;<br>
+----------------+<br>
| @@tx_isolation |<br>
+----------------+<br>
| SERIALIZABLE |<br>
+----------------+<br>
1 row in set (0.00 sec)<br>
<br>
mysql> select * from test.user;<br>
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction<br>
<br>
[窗口A]:<br>
<br>
mysql> commit;<br>
Query OK, 0 rows affected (0.01 sec)<br>
<br>
[窗口B]:<br>
<br>
mysql> mysql> select * from test.user;<br>
+----+------+<br>
| id | name |<br>
+----+------+<br>
| 2 | b |<br>
| 4 | d |<br>
| 5 | e |<br>
+----+------+<br>
3 rows in set (0.00 sec)
Thank ~
来源:http://mp.weixin.qq.com/s?__biz=MjM5NDM4MDIwNw==&mid=2448834642&idx=1&sn=c02c5cc8ab0c1f29142ac8f8aa6b78af#rd
更多【干货分享】,请关注我的个人订阅号。