本篇文章主要介绍了"事务隔离级别 mysql隔离级别",主要涉及到事务隔离级别方面的内容,对于MySql感兴趣的同学可以参考一下:
原文链接
MySQL InnoDB事务的隔离级别有四级,默认是“可重复读”(REPEATABLE READ)。未提交读(READ UNCOMMITTED)。另...
t Session A Session B
|
| STARTTRANSACTION;STARTTRANSACTION;
|
| select actor_id from actor;
| +----------+
| | actor_id |
| +----------+
| | 1 |
| +----------+
| insertinto actor
| select * from sakila.actor where actor_id=2;
|
| select actor_id from actor;
| +----------+
| | actor_id |
| +----------+
| | 1 |
| +----------+
| COMMIT;
|
| select actor_id from actor;
| +----------+
| | actor_id |
| +----------+
| | 1 |
| +----------+
|
| update actor set last_update=now();
| Rows matched: 2 Changed: 2 Warnings: 0
| (怎么多出来一行)
|
| select actor_id from actor;
| +----------+
| | actor_id |
| +----------+
| | 1 |
| | 2 |
| +----------+
|
v
本事务中第一次读取出一行,做了一次更新后,另一个事务里提交的数据就出现了。也可以看做是一种幻读。
那么,InnoDB指出的可以避免幻读是怎么回事呢?
Bydefault, InnoDB operates in REPEATABLE READ transaction isolation level andwith the innodb_locks_unsafe_for_binlog system variable disabled. In this case, InnoDB uses next-key locks for searches and index scans, which prevents phantom rows (see Section 13.6.8.5, “Avoiding the Phantom Problem UsingNext-Key Locking”).
准备的理解是,当隔离级别是可重复读,且禁用innodb_locks_unsafe_for_binlog的情况下,在搜索和扫描index的时候使用的next-key locks可以避免幻读。
关键点在于,是InnoDB默认对一个普通的查询也会加next-key locks,还是说需要应用自己来加锁呢?如果单看这一句,可能会以为InnoDB对普通的查询也加了锁,如果是,那和序列化(SERIALIZABLE)的区别又在哪里呢?
MySQL manual里还有一段: