当前位置:操作系统 > Unix/Linux >>

如何终止特定RAC实例上的session

如何终止特定RAC实例上的session
 
很多情况下,我们需要使用 alter system kill session 终止数据库上的某个 session,释放该 session 所占用的资源来解决问题。
比如,daniel 所在的环境就经常使用如下语句批量语句杀死数据库中的 session。
 
select 'ALTER SYSTEM KILL SESSION '''||b.sid||', '||b.serial#||''';'
from gv$access a,gv$session b
where a.SID=b.SID
and b.program like 'dis%'
group by b.sid,b.serial#;
 
该脚本在单实例情况下(gv$全部替换为 v$)还非常凑效,确实能够杀掉所需的 session。
但是在 RAC 环境下,必须将生成的脚本在所有实例上执行,才能杀掉全部需要杀掉的 session。
我们来关注一下 alter system kill session 的语法:
 
The KILL SESSION clause lets you mark a session as terminated, roll back ongoing transactions, release all session locks, and partially recover session resources. To use this clause, your instance must have the database open. Your session and the session to be terminated must be on the same instance unless you specify integer3.You must identify the session with the following values from the V$SESSION view:
 
KILL SESSION 'integer1, integer2[, @integer3]' [IMMEDIATE]
 
For integer1, specify the value of the SID column.
For integer2, specify the value of the SERIAL# column.
For the optional integer3, specify the ID of the instance where the target session to be killed exists. You can find the instance ID by querying the GV$ tables.
 
IMMEDIATE Specify IMMEDIATE to instruct Oracle Database to roll back ongoing transactions, release all session locks, recover the entire session state, and return control to you immediately.
 
在 RAC 环境下,我们使用 gv$session 视图,获取相应的 session 信息,除了要获取 session 的 sid 和 serial# 外,还需要获取 session 所运行的 inst_id,
并在 alter system kill session 子句中用 @inst_id 指定,才能杀掉该 session,否则必须连接到相应的 RAC 实例才能杀掉。
 
示例:
 
RAC 环境的节点1 上运行如下 session:
21:15:16 sys@RAC> SELECT SID, SERIAL#, INST_ID FROM GV$SESSION WHERE USERNAME='SCOTT';
 
 
       SID    SERIAL#    INST_ID
---------- ---------- ----------
       158         75          1
       
在 RAC 环境的节点2上运行如下语句便可终止该 session:
 
节点2:
21:17:32 sys@RAC> alter system kill session '158,75,@1';
 
System altered.
 
节点1:
21:19:26 scott@RAC> select * from dual;
select * from dual
*
ERROR at line 1:
ORA-00028: your session has been killed
 
在 RAC 环境下,批量脚本应修改为如下格式:
 
col kill_session for a50
select 'alter system kill session '''||sid||', '||serial#||', @'||inst_id||';' kill_session from gv$session where username='SCOTT';
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,