mysql悲觀鎖總結和實踐

轉載地址:http://chenzhou123520.iteye.com/blog/1860954

悲觀鎖介紹(百科):

悲觀鎖,正如其名,它指的是對數據被外界(包括本系統當前的其他事務,以及來自外部系統的事務處理)修改持保守態度,因此,在整個數據處理過程中,將數據處於鎖定狀態。悲觀鎖的實現,往往依靠數據庫提供的鎖機制(也只有數據庫層提供的鎖機制才能真正保證數據訪問的排他性,否則,即使在本系統中實現了加鎖機制,也無法保證外部系統不會修改數據)。

 

使用場景舉例:以MySQL InnoDB爲例

商品goods表中有一個字段status,status爲1代表商品未被下單,status爲2代表商品已經被下單,那麼我們對某個商品下單時必須確保該商品status爲1。假設商品的id爲1。

 

1如果不採用鎖,那麼操作方法如下:

//1.查詢出商品信息

select status from t_goods where id=1;

//2.根據商品信息生成訂單

insert into t_orders (id,goods_id) values (null,1);

//3.修改商品status爲2

update t_goods set status=2;

 

上面這種場景在高併發訪問的情況下很可能會出現問題。

前面已經提到,只有當goods status爲1時才能對該商品下單,上面第一步操作中,查詢出來的商品status爲1。但是當我們執行第三步Update操作的時候,有可能出現其他人先一步對商品下單把goods status修改爲2了,但是我們並不知道數據已經被修改了,這樣就可能造成同一個商品被下單2次,使得數據不一致。所以說這種方式是不安全的。

 

2使用悲觀鎖來實現:

在上面的場景中,商品信息從查詢出來到修改,中間有一個處理訂單的過程,使用悲觀鎖的原理就是,當我們在查詢出goods信息後就把當前的數據鎖定,直到我們修改完畢後再解鎖。那麼在這個過程中,因爲goods被鎖定了,就不會出現有第三者來對其進行修改了。

 

注:要使用悲觀鎖,我們必須關閉mysql數據庫的自動提交屬性,因爲MySQL默認使用autocommit模式,也就是說,當你執行一個更新操作後,MySQL會立刻將結果進行提交。

 

我們可以使用命令設置MySQL爲非autocommit模式:

set autocommit=0;

 

設置完autocommit後,我們就可以執行我們的正常業務了。具體如下:

//0.開始事務

begin;/begin work;/start transaction; (三者選一就可以)

//1.查詢出商品信息

select status from t_goods where id=1 for update;

//2.根據商品信息生成訂單

insert into t_orders (id,goods_id) values (null,1);

//3.修改商品status爲2

update t_goods set status=2;

//4.提交事務

commit;/commit work;

 

注:上面的begin/commit爲事務的開始和結束,因爲在前一步我們關閉了mysql的autocommit,所以需要手動控制事務的提交,在這裏就不細表了。

 

上面的第一步我們執行了一次查詢操作:select status from t_goods where id=1 for update;

與普通查詢不一樣的是,我們使用了select…for update的方式,這樣就通過數據庫實現了悲觀鎖。此時在t_goods表中,id爲1的 那條數據就被我們鎖定了,其它的事務必須等本次事務提交之後才能執行。這樣我們可以保證當前的數據不會被其它事務修改。

 

注:需要注意的是,在事務中,只有SELECT ... FOR UPDATE 或LOCK IN SHARE MODE 同一筆數據時會等待其它事務結束後才執行,一般SELECT ... 則不受此影響。拿上面的實例來說,當我執行select status from t_goods where id=1 for update;後。我在另外的事務中如果再次執行select status from t_goods where id=1 for update;則第二個事務會一直等待第一個事務的提交,此時第二個查詢處於阻塞的狀態,但是如果我是在第二個事務中執行select status from t_goods where id=1;則能正常查詢出數據,不會受第一個事務的影響。

 

補充:MySQL select…for update的Row Lock與Table Lock

上面我們提到,使用select…for update會把數據給鎖住,不過我們需要注意一些鎖的級別,MySQL InnoDB默認Row-Level Lock,所以只有「明確」地指定主鍵,MySQL 纔會執行Row lock (只鎖住被選取的數據) ,否則MySQL 將會執行Table Lock (將整個數據表單給鎖住)。

 

舉例說明:

數據庫表t_goods,包括id,status,name三個字段,id爲主鍵,數據庫中記錄如下;

Sql代碼  收藏代碼
  1. mysql> select * from t_goods;  
  2. +----+--------+------+  
  3. | id | status | name |  
  4. +----+--------+------+  
  5. |  1 |      1 | 道具 |  
  6. |  2 |      1 | 裝備 |  
  7. +----+--------+------+  
  8. rows in set  
  9.   
  10. mysql>  

注:爲了測試數據庫鎖,我使用兩個console來模擬不同的事務操作,分別用console1、console2來表示。 

 

例1: (明確指定主鍵,並且有此數據,row lock)

console1:查詢出結果,但是把該條數據鎖定了

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where id=1 for update;  
  2. +----+--------+------+  
  3. | id | status | name |  
  4. +----+--------+------+  
  5. |  1 |      1 | 道具 |  
  6. +----+--------+------+  
  7. 1 row in set  
  8.   
  9. mysql>  

console2:查詢被阻塞

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where id=1 for update;  

console2:如果console1長時間未提交,則會報錯

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where id=1 for update;  
  2. ERROR 1205 : Lock wait timeout exceeded; try restarting transaction  

 

例2: (明確指定主鍵,若查無此數據,無lock)

console1:查詢結果爲空

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where id=3 for update;  
  2. Empty set  

console2:查詢結果爲空,查詢無阻塞,說明console1沒有對數據執行鎖定

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where id=3 for update;  
  2. Empty set  

 

例3: (無主鍵,table lock)

console1:查詢name=道具 的數據,查詢正常

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where name='道具' for update;  
  2. +----+--------+------+  
  3. | id | status | name |  
  4. +----+--------+------+  
  5. |  1 |      1 | 道具 |  
  6. +----+--------+------+  
  7. 1 row in set  
  8.   
  9. mysql>  

console2:查詢name=裝備 的數據,查詢阻塞,說明console1把表給鎖住了

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where name='裝備' for update;  

console2:若console1長時間未提交,則查詢返回爲空

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where name='裝備' for update;  
  2. Query OK, -1 rows affected  

 

例4: (主鍵不明確,table lock)

console1:查詢正常

Sql代碼  收藏代碼
  1. mysql> begin;  
  2. Query OK, 0 rows affected  
  3.   
  4. mysql> select * from t_goods where id>0 for update;  
  5. +----+--------+------+  
  6. | id | status | name |  
  7. +----+--------+------+  
  8. |  1 |      1 | 道具 |  
  9. |  2 |      1 | 裝備 |  
  10. +----+--------+------+  
  11. rows in set  
  12.   
  13. mysql>  

console2:查詢被阻塞,說明console1把表給鎖住了

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where id>1 for update;  

 

例5: (主鍵不明確,table lock)

console1:

Sql代碼  收藏代碼
  1. mysql> begin;  
  2. Query OK, 0 rows affected  
  3.   
  4. mysql> select * from t_goods where id<>1 for update;  
  5. +----+--------+------+  
  6. | id | status | name |  
  7. +----+--------+------+  
  8. |  2 |      1 | 裝備 |  
  9. +----+--------+------+  
  10. 1 row in set  
  11.   
  12. mysql>  

console2:查詢被阻塞,說明console1把表給鎖住了

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where id<>2 for update;  

console1:提交事務

Sql代碼  收藏代碼
  1. mysql> commit;  
  2. Query OK, 0 rows affected  

console2:console1事務提交後,console2查詢結果正常

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where id<>2 for update;  
  2. +----+--------+------+  
  3. | id | status | name |  
  4. +----+--------+------+  
  5. |  1 |      1 | 道具 |  
  6. +----+--------+------+  
  7. 1 row in set  
  8.   
  9. mysql>  

 

以上就是關於數據庫主鍵對MySQL鎖級別的影響實例,需要注意的是,除了主鍵外,使用索引也會影響數據庫的鎖定級別

 

舉例:

我們修改t_goods表,給status字段創建一個索引

修改id爲2的數據的status爲2,此時表中數據爲:

Sql代碼  收藏代碼
  1. mysql> select * from t_goods;  
  2. +----+--------+------+  
  3. | id | status | name |  
  4. +----+--------+------+  
  5. |  1 |      1 | 道具 |  
  6. |  2 |      2 | 裝備 |  
  7. +----+--------+------+  
  8. rows in set  
  9.   
  10. mysql>  

 

例6: (明確指定索引,並且有此數據,row lock)

console1:

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where status=1 for update;  
  2. +----+--------+------+  
  3. | id | status | name |  
  4. +----+--------+------+  
  5. |  1 |      1 | 道具 |  
  6. +----+--------+------+  
  7. 1 row in set  
  8.   
  9. mysql>  

console2:查詢status=1的數據時阻塞,超時後返回爲空,說明數據被console1鎖定了

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where status=1 for update;  
  2. Query OK, -1 rows affected  

console2:查詢status=2的數據,能正常查詢,說明console1只鎖住了行,未鎖表

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where status=2 for update;  
  2. +----+--------+------+  
  3. | id | status | name |  
  4. +----+--------+------+  
  5. |  2 |      2 | 裝備 |  
  6. +----+--------+------+  
  7. 1 row in set  
  8.   
  9. mysql>  

 

例7: (明確指定索引,若查無此數據,無lock)

console1:查詢status=3的數據,返回空數據

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where status=3 for update;  
  2. Empty set  

console2:查詢status=3的數據,返回空數據

Sql代碼  收藏代碼
  1. mysql> select * from t_goods where status=3 for update;  
  2. Empty set  

發佈了25 篇原創文章 · 獲贊 9 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章