oracle context
context:上下文
context的信息也就是当前会话中的环境变量,如:登录的session_id,用户名,语言等信息
查看context中的属性信息。
oracle默认的为我们创建了一个context叫userenv(user environment)
SYS_CONTEXT('USERENV','TERMINAL')
SYS_CONTEXT('USERENV','LANGUAGE')
SYS_CONTEXT('USERENV','SESSIONID')
SYS_CONTEXT('USERENV','INSTANCE')
SYS_CONTEXT('USERENV','ENTRYID')
SYS_CONTEXT('USERENV','ISDBA')
SYS_CONTEXT('USERENV','NLS_TERRITORY')
SYS_CONTEXT('USERENV','NLS_CURRENCY')
SYS_CONTEXT('USERENV','NLS_CALENDAR')
SYS_CONTEXT('USERENV','NLS_DATE_FORMAT')
SYS_CONTEXT('USERENV','NLS_DATE_LANGUAGE')
SYS_CONTEXT('USERENV','NLS_SORT')
SYS_CONTEXT('USERENV','CURRENT_USER')
SYS_CONTEXT('USERENV','CURRENT_USERID')
SYS_CONTEXT('USERENV','SESSION_USER')
SYS_CONTEXT('USERENV','SESSION_USERID')
SYS_CONTEXT('USERENV','PROXY_USER')
SYS_CONTEXT('USERENV','PROXY_USERID')
SYS_CONTEXT('USERENV','DB_DOMAIN')
SYS_CONTEXT('USERENV','DB_NAME')
SYS_CONTEXT('USERENV','HOST')
SYS_CONTEXT('USERENV','OS_USER')
SYS_CONTEXT('USERENV','EXTERNAL_NAME')
SYS_CONTEXT('USERENV','IP_ADDRESS')
SYS_CONTEXT('USERENV','NETWORK_PROTOCOL')
SYS_CONTEXT('USERENV','BG_JOB_ID')
SYS_CONTEXT('USERENV','FG_JOB_ID')
SYS_CONTEXT('USERENV','AUTHENTICATION_TYPE')
SYS_CONTEXT('USERENV','AUTHENTICATION_DATA')
例子:
查看当前session的字符集信息
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
----------------------------------------------------------------
UTF8
1 row selected.
创建自定义oracle context
Application Context是内存中的一组name-value对,application context从属于某个命名空间(namespace);
用户只能通过一个自定义封装包或存储过程中调用dbms_session.set_context来设置application context的值;
用户使用sys_context(<namespace>,<name>)来获取某个application context的值。
语法:
DBMS_SESSION.SET_CONTEXT
( namespaceVARCHAR2, attribute VARCHAR2, value VARCHAR2, username VARCHAR2, client_idVARCHAR2 );
namespace context的命名空间 --必要参数
attribute 属性值 --必要参数
value 值 --必要参数
username 用户名(默认为Null) --可选参数
client_id 指定的clientid --可选参数
Application context分为三种
dabase session-based application context --local application context
global application context --global application context
client session-based application context --通过OCI管理使用的,存储在客户端内存的context
Local applicationcontext
context存储在UGA中,本地context是session/server process级别的,当会话/serverprocess终止时,context也会结束
Global application context
context存储在SGA中,SGA消失,全局context也就会结束;它常常用于跨会话,应用于与会话无关的场景
例子:
1.创建一个context
SQL> create context context_only using pkg_only;
Context created.
2.建立与上下文先关的包来封装(也就是上面的pkg_name)
SQL> create package pkg_only
2 as
3 procedure put(key varchar2,value varchar2);
4 end;
5 /
Package created.
SQL> create package body pkg_only
2 as
3 procedure put(key varchar2,value varchar2)
4 as
5 begin
6 dbms_session.set_context('context_only',key,value);
7 end;
8 end;
9 /
Package body created.
3.设置存储过程输入输出
SQL> exec pkg_only.put('name','only');
PL/SQL procedure successfully completed.
4.调用context
SQL> select sys_context('context_only','name') from dual;
SYS_CONTEXT('CONTEXT_ONLY','NAME')
--------------------------------------------------------------------------------
only
转自:https://blog.51cto.com/ionly/1605229