PL/SQL 过程如何判断它是否来自并发程序 运行?

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

我想编写一个过程,当 运行 来自并发程序时将输出记录到 Oracle 并发管理器日志,但当 运行 “独立”时写入 dbms_output。

有没有办法从 PL/SQL 检查我的代码是否来自并发请求 运行?我能找到的最好方法是

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

但这很慢。有没有我可以查询的函数或 table 可以更有效地为我提供信息?

向过程添加一个布尔标志参数,您可以使用它来告诉它在调用该过程时要登录到的位置,然后传递来自两个不同 (concurrent/non-concurrent) 程序的不同标志:

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;
/

如果您想在程序中使用一次支票,那么您可以使用:

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

您最好像我们在闪电战报告代码中那样使用 fnd_global.conc_request_id:

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;