本篇文章主要介绍了"事务隔离级别 mysql隔离级别",主要涉及到事务隔离级别方面的内容,对于MySql感兴趣的同学可以参考一下:
原文链接
MySQL InnoDB事务的隔离级别有四级,默认是“可重复读”(REPEATABLE READ)。未提交读(READ UNCOMMITTED)。另...
13.2.8.5. Avoiding the Phantom Problem Using Next-Key Locking (http://dev.mysql.com/doc/refman/5.0/en/innodb-next-key-locking.html)To prevent phantoms, InnoDB uses an algorithm called next-key locking that combines index-row lockingwith gap locking.
You can use next-key lockingto implement a uniqueness check in your application: If you read your data in share mode anddonot see a duplicate for a row you are going to insert, then you can safely insert your row and know that the next-key lock seton the successor of your row during the read prevents anyone meanwhile inserting a duplicate for your row. Thus, the next-key locking enables you to “lock” the nonexistence of something in your table.
我的理解是说,InnoDB提供了next-key locks,但需要应用程序自己去加锁。manual里提供一个例子:
SELECT * FROM child WHERE id > 100FORUPDATE;
这样,InnoDB会给id大于100的行(假如child表里有一行id为102),以及100-102,102+的gap都加上锁。
可以使用show innodb status来查看是否给表加上了锁。
再看一个实验,要注意,表actor里的actor_id为主键字段。实验三:
t Session A Session B
|
| STARTTRANSACTION;STARTTRANSACTION;
|
| select * from actor
| where actor_id <=3
| FORUPDATE;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+---------------------+
| 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
| 3 | t3 | t3 | 2016-06-16 15:18:33 |
+----------+------------+-----------+---------------------+
| insertinto actor
| values (2,'t2','t2',now());
| ERROR 1205 (HY000): Lock wait timeout exceeded;
| try restarting transaction
|
| select * from actor;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+---------------------+
| 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
| 3 | t3 | t3 | 2016-06-16 15:18:33 |
| 4 | t0 | t0 | 2016-06-16 15:18:36 |
+----------+------------+-----------+---------------------+
| insertinto actor
| values (5,'t5','t5',now());
| Query OK, 1 row affected (0.00 sec)
|
| select * from actor;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+---------------------+
| 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
| 3 | t3 | t3 | 2016-06-16 15:18:33 |
| 4 | t0 | t0 | 2016-06-16 15:18:36 |
+----------+------------+-----------+---------------------+
| COMMIT;
|
| select * from actor;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+---------------------+
| 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
| 3 | t3 | t3 | 2016-06-16 15:18:33 |
| 4 | t0 | t0 | 2016-06-16 15:18:36 |
+----------+------------+-----------+---------------------+
v
可以看到,用actor_id <=3加的锁,只锁住了actor_id <=3的范围,可以成功添加actor_id 为5的记录,添加actor_id 为2的记录时就会等待锁的释放。。
MySQL manual里对可重复读里的锁的详细解释:
For locking reads (SELECTwithFORUPDATEorLOCKIN SHARE MODE),UPDATE, andDELETE statements, locking depends on whether the statement uses a unique index with a unique search condition, or a range-type search condition. For a unique index with a unique search condition, InnoDB locks only the index record found, not the gap before it. For other search conditions, InnoDB locks the index range scanned, using gap locks ornext-key (gap plus index-record) locks to block insertions by other sessions into the gaps covered by the range.
一致性读和提交读,先看实验,实验四: