Home > Net >  How can a PL/SQL procedure tell if it is being run from a concurrent program?
How can a PL/SQL procedure tell if it is being run from a concurrent program?

Time:11-17

I want to write a procedure that logs output to the Oracle concurrent manager log when run from a concurrent program, but writes to dbms_output when run "standalone".

Is there a way from PL/SQL to check whether my code is being run from a concurrent request? The best way I've been able to find is

select * from fnd_concurrent_requests
where oracle_session_id = userenv('SESSIONID');

but that's pretty slow. Is there a function or table I can query that gives me the information more efficiently?

CodePudding user response:

You can best use fnd_global.conc_request_id like we do in our blitz report code:

procedure write_log(p_text in varchar2, p_log_level in number default 1) is
begin
  if fnd_global.conc_request_id>0 then
    fnd_file.put_line(fnd_file.log,p_text);
  else
    fnd_log.string(p_log_level,'XXEN',p_text); --or your dbms_output.put_line() call
  end if;
end write_log;

CodePudding user response:

Add a boolean flag argument to the procedure that you can use to tell it where you want to log to when you call the procedure and then pass different flags from your two different (concurrent/non-concurrent) programs:

CREATE PROCEDURE my_proc(
  i_value1                 IN NUMBER,
  i_use_concurrent_logging IN BOOLEAN DEFAULT FALSE
)
IS
  -- Helper function so you only check the flag in one place.
  PROCEDURE log(value IN VARCHAR2)
  IS
  BEGIN
    IF i_use_concurrent_logging THEN
      -- put your concurrent logging code here.
      NULL;
    ELSE
      DBMS_OUTPUT.PUT_LINE(value);
    END IF;
  END;
BEGIN
  -- Do stuff.
  
  log('Stuff done');

  -- Do other stuff

  log('Other Stuff done');
END;
/

If you want to use your check once in the procedure then you could use:

CREATE OR REPLACE PROCEDURE my_proc(
  i_value1                 IN NUMBER
)
IS
  v_use_concurrent_logging BOOLEAN := FALSE;
  
  PROCEDURE log(value IN VARCHAR2)
  IS
  BEGIN
    IF v_use_concurrent_logging THEN
      -- put your concurrent logging code here.
      NULL;
    ELSE
      DBMS_OUTPUT.PUT_LINE(value);
    END IF;
  END;

BEGIN
  DECLARE
    v_exists INT;
  BEGIN
    SELECT 1
    INTO   v_exists
    FROM   fnd_concurrent_requests
    WHERE  oracle_session_id = userenv('SESSIONID')
    AND    ROWNUM = 1;
    
    v_use_concurrent_logging := TRUE;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      v_use_concurrent_logging := FALSE;
  END;

  -- Do stuff.
  
  log('Stuff done');

  -- Do other stuff

  log('Other Stuff done');
END;
/

db<>fiddle here

  • Related