会话存储在某个地方?

时间:2012-11-01 13:21:59

标签: oracle oracle11g

V$SESSION视图包含当前登录的用户列表,但它们是存储在日志表中的某个位置还是什么?

我需要获取使用pl / sql登录的昨天用户列表(例如)

1 个答案:

答案 0 :(得分:4)

默认情况下,Oracle不存储此类信息。你有几个选择:

  • 启用系统事件审核(本例中为longon \ logoff)。
  • 写入系统事件(在longon之后,在注销之前)触发以收集该信息。

以下是示例:

SQL> show parameter audit_trail

NAME                                 TYPE        VALUE                          
------------------------------------ ----------- ------------------------------ 
audit_trail                          string      DB, EXTENDED       

SQL> select sessionid
  2       , userid
  3       , decode(action#, 100, 'logon', 101, 'logoff') 
  4    from sys.aud$ 
  5    where action# in (100, 101);

no rows selected

SQL> audit connect;

Audit succeeded.

SQL> conn hr/hr
Connected.
SQL> select sessionid
  2       , userid
  3       , decode(action#, 100, 'logon', 101, 'logoff')
  4    from sys.aud$
  5   where action# in (100, 101);

 SESSIONID USERID                         DECODE                                
---------- ------------------------------ ------                                
   1000712 HR                             logon                                 

SQL> conn hr/hr
Connected.
SQL> select sessionid
  2       , userid
  3       , decode(action#, 100, 'logon', 101, 'logoff')
  4   from sys.aud$
  5  where action# in (100, 101);

 SESSIONID USERID                         DECODE                                
---------- ------------------------------ ------                                
   1000712 HR                             logon                                 
   1000712 HR                             logoff                                
   1000713 HR                             logon   

以触发器为例:

SQL> create table Logon_history(
  2    sessionid  number,
  3    userid     varchar2(31),
  4    logon_date timestamp,
  5    logoff_date timestamp
  6  )
  7  ;

Table created


 SQL> create or replace trigger TR_LOGON_STAT after logon
  2  on database
  3  begin
  4    insert into Logon_history(Sessionid, Userid, Logon_Date, Logoff_Date)
  5      values(sys_context('userenv', 'sessionid'), user, systimestamp, null);
  6  end;
  7  /

Trigger created

SQL> create or replace trigger TR_LOGOFF_STAT before logoff
  2  on database
  3  begin
  4    insert into Logon_history(Sessionid, Userid, Logon_Date, Logoff_Date)
  5      values(sys_context('userenv', 'sessionid'), user, null,systimestamp);
  6  end;
  7  /

Trigger created

 SQL> select sessionid
  2       , userid
  3       , logon_date
  4       , logoff_date
  5  from Logon_history
  6  ;

  no rows selected

 SQL> conn hr/hr
 Connected.

 SQL> select sessionid
  2       , userid
  3       , logon_date
  4       , logoff_date
  5  from Logon_history
  6  ;

Sessionid   Userid   Logon_Date                       Logoff_Date 
-------------------------------------------------------------------------------- 
 2490674    HR       01-NOV-12 11.46.23.421000 PM   
 2490672    HR                                     01-NOV-12 11.46.23.343000 PM