Skip to main content

ARTICLE FOR SETTING SPECIFIC SESSIONS AND RELEASING ETC

ARTICLE FOR SETTING SPECIFIC SESSIONS AND RELEASING ETC


---------------------------------------------------------------------------------------------------
Use Oracle's DBMS_APPLICATION_INFO to Prevent Routines from Running Simultaneously


On occasion, it may be necessary to implement business logic where, when one routine or business process is being run by a user, certain other routine(s) should not be allowed to run and vice versa. This requirement may be a functional or a technical need.

You may be able to relate to the following scenarios:

A critical accounting process is triggered by a user. It is necessary to avoid multiple runs of the same process at any given time. Therefore, if one user has triggered the process, others should be prevented from doing so.
Heavy data loading processes or data interfacing may require that two routines do not run simultaneously. When a heavy upload is triggered, simultaneous uploads from other sources should be prevented until resources are available for use.
Several heavy-duty batch processes exist in the system and proper resource sharing should be done. Running multiple batch processes may slow down the system and eventually result in resource contentions. Preventing such identified processes from running simultaneously is necessary.
Two or more processes carry out DML activities on common objects, which may result in locking issues. This could give rise to errors like 0RA-00054 (resource busy) or ORA-00060(deadlock detected).
One solution would be to request that users avoid running identified processes simultaneously; however, this is a manual control that can be easily overlooked, requiring the DBA to kill the bad sessions to avoid database congestion.

Using code, developers normally use flags or locks set in some table that is referred to by all sessions. This will not work if the session is killed or closed abnormally. Explicit coding needs to be done in such cases to make the system foolproof; this could again amount to an overhead.

The Oracle provided package, DBMS_APPLICATION_INFO, could be used to satisfy this requirement, thus preventing the abnormal situations mentioned above from arising.

Register the routine that is being run by calling DBMS_APPLICATION_INFO.SET_MODULE. This tags the routine name in V$SESSION dynamic view. Check for this information at the beginning of the concerned routines. Once a routine is complete, unregister it. Note when a session is abnormally closed or killed, V$SESSION is updated accordingly. There is no need of commit or rollback or explicit updates.

Three columns, MODULE, ACTION and CLIENT_INFO can be set in V$SESSION using DBMS_APPLICATION_INFO which could later be referred to, to identify processes already running in the system. The following example sets these three columns.

begin
  dbms_application_info.set_module(module_name => 'File Upload Process',
                                   action_name => 'Uploading file');
  dbms_application_info.set_client_info('WDA001');
end;
On running the above code, the three columns are set with the value provided, and can be viewed from V$SESSION.

SQL> select module, action, client_info
  2  from v$session
  3  where sid = (select sid from v$mystat where rownum = 1);

MODULE               ACTION               CLIENT_INFO
-------------------- -------------------- --------------------
File Upload Process  Uploading file       WDA001
If direct access to the dictionary is not available, the set data can be viewed using the same package.

declare
  l_client_info  varchar2(64);
  l_module      varchar2(48);
  l_action        varchar2(32);
begin
  dbms_application_info.read_module(l_module, l_action);
  dbms_application_info.read_client_info(l_client_info);

  dbms_output.put_line(l_module||'/'||l_action||'/'||l_client_info);
end;

Output:
File Upload Process/Uploading file/WDA001
The data returned in the above case is only for the current session. For our situation it is required that access be given to V$SESSION to view all sessions data. Select privilege can be granted to the public so that all logins have access to it.

SQL> grant select on v_$session to public;

Grant succeeded.
Let's look at an example. This is a very specific example but generic code could be written along similar lines. AM_DEP1 and AM_DEP2 are inter-dependent procedures and should not run simultaneously. The following function checks to see if a particular procedure session is active and returns the status accordingly.

create or replace function chk_dep_run (pi_proc in varchar2)
return boolean is
  l_cnt  pls_integer;
begin
  select 1
  into   l_cnt
  from   v$session
  where  module = pi_proc
  and    rownum = 1;    

  return(false);
exception
  when no_data_found then
    return(true);
end;
AM_DEP1 and AM_DEP2 will call the above function in the beginning. Once the function confirms that the other procedure is not active, the current routine will be registered and executed. Registering will tag the necessary information in V$SESSION view.

create or replace procedure am_dep1 is
  ..
begin
  if not chk_dep_run('AM_DEP2') then
    dbms_output.put_line('AM_DEP2 is currently running');
    return;
  end if;

  dbms_application_info.set_module('AM_DEP1', 'running apps1'); --register
  -- continue executing the code.
  ..
  dbms_application_info.set_module(null, null);  --unregister
end;


create or replace procedure am_dep2 is
  ..
begin
  if not chk_dep_run('AM_DEP1') then
    dbms_output.put_line('AM_DEP1 is currently running');
    return;
  end if;

  dbms_application_info.set_module('AM_DEP2', 'running apps2');  --register
  -- continue executing the code.
  ..
 dbms_application_info.set_module(null, null);  --unregister
end;
Conclusion

Use of DBMS_APPLICATION_INFO depends a lot on the coding. Flexibility is provided to put any type of character string in the dictionary table columns and then refer to it from the application. Proper coding standards should be implemented for using this feature as per the application requirement. Call the package at the beginning of all application sessions to set the appropriate information. The package may be called multiple times to update the dictionary during the lifetime of the session.




---------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

FND_PROFILE API In Oracle Apps

 FND_PROFILE API In Oracle Apps --------------------------------------------------------------------------------------------------- FND_PROFILE API is used very frequently in oracle apps development. The major purpose of this API is to: ·                      Retrieve user profile values for the current run-time environment ·                      Set user profile values for the current run-time environment FND_PROFILE API has different procedures and functions which are most frequently used. Following are the brief explanation of them: 1. FND_PROFILE.PUT:  This can be used to put a value to the specified user profile option. Example:  FND_Profile.Put(‘PROFILE_NAME’, ‘New_Value’) FND_Profile.Put(’USERNAME’, Usr_Name) FND_Profile.Pu...

PO Matching Setting in Purchase Order Shipment Line(2-way,3-way,4-way)

[PO] Matching Setting in Purchase Order Shipment Line Invoice matching can be set in five different areas of Oracle Purchasing: In the list below, a setting at any level will override the settings above it. 1. Oracle Purchasing Options a. Navigate to: Setup > Organizations > Purchasing Options b. Select Default Alternative Region 2. Supplier Information a. Navigate to: Supply Base > Suppliers b. Query on specific supplier c. Click on Open d. Select Receiving Alternative Region 3. Line Types a. Navigate to: Setup > Purchasing > Line Types b. In the Receipt Required field: Yes = 3-way, No = 2-way 4. Items a. Navigate to: Items > Master Items b. Query on specific item c. Select Purchasing Alternative Region d. In the Invoice Matching section: Yes = 3-way, No = 2-way 5. Purchase Order Shipments a. Navigate to: Purchase Orders > Purchase Orders b. Enter (header and) line information c. Click on Shipments button d. Select More Alternative Region Ramification of Invoice ...

Query To get the SMTP Server Details for a database server

Query To get the SMTP Server Details for a database server -------------------------------------------------------------------------------------------------------------------------------------- select fscpv.parameter_value "SMTP Host Name"       --SMTP protocol uses default port number 25 for outgoing emails       ,25                    "SMTP Port Number"       ,fscpt.description   from fnd_svc_comp_params_tl fscpt       ,fnd_svc_comp_param_vals fscpv  where fscpt.parameter_id = fscpv.parameter_id    and fscpt.display_name = 'Outbound Server Name' --'Inbound Server Name'    and fscpt.language = 'US'; --------------------------------------------------------------------------------------------------------------------------------------