有什么方法可以从oracle中的gv $ session获取有关当前会话的信息? [英] Is there any way to get information about current session from gv$session in oracle?
问题描述
有什么方法可以在Oracle的GV$SESSION
视图中唯一标识当前会话?
Is there any way to uniquely identify current session in GV$SESSION
view in Oracle?
我遇到的问题是,在Oracle RAC配置的情况下,以下查询可能返回多个行:
I've faced with the problem that the following query may return more than one row in case of Oracle RAC configuration:
SELECT SID, SERIAL#
FROM GV$SESSION
WHERE AUDSID = Sys_Context('USERENV', 'SESSIONID')
AND SID = Sys_Context('USERENV', 'SID');
也不选择使用V$MYSTAT
,因为V$MYSTAT
在当前会话中可能不可访问(例如,禁用统计功能时).
Using V$MYSTAT
is not an option either, because V$MYSTAT
may not be accessible for the current session (for example when statistic is disabled).
推荐答案
尝试一下:
SELECT SID, SERIAL#
FROM V$SESSION
WHERE AUDSID = Sys_Context('USERENV', 'SESSIONID');
由于您对当前会话感兴趣,因此当前会话必须在本地实例上(根据定义),因此请使用V$SESSION
而不是GV$SESSION
.另外,您只需AUDSID
即可唯一标识您的会话.
Since you're interested in current session, the current session must be on the local instance (by definition), so use V$SESSION
instead of GV$SESSION
. Also, all you need is AUDSID
to uniquely identify your session.
如果您出于某些原因确实需要使用GV$SESSION
(无法想象为什么会这样),则可以改用以下方法:
If you've got some reason you really need to use GV$SESSION
(can't imagine why that would be), you could do this instead:
SELECT SID, SERIAL#
FROM GV$SESSION
WHERE AUDSID = Sys_Context('USERENV', 'SESSIONID')
AND INST_ID = USERENV('Instance');
此外,获取当前会话的SID
的另一种方法是:
Also, an alternate way to get the SID
of the current session is:
select sid from v$mystat where rownum=1;
希望有帮助.
这篇关于有什么方法可以从oracle中的gv $ session获取有关当前会话的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!