首先你要知道表鎖住了是不是正常鎖?因?yàn)槿魏蜠ML語句都會(huì)對表加鎖。\x0d\x0a\x0d\x0a你要先查一下是那個(gè)會(huì)話那個(gè)sql鎖住了表,有可能這是正常業(yè)務(wù)需求,不建議隨便KILLsession,如果這個(gè)鎖表是正常業(yè)務(wù)你把sessionkill掉了會(huì)影響業(yè)務(wù)的。\x0d\x0a建議先查原因再做決定。\x0d\x0a\x0d\x0a(1)鎖表查詢的代碼有以下的形式:\x0d\x0aselectcount(*)fromv$locked_object;\x0d\x0aselect*fromv$locked_object;\x0d\x0a(2)查看哪個(gè)表被鎖\x0d\x0aselectb.owner,b.object_name,a.session_id,a.locked_modefromv$locked_objecta,dba_objectsbwhereb.object_id=a.object_id;\x0d\x0a(3)查看是哪個(gè)session引起的\x0d\x0aselectb.username,b.sid,b.serial#,logon_timefromv$locked_objecta,v$sessionbwherea.session_id=b.sidorderbyb.logon_time;\x0d\x0a\x0d\x0a(4)查看是哪個(gè)sql引起的\x0d\x0aselectb.username,b.sid,b.serial#,c.*fromv$locked_objecta,v$sessionb,v$sqlcwherea.session_id=b.sid\x0d\x0aandb.SQL_ID=c.sql_idandc.sql_id=''\x0d\x0aorderbyb.logon_time;\x0d\x0a\x0d\x0a(5)殺掉對應(yīng)進(jìn)程\x0d\x0a執(zhí)行命令:altersystemkillsession'1025,41';\x0d\x0a其中1025為sid,41為serial#.
創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè),為客戶提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)開發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗(yàn),各類網(wǎng)站都可以開發(fā),成都品牌網(wǎng)站建設(shè),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計(jì),建網(wǎng)站費(fèi)用,建網(wǎng)站多少錢,價(jià)格優(yōu)惠,收費(fèi)合理。
Session1創(chuàng)建測試表:
SQL create table test (id number (10) not null , name varchar(20), primary key(id));
Table created.
SQL desc test;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(10)
NAME VARCHAR2(20)
SQL insert into test values(001,'tom');
1 row created.
SQL insert into test values(002,'lisa');
1 row created.
SQL insert into test values(003,'joy');
1 row created.
SQL insert into test values(004,'jia');
1 row created.
查看test表信息
SQL update test set name='xue' where name='joy';
1 row updated.
SQL commit;
Commit complete.
SQL select * from test updata;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
重新打開session 2:
SQL select * from test;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
update模擬鎖表
SQL update test set name='da' where name='tom';
1 row updated.
注:不提交
Session2查詢:
SQL select * from test;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
查看哪個(gè)表被鎖
SQL select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id;
OWNER
------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
SESSION_ID LOCKED_MODE
---------- -----------
SYS
TEST
23 3
查看是哪個(gè)session引起的
SQL select b.username,b.sid,b.serial#,logon_time from v$locked_object a,v$session b where a.session_id = b.sid order by b.logon_time;
USERNAME SID SERIAL# LOGON_TIM
------------------------------ ---------- ---------- ---------
SYS 23 23 02-JAN-20
殺掉對應(yīng)進(jìn)程
SQL alter system kill session'23,23';
System altered.
其中23為sid,23為serial#.
SQL select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id;
no rows selected
SQL select b.username,b.sid,b.serial#,logon_time from v$locked_object a,v$session b where a.session_id = b.sid order by b.logon_time;
no rows selected
session 1查詢:
SQL select * from test;
select * from test
*
ERROR at line 1:
ORA-00028: your session has been killed
SQL select * from test;
select * from test
*
ERROR at line 1:
ORA-01012: not logged on
Process ID: 5366
Session ID: 23 Serial number: 23
重新連接SQL
[oracle@localhost ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Thu Jan 2 11:39:53 2020
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL select * from test updata;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
在對指定表做append操作,其他再做truncate時(shí)候,會(huì)產(chǎn)生鎖表,如下驗(yàn)證步驟,
1、創(chuàng)建測試表,
create table test_lock(id number, value varchar2(200));
2、執(zhí)行append語句;并且不做提交,insert /*+append*/ into test_lock values(1,1);
3、再次執(zhí)行清表語句,truncate table test_lock;報(bào)鎖表錯(cuò)誤,
4、查看鎖表語句,發(fā)現(xiàn)被鎖表,
select b.object_name, t.*
from v$locked_object t, user_objects b
where t.object_id = b.object_id
oracle數(shù)據(jù)庫分行級(jí)鎖和表級(jí)鎖。用select * from table-name for update完成行級(jí)鎖。用delete或update完成表級(jí)鎖。你鎖定的資源 別人會(huì)等待你的提交語句或回退語句完成以后再繼續(xù)進(jìn)行。