關於Mysql的事務和鎖 看這一篇文章就夠了
阿新 • • 發佈:2019-02-13
- 鎖
- 共享讀鎖(S鎖)和 排他寫鎖(X鎖)
- 行鎖與表鎖
Session 1 | Session 2 | |
T1 | start transaction; | start transaction; |
T2 | select * from user;+----+--------+------+| id | name | age |+----+--------+------+| 1 | Jammes | 23 || 2 | Kobe | 34 || 3 | Smith | 45 || 4 | JR | 34 || 5 | Yi | 45 |+----+--------+------+ | |
T3 | update user set name="Polo" where name="Kobe" and age=45; | |
T4 | update user set name = "Smith" where id=5;^C^C -- query abortedERROR 1317 (70100): Query execution was interrupted |
- 死鎖
- 事務的特性ACID
- 原子性(atomicity)
- 一致性(consistency)
- 隔離型(isolation)
- 永續性(durability)
- MVCC, 多版本併發控制, 是指為了提高mysql的併發讀寫能力,事務查詢時只是讀取行記錄某個時間點的快照,所以並不會去對資料行加讀鎖,也不用等待資料行鎖的釋放,自然對併發讀寫效能會有很大提升。
- 隔離級別
- 讀未提交(Read Uncommitted)
Session 1 | Session 2 | |
T1 | start transaction; | start transaction; |
T2 | select * from user where id = 4;+----+-------+------+| id | name | age |+----+-------+------+| 4 | Smith | 34 |+----+-------+------+ | |
T3 | update user set name = 'JR' where id = 4;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0 | |
T4 | select * from user where id = 4;+----+------+------+| id | name | age |+----+------+------+| 4 | JR | 34 |+----+------+------+1 row in set (0.00 sec) | |
T5 | Rollback; | |
T6 | select * from user where id = 4;+----+-------+------+| id | name | age |+----+-------+------+| 4 | Smith | 34 |+----+-------+------+ |
- 讀提交(Read Committed)
Session 1 | Session 2 | |
T1 | start transaction; | start transaction; |
T2 | select * from user where id = 4;+----+------+------+| id | name | age |+----+------+------+| 4 | JR | 34 |+----+------+------+ | |
T3 | update user set name = 'Smith' where id = 4;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0 | |
T4 | select * from user where id = 4;+----+------+------+| id | name | age |+----+------+------+| 4 | JR | 34 |+----+------+------+1 row in set (0.00 sec) | |
T5 | commit; | |
T6 | select * from user where id = 4;+----+-------+------+| id | name | age |+----+-------+------+| 4 | Smith | 34 |+----+-------+------+ |
- 可重複讀(Repeatable Read)
Session 1 | Session 2 | |
T1 | start transaction; | start transaction; |
T2 | select * from user where id = 4;+----+------+------+| id | name | age |+----+------+------+| 4 | JR | 34 |+----+------+------+ | |
T3 | update user set name = 'Smith' where id = 4;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0 | |
T4 | select * from user where id = 4;+----+------+------+| id | name | age |+----+------+------+| 4 | JR | 34 |+----+------+------+1 row in set (0.00 sec) | |
T5 | commit; | |
T6 | select * from user where id = 4;+----+------+------+| id | name | age |+----+------+------+| 4 | JR | 34 |+----+------+------+ |
- 可序列化(Serializable)
Session 1 | Session 2 | |
T1 | start transaction; | start transaction; |
T2 | select * from user where id = 4;+----+------+------+| id | name | age |+----+------+------+| 4 | JR | 34 |+----+------+------+ | |
T3 | update user set name = 'Smith' where id = 4;等待鎖釋放…….. |
Session 1 | Session 2 |
T1 | start transaction; |
T2 | update user set name = "Smith" where id=4; Query OK, 1 row affected (0.00 sec) |
T3 | select * from user; +----+--------+———+ | id | name | age | +----+--------+———+ | 1 | Jammes | 23 | | 2 | Kobe | 34 | | 3 | Wade | 45 | | 4 | JR | 34 | +----+--------+------+ |
- 一致性非鎖定讀
- 關於髒讀和幻讀
- 隔離級別的應用場景