We are building our own App for SAP S4HANA 1909. The frontend is built in a Microsoft based tool. We integrated the App to S4HANA using OData protocol.
One of the requirement is to lock the object, once the user clicks on edit in the App. So that nobody else can edit, either from the app, or in SAP GUI.
Talking about lock in SAP NetWeaver Gateway-OData(on-premise), till date I could not find any solution which will behave similar way as in SAP GUI because of rest property(as rest is stateless) so cant store the state there.
So possible solution is either use optimistic lock(Etag, Hash) or we can achieve using soft state. Soft state is where we are changing rest property from stateless to statefull for particular time, which is not good as it will consume lot of resources and advantages of REST like light weight, will be compromised.
Another option I tried is, to use the enqueue module (Ex: ENQUEUE_EMEKKOE ), in a OData service which the App can call once the user hits edit. But the problem is, these function modules release the lock once the Service call ends. The locks are not persisted with this logic.
So after doing lot of debugging the standard lock mechanism, at last I was able to achieve lock which is at kernel level(not sure about any drawback) but it will behave same as in SAP GUI, So without wasting more time on theory let me show you the code below.
CALL 'C_ENQUEUE' "#EC CI_CCALL
ID 'OPCODE' FIELD '1' " enqueue request
ID 'ENQOBJ' FIELD iv_enqueue_object
ID 'GRANULES' FIELD it_lock_list
ID 'DELAY_ON_REJECT' FIELD ' '
ID 'USTP' FIELD '2'
ID 'COLLISION_UNAME' FIELD ev_user
ID 'COLLISION_OBJECT' FIELD ev_object
ID 'SYNCHRON' FIELD 'X'
ID 'USER' FIELD sy-uname
ID 'USVB' FIELD '00000000000000_OWNER_PERSIST'.
For removing the lock the code is as below
CALL 'C_ENQUEUE' "#EC CI_CCALL
ID 'OPCODE' FIELD '3' " Dequeue request
ID 'ENQOBJ' FIELD iv_enqueue_object
ID 'GRANULES' FIELD it_lock_list
ID 'DELAY_ON_REJECT' FIELD ' '
ID 'USTP' FIELD '2'
ID 'COLLISION_UNAME' FIELD ev_user
ID 'COLLISION_OBJECT' FIELD ev_object
ID 'SYNCHRON' FIELD 'X'
ID 'USER' FIELD sy-uname
ID 'USVB' FIELD '00000000000000_OWNER_PERSIST'.
We also wrapped this in OData service which the App can use to remove locks.
Below is the lock service.
No comments:
Post a Comment