Search code examples
oracle-databaseprocedure

Oracle PL/SQL: How to detect if a procedure is ALREADY running?


Please suppose that we have a procedure inside a package:

MY_PACKAGE.MY_PROCEDURE

This procedure could be launched from many users.

How can I modify the procedure in order to detect if the procedure is at present running since launched from another user?

What is the safest way to detect it?

Thank you for considering my request.

EDIT 01: "It'll depend on why you need to know if a proc is already running or not" ==> If the procedure is at present running, it WON'T be launched again.


Solution

  • You can use the DBMS_APPLICATION_INFO package for such information.

    PROCEDURE MY_PROCEDURE(..) IS
    BEGIN
    DBMS_APPLICATION_INFO.SET_CLIENT_INFO('MY_PACKAGE.MY_PROCEDURE running');
    
       ... All your stuff
    
    DBMS_APPLICATION_INFO.SET_CLIENT_INFO(NULL);
    EXCEPTION
      WHEN OTHERS THEN
        DBMS_APPLICATION_INFO.SET_CLIENT_INFO(NULL);
        RAISE;
    END MY_PROCEDURE;
    

    In order to check it, you can select V$SESSION View:

    SELECT * 
    FROM v$session 
    WHERE client_info = 'MY_PACKAGE.MY_PROCEDURE running';
    

    If you get any records then the procedure is running.