如何查詢oracle會話及鎖 如何查鎖了哪張表?如何殺掉會話
===================================================================================================================
--檢視鎖表:
SELECT O.OWNER, O.OBJECT_NAME, O.OBJECT_TYPE, S.SID, S.SERIAL#
FROM V$LOCKED_OBJECT L, DBA_OBJECTS O, V$SESSION S
WHERE L.OBJECT_ID = O.OBJECT_ID
AND L.SESSION_ID = S.SID
ORDER BY O.OBJECT_ID, XIDUSN DESC;
--解鎖
alter system kill session '372,20909';
--查死鎖 可以查出來哪個庫的哪個表鎖了--
select s.sid,
s.serial#,
s.LOGON_TIME,
s.MACHINE,
s.TERMINAL,
l.oracle_username,
l.os_user_name,
a.object_name,--被鎖的表名
l.locked_mode
from v$locked_object l, dba_objects a, v$session s
where a.object_id = l.object_id
and l.session_id = s.sid;
alter system kill session 'sid,serial#';
--在oralce中 會話失效了,但是鎖還在,所以,如果某張表被鎖了,查詢是哪個會話鎖的,不僅要看活著的會話,還需要看失效的會話
--1.使用以上sql語句,根據表名查詢對應的會話記錄, 2.kill調對應的會話(包括失效的會話)
--查詢某個session的程序狀態
SELECT a.username,
a.machine,
a.program,
b.spid,
a.sid,
a.serial#,
a.status,--顯示當前會話的狀態
c.piece,
c.sql_text
FROM v$session a, v$process b, v$sqltext c
WHERE a.sid = '1869'
AND b.addr = a.paddr
AND a.sql_address = c.address(+)
ORDER BY c.piece;
===================================================================================================================
--死鎖是資料庫經常發生的問題,資料庫一般不會無緣無故產生死鎖,死鎖通常都是由於我們應用程式的設計本身造成的。產生死鎖時,如何解決呢,下面是常規的解決辦法:
--1)執行下面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;
--2)查處引起死鎖的會話
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;
--這裡會列出SID
--3) 查出SID和SERIAL#:
--查V$SESSION檢視:
SELECT SID,SERIAL#,PADDR FROM V$SESSION WHERE SID='剛才查到的SID';
--這一步將得到PADDR
--4)查V$PROCESS檢視:
SELECT SPID FROM V$PROCESS WHERE ADDR='剛才查到的PADDR';
--這一步得到SPID
--5)殺死程序
--(1)在資料庫中,殺掉ORACLE程序:
ALTER SYSTEM KILL SESSION '查出的SID,查出的SERIAL#';
--(2)如果在ORACLE中不能殺死程序,我們只能到作業系統中,使用作業系統命令殺死程序
KILL -9 “剛才查出的SPID”
--在WINDOWS平臺,可以是偶那個orakill。
===================================================================================================================
--oracle層面殺會話
select object_name,machine,s.sid,s.serial# from v$locked_object l,dba_objects o,v$session s where l.object_id=o.object_id and l.session_id=s.sid;(查詢被鎖物件)
alter system kill session '5,55'; (其中5,55分別是上面查詢出的sid,serial#)
--作業系統層面殺程序(linux)
select spid, osuser, s.program from v$session s,v$process p where s.paddr=p.addr and s.sid=5; (5是上面的sid)
kill -9 55555(55555是剛查詢出的spid)
--作業系統層面殺程序(win)
SQL>host orakill 例項名 55555;(555是剛查詢出的spid)
===================================================================================================================