共享鎖【寫鎖】少用
共享鎖的現(xiàn)象:注意薪寓,操作要快,不然會超時
1澜共、分別打開兩個Mysql命令行 A和B
image.png
2向叉、使用相同數(shù)據(jù)庫
3、A和B依次輸入 start transaction嗦董;
4母谎、A和B查詢相同表 select * from 表名 lock in share mode;
5、A 輸入更新語句京革, B 輸入更新語句【總之會有一方卡住】
image.png
卡住的原因是一方拿到了共享鎖奇唤,拿到共享鎖的一方提交事務(wù)【commit】后另一方才可以執(zhí)行幸斥;
排他鎖【寫鎖】常用鎖行,不建議鎖表
1咬扇、分別打開兩個Mysql命令行 A和B
2甲葬、使用相同數(shù)據(jù)庫
3、A和B依次輸入 start transaction懈贺;
4经窖、A輸入select * from t_customer for update ;【鎖表】后,發(fā)現(xiàn)界面輸出梭灿;
5画侣、B輸入select * from t_customer for update ;【鎖表】后,界面無輸出堡妒;
image.png
6配乱、A提交事務(wù)【commit】后,B有輸出
image.png
7皮迟、執(zhí)行鎖行操作A輸入select * from t_customer where id=1 for update ;【鎖表】后宪卿,發(fā)現(xiàn)界面輸出;
8万栅、B執(zhí)行select * from t_customer where id=2 for update ;有輸出
image.png
9佑钾、B執(zhí)行select * from t_customer where id=1 for update ;卡住
image.png
10、A提交事務(wù)【commit】后烦粒,B執(zhí)行select * from t_customer where id=1 for update ;輸出
image.png
Hibernate寫鎖【排他鎖】實現(xiàn)
現(xiàn)象:命令行開啟線程休溶,應(yīng)用程序開啟線程
1、命令行就緒
image.png
2扰她、執(zhí)行應(yīng)用程序
image.png
3兽掰、應(yīng)用程序輸出【卡住】
image.png
3、命令行提交事務(wù)【commit】
image.png
4徒役、查看應(yīng)用程序控制臺
image.png