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

来源:互联网 发布:淘宝做的最好的店铺 编辑:程序博客网 时间:2024/06/06 17:31
很多情况下,我们需要使用 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';


转载请注明作者出处及原文链接,否则将追究法律责任:

作者:xiangsir

原文链接:http://blog.csdn.net/xiangsir/article/details/8957559

QQ:444367417

MSN:xiangsir@hotmail.com