|
|||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
同一個事務中 select c from a where a=1 for update 與 select c from a where a=1 取出的值不一樣
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
|
一致性的非鎖定讀: 通過Undo段實現,不同的隔離級別讀取的快照數據不同 多版本併發控制(Multi Version Concurrency Control, MVCC)
|
|||||||||||||||||||||||||||||||||||
|
|
事務一 |
事務二 |
結果(更新後 讀最新值) |
|
start transaction; |
|
|
|
|
start transaction; |
|
Q1 |
|
select c from a where a=1; |
0 |
|
|
update a set c=c+1 where a=1; |
|
Q1 |
|
select c from a where a=1; |
1 |
|
|
commit; |
|
P1 |
select c from a where a=1; |
|
1 |
|
|
|
|
|
事務一 |
事務二 |
結果(更新後 讀最新值) |
|
start transaction; |
|
|
|
|
start transaction; |
|
P1 |
select c from a where a=1; |
|
0 |
|
|
update a set c=c+1 where a=1; |
|
Q1 |
|
select c from a where a=1; |
1 |
|
update a set c=c+1 where a=1; |
|
|
|
|
commit; |
|
P2 |
select c from a where a=1; |
|
3 |
|
|
|
|
|
|||||||||||||||||||||
|
|
||||||||||||||||||||
|
寫一致性
|
||||||||||||||||||||
|
事務一 |
事務二 |
next-key-lock |
begin; |
|
|
select c from a where a<3 lock in share mode; |
|
|
|
begin; |
|
|
Insert into a(a) select 1; |
鎖等待 |
Commit; |
|
|
SELECT@@tx_isolation;
SET [SESSION |GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED |REPEATABLE READ | SERIALIZABLE}
SET SESSION TRANSACTION ISOLATION LEVELREPEATABLE READ;