I have the below code which i have written to debug some information
I want to call GLOBAL.INIT which needs to be called only once per user session
Hence i have used PLSQL table to cache the value and if the SID is different then again call GLOBAL.INIT but the caching is not working,Kindly help me
create or replace procedure dbg(message in VARCHAR2) is
sid1 NUMBER;
idx NUMBER;
Type sidcache is table of NUMBER index by binary_integer;
sidcache1 sidcache;
sid2 NUMBER;
begin
select sid
into sid1
FROM v$session se
where se.audsid = SYS_CONTEXT('userenv', 'sessionid');
BEGIN
sid2 := sidcache1(sid1);
EXCEPTION
WHEN no_data_found THEN
sidcache1(sid1) := sid1;
global.INIT('100', 'SYSTEM');
dbms_output.put_line(1);
WHEN OTHERS THEN
dbms_output.put_line(sqlerrm);
END;
WRITE_DEBUG.pr_debug('LD', message);
end dbg;
EDIT#1:-
After trying Mike’s answer the same requirement was acheived using the below
create or replace procedure dbg(message in VARCHAR2) is
sid2 NUMBER;
BEGIN
sid2 := WRITE_DEBUG.sidcache1(1);
EXCEPTION
WHEN no_data_found THEN
WRITE_DEBUG.sidcache1(1) := 1;
global.INIT('100', 'DEBUG');
WHEN OTHERS THEN
dbms_output.put_line(sqlerrm);
END;
WRITE_DEBUG.pr_debug('LD', message);
end dbg;
By default PL/SQL variables are initialised each time the block is entered and the values are not carried across calls. This means that each time you call this procedure, the sidcache1 variable will be null.
An easy way to get around this would be to declare sidcache1 as a variable in a package. Package variables are initialised once per session, with each session having their own unique set of variables. I don’t think you would even need to store the SID, a Boolean might even be sufficient to record if global.init has been called.