mysql?樂(lè)觀鎖和悲觀鎖的具體使用
悲觀鎖介紹(百科):
悲觀鎖,正如其名,它指的是對(duì)數(shù)據(jù)被外界(包括本系統(tǒng)當(dāng)前的其他事務(wù),以及來(lái)自外部系統(tǒng)的事務(wù)處理)修改持保守態(tài)度,因此,在整個(gè)數(shù)據(jù)處理過(guò)程中, 將數(shù)據(jù)處于鎖定狀態(tài)。悲觀鎖的實(shí)現(xiàn),往往依靠數(shù)據(jù)庫(kù)提供的鎖機(jī)制(也只有數(shù)據(jù)庫(kù)層提供的鎖機(jī)制才能真正保證數(shù)據(jù)訪問(wèn)的排他性,否則,即使在本系統(tǒng)中實(shí)現(xiàn)了 加鎖機(jī)制,也無(wú)法保證外部系統(tǒng)不會(huì)修改數(shù)據(jù))。
使用場(chǎng)景舉例:以MySQL InnoDB為例
商品goods表中有一個(gè)字段status,status為1代表商品未被下單,status為2代表商品已經(jīng)被下單,那么我們對(duì)某個(gè)商品下單時(shí)必須確保該商品status為1。假設(shè)商品的id為1。
1如果不采用鎖,那么操作方法如下:
//1.查詢出商品信息 select status from t_goods where id=1; //2.根據(jù)商品信息生成訂單 insert into t_orders (id,goods_id) values (null,1); //3.修改商品status為2 update t_goods set status=2;
上面這種場(chǎng)景在高并發(fā)訪問(wèn)的情況下很可能會(huì)出現(xiàn)問(wèn)題。
前面已經(jīng)提到,只有當(dāng)goods status為1時(shí)才能對(duì)該商品下單,上面第一步操作中,查詢出來(lái)的商品status為1。但是當(dāng)我們執(zhí)行第三步Update操作的時(shí)候,有可能出現(xiàn)其他 人先一步對(duì)商品下單把goods status修改為2了,但是我們并不知道數(shù)據(jù)已經(jīng)被修改了,這樣就可能造成同一個(gè)商品被下單2次,使得數(shù)據(jù)不一致。所以說(shuō)這種方式是不安全的。
2使用悲觀鎖來(lái)實(shí)現(xiàn):
在上面的場(chǎng)景中,商品信息從查詢出來(lái)到修改,中間有一個(gè)處理訂單的過(guò)程,使用悲觀鎖的原理就是,當(dāng)我們?cè)诓樵兂鰃oods信息后就把當(dāng)前的數(shù)據(jù)鎖定,直到我們修改完畢后再解鎖。那么在這個(gè)過(guò)程中,因?yàn)間oods被鎖定了,就不會(huì)出現(xiàn)有第三者來(lái)對(duì)其進(jìn)行修改了。
注:要使用悲觀鎖,我們必須關(guān)閉mysql數(shù)據(jù)庫(kù)的自動(dòng)提交屬性,因?yàn)镸ySQL默認(rèn)使用autocommit模式,也就是說(shuō),當(dāng)你執(zhí)行一個(gè)更新操作后,MySQL會(huì)立刻將結(jié)果進(jìn)行提交。
我們可以使用命令設(shè)置MySQL為非autocommit模式:
set autocommit=0;
設(shè)置完autocommit后,我們就可以執(zhí)行我們的正常業(yè)務(wù)了。具體如下:
//0.開(kāi)始事務(wù) begin;/begin work;/start transaction; (三者選一就可以) //1.查詢出商品信息 select status from t_goods where id=1 for update; //2.根據(jù)商品信息生成訂單 insert into t_orders (id,goods_id) values (null,1); //3.修改商品status為2 update t_goods set status=2; //4.提交事務(wù) commit;/commit work;
注:上面的begin/commit為事務(wù)的開(kāi)始和結(jié)束,因?yàn)樵谇耙徊轿覀冴P(guān)閉了mysql的autocommit,所以需要手動(dòng)控制事務(wù)的提交,在這里就不細(xì)表了。
上面的第一步我們執(zhí)行了一次查詢操作:select status from t_goods where id=1 for update;
與普通查詢不一樣的是,我們使用了select…for update的方式,這樣就通過(guò)數(shù)據(jù)庫(kù)實(shí)現(xiàn)了悲觀鎖。此時(shí)在t_goods表中,id為1的 那條數(shù)據(jù)就被我們鎖定了,其它的事務(wù)必須等本次事務(wù)提交之后才能執(zhí)行。這樣我們可以保證當(dāng)前的數(shù)據(jù)不會(huì)被其它事務(wù)修改。
注:需要注意的是,在事務(wù)中,只有SELECT ... FOR UPDATE 或LOCK IN SHARE MODE 同一筆數(shù)據(jù)時(shí)會(huì)等待其它事務(wù)結(jié)束后才執(zhí)行,一般SELECT ... 則不受此影響。拿 上面的實(shí)例來(lái)說(shuō),當(dāng)我執(zhí)行select status from t_goods where id=1 for update;后。我在另外的事務(wù)中如果再次執(zhí)行select status from t_goods where id=1 for update;則第二個(gè)事務(wù)會(huì)一直等待第一個(gè)事務(wù)的提交,此時(shí)第二個(gè)查詢處于阻塞的狀態(tài),但是如果我是在第二個(gè)事務(wù)中執(zhí)行select status from t_goods where id=1;則能正常查詢出數(shù)據(jù),不會(huì)受第一個(gè)事務(wù)的影響。
補(bǔ)充:MySQL select…for update的Row Lock與Table Lock
上面我們提到,使用select…for update會(huì)把數(shù)據(jù)給鎖住,不過(guò)我們需要注意一些鎖的級(jí)別,MySQL InnoDB默認(rèn)Row-Level Lock,所以只有「明確」地指定主鍵,MySQL 才會(huì)執(zhí)行Row lock (只鎖住被選取的數(shù)據(jù)) ,否則MySQL 將會(huì)執(zhí)行Table Lock (將整個(gè)數(shù)據(jù)表單給鎖住)。
舉例說(shuō)明:
數(shù)據(jù)庫(kù)表t_goods,包括id,status,name三個(gè)字段,id為主鍵,數(shù)據(jù)庫(kù)中記錄如下;
mysql> select * from t_goods; +----+--------+------+ | id | status | name | +----+--------+------+ | 1 | 1 | 道具 | | 2 | 1 | 裝備 | +----+--------+------+ 2 rows in set mysql>
注:為了測(cè)試數(shù)據(jù)庫(kù)鎖,我使用兩個(gè)console來(lái)模擬不同的事務(wù)操作,分別用console1、console2來(lái)表示。
例1: (明確指定主鍵,并且有此數(shù)據(jù),row lock)
console1:查詢出結(jié)果,但是把該條數(shù)據(jù)鎖定了
mysql> select * from t_goods where id=1 for update; +----+--------+------+ | id | status | name | +----+--------+------+ | 1 | 1 | 道具 | +----+--------+------+ 1 row in set mysql>
console2:查詢被阻塞
mysql> select * from t_goods where id=1 for update;
console2:如果console1長(zhǎng)時(shí)間未提交,則會(huì)報(bào)錯(cuò)
mysql> select * from t_goods where id=1 for update;
ERROR 1205 : Lock wait timeout exceeded; try restarting transaction
例2: (明確指定主鍵,若查無(wú)此數(shù)據(jù),無(wú)lock)
console1:查詢結(jié)果為空
mysql> select * from t_goods where id=3 for update; Empty set
console2:查詢結(jié)果為空,查詢無(wú)阻塞,說(shuō)明console1沒(méi)有對(duì)數(shù)據(jù)執(zhí)行鎖定
mysql> select * from t_goods where id=3 for update; Empty set
例3: (無(wú)主鍵,table lock)
console1:查詢name=道具 的數(shù)據(jù),查詢正常
mysql> select * from t_goods where name='道具' for update; +----+--------+------+ | id | status | name | +----+--------+------+ | 1 | 1 | 道具 | +----+--------+------+ 1 row in set mysql>
console2:查詢name=裝備 的數(shù)據(jù),查詢阻塞,說(shuō)明console1把表給鎖住了
mysql> select * from t_goods where name='裝備' for update;
console2:若console1長(zhǎng)時(shí)間未提交,則查詢返回為空
mysql> select * from t_goods where name='裝備' for update; Query OK, -1 rows affected
例4: (主鍵不明確,table lock)
console1:查詢正常
mysql> begin; Query OK, 0 rows affected mysql> select * from t_goods where id>0 for update; +----+--------+------+ | id | status | name | +----+--------+------+ | 1 | 1 | 道具 | | 2 | 1 | 裝備 | +----+--------+------+ 2 rows in set mysql>
console2:查詢被阻塞,說(shuō)明console1把表給鎖住了
mysql> select * from t_goods where id>1 for update;
例5: (主鍵不明確,table lock)
console1:
mysql> begin; Query OK, 0 rows affected mysql> select * from t_goods where id<>1 for update; +----+--------+------+ | id | status | name | +----+--------+------+ | 2 | 1 | 裝備 | +----+--------+------+ 1 row in set mysql>
console2:查詢被阻塞,說(shuō)明console1把表給鎖住了
mysql> select * from t_goods where id<>2 for update;
console1:提交事務(wù)
mysql> commit; Query OK, 0 rows affected
console2:console1事務(wù)提交后,console2查詢結(jié)果正常
mysql> select * from t_goods where id<>2 for update; +----+--------+------+ | id | status | name | +----+--------+------+ | 1 | 1 | 道具 | +----+--------+------+ 1 row in set mysql>
以上就是關(guān)于數(shù)據(jù)庫(kù)主鍵對(duì)MySQL鎖級(jí)別的影響實(shí)例,需要注意的是,除了主鍵外,使用索引也會(huì)影響數(shù)據(jù)庫(kù)的鎖定級(jí)別
舉例:
我們修改t_goods表,給status字段創(chuàng)建一個(gè)索引
修改id為2的數(shù)據(jù)的status為2,此時(shí)表中數(shù)據(jù)為:
mysql> select * from t_goods; +----+--------+------+ | id | status | name | +----+--------+------+ | 1 | 1 | 道具 | | 2 | 2 | 裝備 | +----+--------+------+ 2 rows in set mysql>
例6: (明確指定索引,并且有此數(shù)據(jù),row lock)
console1:
mysql> select * from t_goods where status=1 for update; +----+--------+------+ | id | status | name | +----+--------+------+ | 1 | 1 | 道具 | +----+--------+------+ 1 row in set mysql>
console2:查詢status=1的數(shù)據(jù)時(shí)阻塞,超時(shí)后返回為空,說(shuō)明數(shù)據(jù)被console1鎖定了
mysql> select * from t_goods where status=1 for update; Query OK, -1 rows affected
console2:查詢status=2的數(shù)據(jù),能正常查詢,說(shuō)明console1只鎖住了行,未鎖表
mysql> select * from t_goods where status=2 for update; +----+--------+------+ | id | status | name | +----+--------+------+ | 2 | 2 | 裝備 | +----+--------+------+ 1 row in set mysql>
例7: (明確指定索引,若查無(wú)此數(shù)據(jù),無(wú)lock)
console1:查詢status=3的數(shù)據(jù),返回空數(shù)據(jù)
mysql> select * from t_goods where status=3 for update; Empty set
console2:查詢status=3的數(shù)據(jù),返回空數(shù)據(jù)
mysql> select * from t_goods where status=3 for update; Empty set
樂(lè)觀鎖介紹:
樂(lè)觀鎖( Optimistic Locking ) 相對(duì)悲觀鎖而言,樂(lè)觀鎖假設(shè)認(rèn)為數(shù)據(jù)一般情況下不會(huì)造成沖突,所以在數(shù)據(jù)進(jìn)行提交更新的時(shí)候,才會(huì)正式對(duì)數(shù)據(jù)的沖突與否進(jìn)行檢測(cè),如果發(fā)現(xiàn)沖突了,則讓返回用戶錯(cuò)誤的信息,讓用戶決定如何去做。那么我們?nèi)绾螌?shí)現(xiàn)樂(lè)觀鎖呢,一般來(lái)說(shuō)有以下2種方式:
1.使用數(shù)據(jù)版本(Version)記錄機(jī)制實(shí)現(xiàn),這是樂(lè)觀鎖最常用的一種實(shí)現(xiàn) 方式。何謂數(shù)據(jù)版本?即為數(shù)據(jù)增加一個(gè)版本標(biāo)識(shí),一般是通過(guò)為數(shù)據(jù)庫(kù)表增加一個(gè)數(shù)字類型的 “version” 字段來(lái)實(shí)現(xiàn)。當(dāng)讀取數(shù)據(jù)時(shí),將version字段的值一同讀出,數(shù)據(jù)每更新一次,對(duì)此version值加一。當(dāng)我們提交更新的時(shí)候,判斷數(shù)據(jù)庫(kù)表對(duì)應(yīng)記錄 的當(dāng)前版本信息與第一次取出來(lái)的version值進(jìn)行比對(duì),如果數(shù)據(jù)庫(kù)表當(dāng)前版本號(hào)與第一次取出來(lái)的version值相等,則予以更新,否則認(rèn)為是過(guò)期數(shù) 據(jù)。用下面的一張圖來(lái)說(shuō)明:
如上圖所示,如果更新操作順序執(zhí)行,則數(shù)據(jù)的版本(version)依次遞增,不會(huì)產(chǎn)生沖突。但是如果發(fā)生有不同的業(yè)務(wù)操作對(duì)同一版本的數(shù)據(jù)進(jìn)行修 改,那么,先提交的操作(圖中B)會(huì)把數(shù)據(jù)version更新為2,當(dāng)A在B之后提交更新時(shí)發(fā)現(xiàn)數(shù)據(jù)的version已經(jīng)被修改了,那么A的更新操作會(huì)失 敗。
2.樂(lè)觀鎖定的第二種實(shí)現(xiàn)方式和第一種差不多,同樣是在需要樂(lè)觀鎖控制的table中增加一個(gè)字段,名稱無(wú)所謂,字段類型使用時(shí)間戳 (timestamp), 和上面的version類似,也是在更新提交的時(shí)候檢查當(dāng)前數(shù)據(jù)庫(kù)中數(shù)據(jù)的時(shí)間戳和自己更新前取到的時(shí)間戳進(jìn)行對(duì)比,如果一致則OK,否則就是版本沖突。
使用舉例:以MySQL InnoDB為例
還是拿之前的實(shí)例來(lái)舉:商品goods表中有一個(gè)字段status,status為1代表商品未被下單,status為2代表商品已經(jīng)被下單,那么我們對(duì)某個(gè)商品下單時(shí)必須確保該商品status為1。假設(shè)商品的id為1。
下單操作包括3步驟:
1.查詢出商品信息
select (status,status,version) from t_goods where id=#{id}
2.根據(jù)商品信息生成訂單
3.修改商品status為2
update t_goods set status=2,version=version+1 where id=#{id} and version=#{version};
那么為了使用樂(lè)觀鎖,我們首先修改t_goods表,增加一個(gè)version字段,數(shù)據(jù)默認(rèn)version值為1。
t_goods表初始數(shù)據(jù)如下:
mysql> select * from t_goods; +----+--------+------+---------+ | id | status | name | version | +----+--------+------+---------+ | 1 | 1 | 道具 | 1 | | 2 | 2 | 裝備 | 2 | +----+--------+------+---------+ 2 rows in set mysql>
對(duì)于樂(lè)觀鎖的實(shí)現(xiàn),我使用MyBatis來(lái)進(jìn)行實(shí)踐,具體如下:
Goods實(shí)體類:
/** * ClassName: Goods <br/> * Function: 商品實(shí)體. <br/> * date: 2013-5-8 上午09:16:19 <br/> * @author chenzhou1025@126.com */ public class Goods implements Serializable { /** * serialVersionUID:序列化ID. */ private static final long serialVersionUID = 6803791908148880587L; /** * id:主鍵id. */ private int id; /** * status:商品狀態(tài):1未下單、2已下單. */ private int status; /** * name:商品名稱. */ private String name; /** * version:商品數(shù)據(jù)版本號(hào). */ private int version; @Override public String toString(){ return "good id:"+id+",goods status:"+status+",goods name:"+name+",goods version:"+version; } //setter and getter }
GoodsDao
mapper.xml
<update id="updateGoodsUseCAS" parameterType="Goods"> <![CDATA[ update t_goods set status=#{status},name=#{name},version=version+1 where id=#{id} and version=#{version} ]]> </update>
GoodsDaoTest測(cè)試類
@Test public void goodsDaoTest(){ int goodsId = 1; //根據(jù)相同的id查詢出商品信息,賦給2個(gè)對(duì)象 Goods goods1 = this.goodsDao.getGoodsById(goodsId); Goods goods2 = this.goodsDao.getGoodsById(goodsId); //打印當(dāng)前商品信息 System.out.println(goods1); System.out.println(goods2); //更新商品信息1 goods1.setStatus(2);//修改status為2 int updateResult1 = this.goodsDao.updateGoodsUseCAS(goods1); System.out.println("修改商品信息1"+(updateResult1==1?"成功":"失敗")); //更新商品信息2 goods1.setStatus(2);//修改status為2 int updateResult2 = this.goodsDao.updateGoodsUseCAS(goods1); System.out.println("修改商品信息2"+(updateResult2==1?"成功":"失敗")); }
輸出結(jié)果:
good id:1,goods status:1,goods name:道具,goods version:1
good id:1,goods status:1,goods name:道具,goods version:1
修改商品信息1成功
修改商品信息2失敗
說(shuō)明:
在GoodsDaoTest測(cè)試方法中,我們同時(shí)查出同一個(gè)版本的數(shù)據(jù),賦給不同的goods對(duì)象,然后先修改good1對(duì)象然后執(zhí)行更新操作,執(zhí)行成功。然后我們修改goods2,執(zhí)行更新操作時(shí)提示操作失敗。此時(shí)t_goods表中數(shù)據(jù)如下:
mysql> select * from t_goods; +----+--------+------+---------+ | id | status | name | version | +----+--------+------+---------+ | 1 | 2 | 道具 | 2 | | 2 | 2 | 裝備 | 2 | +----+--------+------+---------+ 2 rows in set mysql>
我們可以看到 id為1的數(shù)據(jù)version已經(jīng)在第一次更新時(shí)修改為2了。所以我們更新good2時(shí)update where條件已經(jīng)不匹配了,所以更新不會(huì)成功,具體sql如下:
update t_goods set status=2,version=version+1 where id=#{id} and version=#{version};
這樣我們就實(shí)現(xiàn)了樂(lè)觀鎖
到此這篇關(guān)于mysql 樂(lè)觀鎖和悲觀鎖的具體使用的文章就介紹到這了,更多相關(guān)mysql 樂(lè)觀鎖和悲觀鎖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL實(shí)現(xiàn)批量推送數(shù)據(jù)到Mongo
這篇文章主要為大家詳細(xì)介紹了MySQL如何實(shí)現(xiàn)批量推送數(shù)據(jù)到Mongo,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下2023-05-05mysql中g(shù)roup by與having合用注意事項(xiàng)分享
在mysql中g(shù)roup by分組查詢我們經(jīng)常會(huì)用到,并且還同時(shí)會(huì)與having合用,下面我介紹group by用法與having合用注意事項(xiàng),希望此教程對(duì)各位朋友有所幫助2013-10-10MySQL添加索引特點(diǎn)及優(yōu)化問(wèn)題
這篇文章主要介紹了MySQL添加索引特點(diǎn)及優(yōu)化問(wèn)題,MySQL索引的建立對(duì)于MySQL的高效運(yùn)行是很重要的,索引可以大大提高M(jìn)ySQL的檢索速度,感興趣的小伙伴可以參考一下2022-07-07IDEA連接mysql時(shí)區(qū)問(wèn)題解決
在使用MySQL數(shù)據(jù)庫(kù)時(shí),經(jīng)常會(huì)遇到需要設(shè)置時(shí)區(qū)的情況,本文主要介紹了IDEA連接mysql時(shí)區(qū)問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06MySQL字符串索引更合理的創(chuàng)建規(guī)則討論
這篇文章主要給大家介紹了關(guān)于MySQL字符串索引更合理的創(chuàng)建規(guī)則,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11MySQL觸發(fā)器學(xué)習(xí)總結(jié)
創(chuàng)建觸發(fā)器,當(dāng)往order表中添加記錄是,更新goods表,大家可以看下語(yǔ)句即可2012-09-09