点滴心头,厚积薄发。

导航

How to get session information and USERENV details.

Posted on 2013-03-06 22:59  biotwang  阅读(112)  评论(0)    收藏  举报

有时候我们需要知道当前连接会话的session信息,有多种办法的到当前session的相关信息。

1 V$MYSTAT
通过这个视图可以得到基本的信息,通常我通过该视图得到SID

SQL> select sid from v$mystat where rownum=1;

SID
----------
1564


2 USERENV
userenv是个历史遗留功能,保留它只是为了向后兼容。Oracle建议你是用sys_context函数功能来替代userenv功能。
这个是强大的内置函数,通过该函数可以得到登陆用户的相关信息(即userenv namespace下的参数),比如要从v$session获得信息:

SQL> SELECT Sid,serial#,audsid,username From v$session Where Sid=Userenv('SID');

SID        SERIAL#    AUDSID     USERNAME
---------- ---------- ---------- ------------------------------
1564       65273      4294967295 SYS


3 SYS_CONTEXT
与Userenv类似功能的还有SYS_CONTEXT函数,后者比前者提够的更为强大的功能,oracle强烈建议用有SYS_CONTEXT替代Userenv使用。

SQL> SELECT Sid,serial#,audsid,username From v$session Where Sid=SYS_CONTEXT('USERENV','SID');

SID        SERIAL#    AUDSID     USERNAME
---------- ---------- ---------- ------------------------------
1564       65273      4294967295 SYS


sys_context函数是Oracle提供的一个获取环境上下文信息的预定义函数。该函数用来返回一个指定namespace下的parameter值。该函数可以在SQL和PL/SQL语言中使用。
sys_context实际上就是一个Oracle存储和传递参数的容器访问函数。我们登入Oracle服务器,是带有会话信息session_info和其他一些属性信息。
其中,有一些是Oracle预定义的,登录系统的时候自动填入到指定的变量中。还有一些是我们自己定义到其中,用于传递值使用的。

下面是sys_context函数的使用格式:

sys_context(‘namespace’,’parameter’{,length});


其中,namespace是存储信息的一个组group单位,namespace是按照类别进行分类的。一个namespace下可以有多个参数值,通过不同的parameter进行区分。
namespace是预先定义好的SQL标识符,而parameter是可以任意大小写非敏感的字符串,不超过30位长度。
函数返回值为varchar2类型,长度默认为256位。如果需要限制这个默认值,可以数据length参数作为新的返回长度值。
设置namespace指定parameter值,可以使用dbms_session.set_context方法进行。

//自定义一个namespace,并且规定的设置的方法句柄;…………………….step 1
SQL> create context Test using set_test_context;

Context created

//定义方法…………………….step 2
create or replace procedure set_test_context
(
  vc_value in varchar2
)
is
begin
 dbms_session.set_context('Test','a1',vc_value);
end set_test_context;

//设置上值…………………….step 3
SQL> exec set_test_context('m');

PL/SQL procedure successfully completed

//获取这个值
SQL> select sys_context('Test','a1') from dual;

SYS_CONTEXT('TEST','A1')
------------------------------------
m


step1-3很重要,因为Test namespace为自定义的namespace,所以需要这样的设置,以确定权限所属。

sys_context函数最常用的就是userenv命名空间下的系列参数。下面是其中一些参数(非全部):
from: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions184.htm

CLIENT_INFO
Returns up to 64 bytes of user session information that can be stored by an application using the DBMS_APPLICATION_INFO package.

select sys_context('USERENV', 'CLIENT_INFO') from dual;
exec dbms_application_info.set_client_info('TEST');
select sys_context('USERENV', 'CLIENT_INFO') from dual;


CURRENT_USER
The name of the database user whose privileges are currently active. This may change during the duration of a session to reflect the owner of any active definer's rights object.
When no definer's rights object is active, CURRENT_USER returns the same value as SESSION_USER. When used directly in the body of a view definition, this returns the user
that is executing the cursor that is using the view; it does not respect views used in the cursor as being definer's rights.

DB_NAME
Name of the database as specified in the DB_NAME initialization parameter.

HOST
Name of the host machine from which the client has connected.

SERVER_HOST
The host name of the machine on which the instance is running.

INSTANCE
The instance identification number of the current instance.

INSTANCE_NAME
The name of the instance.

LANGUAGE
The language and territory currently used by your session, along with the database character set, in this form.

LANG
The abbreviated name for the language, a shorter form than the existing 'LANGUAGE' parameter.

SESSIONID
The auditing session identifier. You cannot use this attribute in distributed SQL statements.

SID
The session ID.

TERMINAL
The operating system identifier for the client of the current session.