模擬RI鎖定導致阻塞的場景
1.建表並驗證對和表DML操作時的鎖定狀態
a是主表,定義了id欄位為主鍵
b是從表,id欄位是引用主表的id欄位
結論:
BYS@dg2>select distinct sid from v$mystat;
SID
----------
17
BYS@dg2>create table a(id int primary key);
Table created.
BYS@dg2>create table b(id references a(id));
Table created.
BYS@dg2>insert into a values(1);
1 row created.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
17 TM 75054 0 3 0 0 insert由於有從屬關係因此會在2個表上都加3號共享鎖
17 TM 75056 0 3 0 0
17 TX 458758 888 6 0 0
BYS@dg2>select sid,event from v$session_wait where sid in (49,17);
SID EVENT
---------- ----------------------------------------------------------------
17 SQL*Net message to client
49 SQL*Net message from client
BYS@dg2>col object_name for a20
BYS@dg2>select object_name from dba_objects where object_id in(75054,75056);
OBJECT_NAME
--------------------
A
B
BYS@dg2>commit; 鎖會隨著事務的結束而釋放
Commit complete.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
no rows selected
BYS@dg2>select * from a;
ID
----------
1
BYS@dg2>select * from b;
no rows selected
BYS@dg2>update a set id=111 where id=1;
1 row updated.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
17 TM 75054 0 3 0 0 update現在只對主表有鎖定,從表沒有鎖定
17 TX 393219 1031 6 0 0
BYS@dg2>commit;
Commit complete.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
no rows selected
BYS@dg2>select * from a;
ID
----------
111
BYS@dg2>delete a;
1 row deleted.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
17 TM 75054 0 3 0 0 delete也是隻對主表有鎖定,從表沒有鎖定
17 TX 524310 1244 6 0 0
BYS@dg2>commit;
Commit complete.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
no rows selected
#########################################################################
BYS@dg2>insert into b values(2); --直接給從表插入記錄,如果主表沒有的話,會報錯違反引用完整性約束
insert into b values(2)
*
ERROR at line 1:
ORA-02291: integrity constraint (BYS.SYS_C0011302) violated - parent key not found
BYS@dg2>insert into a values(2);
1 row created.
BYS@dg2>commit;
Commit complete.
BYS@dg2>select * from a;
ID
----------
2
BYS@dg2>select * from b;
no rows selected
BYS@dg2>insert into b values(2);
1 row created.
BYS@dg2>select * from b;
ID
----------
2
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
17 TM 75054 0 3 0 0
17 TM 75056 0 3 0 0 從表的insert也會對主從表同時加鎖
17 TX 196634 1043 6 0 0
BYS@dg2>commit;
Commit complete.
BYS@dg2>delete b;
1 row deleted.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
17 TM 75054 0 3 0 0
17 TM 75056 0 3 0 0 從表delete也會對主從表同時加鎖
17 TX 393245 1031 6 0 0
#############################################
BYS@dg2>select distinct sid from v$mystat;
SID
----------
46
BYS@dg2>select * from a;
ID
----------
2
BYS@dg2>select * from b;
no rows selected
BYS@dg2>insert into a values(33);
1 row created.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
46 TM 75054 0 3 0 0 insert由於有從屬關係因此會在2個表上都加3號共享鎖
46 TM 75056 0 3 0 0
46 TX 458758 888 6 0 0
此時在會話2上也執行向從表插入的動作會hang住
BYS@dg2>insert into b values(33);
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
49 TX 589856 1025 0 4 0 主從表插入後產生了2個TX鎖,這說明這是2條不同的獨立的記錄,不是爭用同一條記錄
49 TM 75054 0 3 0 0
49 TM 75056 0 3 0 0
46 TM 75054 0 3 0 0
46 TM 75056 0 3 0 0
49 TX 524298 1245 6 0 0 插入成功,修改值未決
46 TX 589856 1025 6 0 1 46阻塞49,6代表插入成功,修改值未提交是未決狀態
有一個TX鎖正在申請4號鎖,是因為2條記錄的修改值都是未決狀態違反了引用完整性約束從而產生阻塞。導致156會話hang住不能前進。
a是主表,定義了id欄位為主鍵
b是從表,id欄位是引用主表的id欄位
結論:
BYS@dg2>select distinct sid from v$mystat;
SID
----------
17
BYS@dg2>create table a(id int primary key);
Table created.
BYS@dg2>create table b(id references a(id));
Table created.
BYS@dg2>insert into a values(1);
1 row created.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
17 TM 75054 0 3 0 0 insert由於有從屬關係因此會在2個表上都加3號共享鎖
17 TM 75056 0 3 0 0
17 TX 458758 888 6 0 0
BYS@dg2>select sid,event from v$session_wait where sid in (49,17);
SID EVENT
---------- ----------------------------------------------------------------
17 SQL*Net message to client
49 SQL*Net message from client
BYS@dg2>col object_name for a20
BYS@dg2>select object_name from dba_objects where object_id in(75054,75056);
OBJECT_NAME
--------------------
A
B
BYS@dg2>commit; 鎖會隨著事務的結束而釋放
Commit complete.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
no rows selected
BYS@dg2>select * from a;
ID
----------
1
BYS@dg2>select * from b;
no rows selected
BYS@dg2>update a set id=111 where id=1;
1 row updated.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
17 TM 75054 0 3 0 0 update現在只對主表有鎖定,從表沒有鎖定
17 TX 393219 1031 6 0 0
BYS@dg2>commit;
Commit complete.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
no rows selected
BYS@dg2>select * from a;
ID
----------
111
BYS@dg2>delete a;
1 row deleted.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
17 TM 75054 0 3 0 0 delete也是隻對主表有鎖定,從表沒有鎖定
17 TX 524310 1244 6 0 0
BYS@dg2>commit;
Commit complete.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
no rows selected
#########################################################################
BYS@dg2>insert into b values(2); --直接給從表插入記錄,如果主表沒有的話,會報錯違反引用完整性約束
insert into b values(2)
*
ERROR at line 1:
ORA-02291: integrity constraint (BYS.SYS_C0011302) violated - parent key not found
BYS@dg2>insert into a values(2);
1 row created.
BYS@dg2>commit;
Commit complete.
BYS@dg2>select * from a;
ID
----------
2
BYS@dg2>select * from b;
no rows selected
BYS@dg2>insert into b values(2);
1 row created.
BYS@dg2>select * from b;
ID
----------
2
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
17 TM 75054 0 3 0 0
17 TM 75056 0 3 0 0 從表的insert也會對主從表同時加鎖
17 TX 196634 1043 6 0 0
BYS@dg2>commit;
Commit complete.
BYS@dg2>delete b;
1 row deleted.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
17 TM 75054 0 3 0 0
17 TM 75056 0 3 0 0 從表delete也會對主從表同時加鎖
17 TX 393245 1031 6 0 0
#############################################
BYS@dg2>select distinct sid from v$mystat;
SID
----------
46
BYS@dg2>select * from a;
ID
----------
2
BYS@dg2>select * from b;
no rows selected
BYS@dg2>insert into a values(33);
1 row created.
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
46 TM 75054 0 3 0 0 insert由於有從屬關係因此會在2個表上都加3號共享鎖
46 TM 75056 0 3 0 0
46 TX 458758 888 6 0 0
此時在會話2上也執行向從表插入的動作會hang住
BYS@dg2>insert into b values(33);
BYS@dg2>select sid,type,id1,id2,lmode,request,block from v$lock where type in('TM','TX');
SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
49 TX 589856 1025 0 4 0 主從表插入後產生了2個TX鎖,這說明這是2條不同的獨立的記錄,不是爭用同一條記錄
49 TM 75054 0 3 0 0
49 TM 75056 0 3 0 0
46 TM 75054 0 3 0 0
46 TM 75056 0 3 0 0
49 TX 524298 1245 6 0 0 插入成功,修改值未決
46 TX 589856 1025 6 0 1 46阻塞49,6代表插入成功,修改值未提交是未決狀態
有一個TX鎖正在申請4號鎖,是因為2條記錄的修改值都是未決狀態違反了引用完整性約束從而產生阻塞。導致156會話hang住不能前進。
相關文章
- 模擬epoll的飢餓場景
- [20181030]模擬分散式事務掛起導致TX鎖爭用.txt分散式
- ANALYZE導致的阻塞問題分析
- ORACLE透明加密場景模擬Oracle加密
- pytorch dataloader num_workers引數設定導致訓練阻塞PyTorch
- 記一次鎖使用不當導致Dubbo執行緒阻塞問題執行緒
- 連結伺服器查詢導致的阻塞伺服器
- 使用IDEA模擬git命令使用的常見場景IdeaGit
- 5601. 設計有序流 (場景模擬)
- 專案中多次操作SharedPreferences導致ANR場景的解決
- 資料型別隱式轉換導致的阻塞資料型別
- MySQL中2個select被阻塞場景的原因MySql
- Apollo模擬平臺如何Hold住99.9999%的複雜場景?
- 分散式鎖導致的超賣問題分散式
- 簡單模擬死鎖
- MySQL空間暴漲150G導致鎖定,發生了什麼MySql
- @Transactional 中使用執行緒鎖導致了鎖失效執行緒
- 基於simulink的模擬鎖相環和數字鎖相環建模與對比模擬
- 模擬SQLserver死鎖現象SQLServer
- AdornerDecorator的CacheMode繫結和windows鎖屏導致TableControl鎖死問題Windows
- Jmeter(五十)_效能測試模擬真實場景下的使用者操作JMeter
- sock鎖檔案導致的MySQL啟動失敗MySql
- MySQL死鎖系列-常見加鎖場景分析MySql
- 小議“悲觀鎖和樂觀鎖”的原理、場景、示例
- 【分散式鎖的演化】“超賣場景”,MySQL分散式鎖篇分散式MySql
- 如何解鎖元宇宙?應用場景決定商業化變現元宇宙
- Oracle死鎖一例(ORA-00060),鎖表導致的業務死鎖問題Oracle
- 記錄一個 nameko standalone rpc 應答不消費導致阻塞的問題RPC
- sysbench花式踩坑之三:自增值導致的鎖等待
- (效能測試)--記錄一次高可用場景導致CPU資源升高
- 科普:導致“餓怒”的兩個決定因素
- [20191204]sqlplus特殊定義導致的問題.txtSQL
- MySQL Online DDL導致全域性鎖表案例分析MySql
- Python 使用socket模擬http請求,從阻塞到協程PythonHTTP
- 多次密碼錯誤導致登入介面鎖定,可以刪除網站的 runtime 資料夾密碼網站
- mumu模擬器設定代理
- 面試官:你說說互斥鎖、自旋鎖、讀寫鎖、悲觀鎖、樂觀鎖的應用場景面試
- Redis分散式鎖(二):鎖超時後導致多個執行緒獲得鎖的解決方案Redis分散式執行緒
- springboot+redis分散式鎖-模擬搶單Spring BootRedis分散式