1、生成Kill Session語句
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),舞鋼企業(yè)網(wǎng)站建設(shè),舞鋼品牌網(wǎng)站建設(shè),網(wǎng)站定制,舞鋼網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,舞鋼網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
select?'alter?system?kill?session?'''?||?SID?||','?||?SERIAL#?||?''';'?from
(
select?distinct?a.sid,?a.Serial#,?status,?machine,?LOCKWAIT,?logon_time
from?v$session?a,?v$locked_object?b
where?(a.status?=?'ACTIVE'?or?a.status?=?'INACTIVE')
and?a.sid?=?b.session_id
and?b.ORACLE_USERNAME='XYHISTEST'--加上用戶名是避免把其他系統(tǒng)的會話也關(guān)閉,以免傷及無辜
)
2、批量執(zhí)行第一步生成的語句
alter?system?kill?session?'sid,serial#';
--alter?system?kill?session?'6976,33967';
3、查詢oracle用戶名,機(jī)器名,鎖表對象
SELECT?l.session_id?sid,?s.serial#,?l.locked_mode,l.oracle_username,
l.os_user_name,s.machine,?s.terminal,?o.object_name,?s.logon_time
FROM?v$locked_object?l,?all_objects?o,?v$session?s
WHERE?l.object_id?=?o.object_id
AND?l.session_id?=?s.sid
ORDER?BY?sid,?s.serial#?;
/*或者
select?s.SID,?s.OSUSER,?p.spid?as?OSPID,?s.MACHINE,?s.TERMINAL,?s.PROGRAM
from?v$session?s,?v$process?p
where?s.sid?=?6??--session_id
and?s.paddr?=?p.addr;
*/
4、查詢是執(zhí)行何sql語句導(dǎo)致鎖表的
select?b.sql_text
from?v$session?a,?v$sql?b
where?a.sid?=?6?--session_id
and?a.SQL_ADDRESS?=?b.ADDRESS(+);
/*--或者
SELECT?l.session_id?sid,?s.serial#,?l.locked_mode,?l.oracle_username,?s.user#,
l.os_user_name,s.machine,?s.terminal,a.sql_text,?a.action
FROM?v$sqlarea?a,v$session?s,?v$locked_object?l
WHERE?l.session_id?=?s.sid
AND?s.prev_sql_addr?=?a.address
ORDER?BY?sid,?s.serial#;
*/
首先你要知道表鎖住了是不是正常鎖?因為任何DML語句都會對表加鎖。
你要先查一下是那個會話那個sql鎖住了表,有可能這是正常業(yè)務(wù)需求,不建議隨便KILL session,如果這個鎖表是正常業(yè)務(wù)你把session kill掉了會影響業(yè)務(wù)的。
建議先查原因再做決定。
(1)鎖表查詢的代碼有以下的形式:
select count(*) from v$locked_object;
select * from v$locked_object;
(2)查看哪個表被鎖
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;
(3)查看是哪個session引起的
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;
(4)查看是哪個sql引起的
select b.username,b.sid,b.serial#,c.* from v$locked_object a,v$session b,v$sql c where a.session_id = b.sid
and b.SQL_ID = c.sql_id and c.sql_id = ''
order by b.logon_time;
(5)殺掉對應(yīng)進(jìn)程
執(zhí)行命令:alter system kill session'1025,41';
其中1025為sid,41為serial#.
Oracle數(shù)據(jù)庫出現(xiàn)死鎖的時候可以按照以下處理步驟加以解決:
第一步:嘗試在sqlplus中通過sql命令進(jìn)行刪除,如果能夠刪除成功,則萬事大吉!但通常情況下,出現(xiàn)死鎖時,想通過命令行或者通過Oracle的管理工具刪除有死鎖的session,oracle只會將該session標(biāo)記為killed,但無法清除掉,往往需要通過第二步在操作系統(tǒng)層級進(jìn)行刪除!
Connected?to?Oracle9i?Enterprise?Edition?Release?9.2.0.1.0?
Connected?as?quik
SQL?select?xidusn,?object_id,?session_id,?locked_mode?from?v$locked_object;?--查死鎖的對象,獲取其SESSION_ID
XIDUSN?OBJECT_ID?SESSION_ID?LOCKED_MODE
----------?----------?----------?-----------
10?30724?29?3
10?30649?29?3
SQL?select?username,sid,serial#?from?v$session?where?sid=29;?--根據(jù)上步獲取到的sid查看其serial#號
USERNAME?SID?SERIAL#
------------------------------?----------?----------
QUIK?29?57107
SQL?alter?system?kill?session?'29,57107';?--刪除進(jìn)程,如已經(jīng)刪除過,則會報ora-00031的錯誤;否則oracle會將該session標(biāo)記為killed狀態(tài),等待一段時間看能否會自動消失,如長時間消失不掉,則需要做后續(xù)步驟
alter?system?kill?session?'29,57107'
ORA-00031:?session?marked?for?kill
SQL?select?pro.spid?from?v$session?ses,v$process?pro?where?ses.sid=29?and?ses.paddr=pro.addr;?--查看spid號,以便在操作系統(tǒng)中根據(jù)該進(jìn)程號刪除進(jìn)程
SPID
------------
2273286
第二步:進(jìn)入操作系統(tǒng)進(jìn)行刪除進(jìn)程,本示例的操作系統(tǒng)是IBM aix。
login:?root?--錄入用戶名
root's?Password:?--錄入密碼
*******************************************************************************
*?*
*?*
*?Welcome?to?AIX?Version?5.3!?*
*?*
*?*
*?Please?see?the?README?file?in?/usr/lpp/bos?for?information?pertinent?to?*
*?this?release?of?the?AIX?Operating?System.?*
*?*
*?*
*******************************************************************************
Last?unsuccessful?login:?Fri?Apr?23?14:42:57?BEIDT?2010?on?/dev/pts/1?from?10.73
.52.254
Last?login:?Fri?Apr?23?15:27:50?BEIDT?2010?on?/dev/pts/2?from?10.73.52.254
#?ps?-ef|grep?2273286?--查看進(jìn)程詳情
root?2289864?2494636?0?17:07:15?pts/1?0:00?grep?2273286
oracle?2273286?1?0?14:38:24?-?0:21?oracleQUIK?(LOCAL=NO)
#?kill?-9?2273286?--刪除進(jìn)程,小心操作,別寫錯進(jìn)程號,如果oracle的關(guān)鍵進(jìn)程被刪,數(shù)據(jù)庫會崩潰的!
#?ps?-ef|grep?2273286?--再次查看
root?2289864?2494636?0?17:07:15?pts/1?0:00?grep?2273286
For?Windows,?at?the?DOS?Prompt:?orakill?sid?spid
For?UNIX?at?the?command?line?kill?–9?spid