v$session和kill session权限问题

开发测试程序的自动重连的功能需要对有对v$session查看和kill session权限

简单的就是赋一下select any dictionary和alter system 权限,但权限大的操作,一般考虑存储过程实现

网上实用的存储过程

DROP PROCEDURE P_KILL_SESSION;

CREATE OR REPLACE PROCEDURE p_kill_session (
   p_user   IN   VARCHAR2,
   p_sid    IN   VARCHAR2
)
AS
   v_sql   VARCHAR2 (327);
BEGIN
   SELECT 'alter system kill session '''|| SID || ',' || serial# ||''''
     INTO v_sql
     FROM v$session
    WHERE username = p_user AND SID = p_sid;
dbms_output.put_line(v_sql);
   EXECUTE IMMEDIATE v_sql;
EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
   raise_application_error
                          (-20001,
                              'sid:'
                           || p_sid
                           || ' does not exists, or the session user is not '
                           || p_user
                          );
END;
/

CREATE OR REPLACE procedure p_kill_user_session(p_sid in number)
 as
v_username varchar2(100);
v_sid      number;
begin
select sys_context('userenv', 'session_user'),sys_context('userenv', 'sid') INTO v_username, v_sid from dual;
if p_sid != v_sid then
    p_kill_session(v_username, p_sid);
else
    raise_application_error(-20000, 'can not kill current session!');
end if;
end;

/

注意的是存储过程里v$session授权问题,此表是sys.v_v$session的同义词,在过程里必须要表的原有属主(sys)进行授权,不管你是否有dba权限

02年前dba就提出问题参考http://www.itpub.net/thread-55622-1-1.html


你可能感兴趣的:(session,kill,user,application,System,Dictionary)