Set up lock handling
SAP resources can be locked and unlocked from stateless Neptune Apps. The provided functionality takes care of the complete standard ENQUEUE / DEQUEUE process by making use of a background job which is triggered only when needed.
In order to be able to use this functionality, the setup procedure described in this article must be performed only once.
How to
In order to lock/unlock any SAP resource from your App ABAP class, function module /NEPTUNE/REQUEST_LOCKING must be used.
In the header structure:
FUNCTION: Function module to be used for ENQUEUE/DEQUEUEing.
PROCESS: 'E' for Enqueue / 'D' for Dequeue.
OBJ_TYPE and OBJ_KEY are used as identifiers for the locking requests.
In the Parameters table, the actual (IMPORTING) parameters to be used by the FM specified in FUNCTION are specified.
NAME: Parameter name in the FM.
TYPE: Parameter type in the FM.
VALUE: value for the parameter specified in NAME.
Event
Open SAP transaction SM62 → Background Events tab → Click on New.
Event: NEPTUNE_LOCK
Description: Neptune Lock Handler
Background Job
Open SAP transaction SE38.
Program: /NEPTUNE/HANDLER_LOCKING
and click 'Execute' (F8)
Enter the time in seconds between lock requests checks. Recommended and default value = 2.
Enter name of background user (mandatory). NB! This user name must be the same as the user running the background job.
Enter time in minutes to automatically release locks. (Optional) Leave blank to disable automatic release of locks.
Click on 'Save' and create the following variant:
Variant Name: DEFAULT
Click 'Save' again in order to save the variant.
Open SAP transaction SM36.
Enter Job Name: NEPTUNE_LOCK_HANDLER and press Enter.
the job will only have one step with the following settings:
User: Username that will lock/unlock resources on each request. This username will be the one that owns the locks on behalf of the requester.
ABAP Program Name: /NEPTUNE/HANDLER_LOCKING
Variant: DEFAULT
TIP: please make sure that the chosen User has enough permissions for locking/unlocking.
Click on 'Save'.
Finally, set the Start condition for the job. Click on 'Start condition'.
Click on 'After event'.
Event: NEPTUNE_LOCK
IMPORTANT: 'Periodic job' must be checked.
Click on 'Save' and the Job will be ready.
How Locks look like
After locking an object using Neptune Locking Mechanism, entries in SM12 will look like locked by the user who is running the background job.
But when trying to lock again the same object, FM /NEPTUNE/REQUEST_LOCKING will show the correct name of the user who has locked the object (and not the one who is running the background job).
Multiple parameters in lock objects
Support for multiple parameters in lock objects was added in Neptune UXP 4.0 SP04. For code example, please see this post: https://community.neptune-software.com/posts/multiple-parameters-in-lock-objects
Here is a simple demo report to demonstrate how it works. In the picture below, a Sales order is being locked/unlocked.
*---------------------------------------------------------------------* * Report ZLOCKTEST *---------------------------------------------------------------------* * Stateless locking and unlocking of sales order *---------------------------------------------------------------------* report zlocktest. start-of-selection. parameters: p_enque as checkbox, p_deque as checkbox. data: ls_header type /neptune/lock_h, ls_parameters type /neptune/lock_p, lt_parameters type standard table of /neptune/lock_p, ls_status type /neptune/lock_s, lv_type type string, lv_title type string, lv_desc type string. ls_header-obj_type = 'VBAK'. ls_header-obj_key = '0050000005'. ls_parameters-name = 'VBELN'. ls_parameters-type = 'VBAK-VBELN'. ls_parameters-value = '0050000005'. append ls_parameters to lt_parameters. if p_enque = abap_true. " Enqueue object ls_header-function = 'ENQUEUE_EVVBAKE'. ls_header-process = 'E'. call function '/NEPTUNE/REQUEST_LOCKING' exporting wa_header = ls_header importing * msg_code = msg_type = lv_type msg_title = lv_title msg_description = lv_desc tables it_parameters = lt_parameters. if lv_type eq 'Error'. write:/ 'Object could not be locked'. else. write:/ 'Object was locked'. endif. endif. if p_deque = abap_true. " Dequeue object ls_header-function = 'DEQUEUE_EVVBAKE'. ls_header-process = 'D'. call function '/NEPTUNE/REQUEST_LOCKING' exporting wa_header = ls_header importing * msg_code = msg_type = lv_type msg_title = lv_title msg_description = lv_desc tables it_parameters = lt_parameters. if lv_type eq 'Error'. write:/ 'Object could not be unlocked'. else. write:/ 'Object was unlocked'. endif. endif.
Client dependent locks (From Neptune DXP - SAP Edition v.5.2.0)
When calling the function module /NEPTUNE/REQUEST_LOCKING, this parameter should be added:
CLIENT_DEPENDENT = 'X'.
When the code is updated to use the parameter, these steps can be implemented (as an example for client 100 and 200):
A - Delete current defined jobs of NEPTUNE_LOCK_HANDLER in all clients.
B - Declare one event per each client number in SM62. In your case:
-
NEPTUNE_LOCK_100
-
NEPTUNE_LOCK_200
C - Best practice is to include the client number also in the name of the job in each client (SM36):
-
Log on to client 100 and define job NEPTUNE_LOCK_HANDLER_100 triggered by event NEPTUNE_LOCK_100 in SM36.
-
Log on to client 200 and define job NEPTUNE_LOCK_HANDLER_200 triggered by event NEPTUNE_LOCK_200 in SM36.