‎2008 Oct 30 4:11 AM
Is there any possibility to lock the code to get executed by only one session of ABAP. Locking the code before select statement till commit. so that when the same program is executed in parallel session will wait to execute this part of the code.
REPORT Z_XYZ
...
...
...
SELECT * INTO ls_vbak FROM vbak
UP TO 1 ROWS
WHERE (lt_where).
ENDSELECT.
IF sy-subrc IS INITIAL .
....
Else
...
...
Endif.
Call function Bapi
..
...
Commit.
Thanks in advance.
‎2008 Oct 30 2:18 PM
Yes, you can lock a program from getting executed more than once at the same time.
All you need to do is put a lock on the program name using the following function.
call function 'ENQUEUE_E_TRDIR'
exporting
* MODE_TRDIR = 'X'
name = sy-repid
* X_NAME = ' '
* _SCOPE = '2'
* _WAIT = ' '
* _COLLECT = ' '
EXCEPTIONS
FOREIGN_LOCK = 1
SYSTEM_FAILURE = 2
OTHERS = 3
.
if sy-subrc 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.And then of course call the DEQUEUE when you are done. If another person runs this program, they will get an error message saying who is currently locking the program.
Regards,
Rich Heilman
‎2008 Oct 30 4:13 AM
Hi,
You cannot lock a program from getting executed in multiple sessions, what i would suggest is you could create a lock on the database table, on which you want to perform the operation. If you take a lock on a db table, only one session/user can use modify that table.
Lock can be taken on a single record of the database table also.
Thanks & Regards,
Navneeth K.
‎2008 Oct 30 4:47 AM
Hi,
The SAP System Table TRDIR has a field called EDTX which is basically the EDITOR lock filed. Edit Lock facility is given in the PROGRAM ATTRIBUTES. The EDITOR LOCK is a check box given in the PROGRAM ATTRIBUTES. If this field is SET then the program gets locked and if this is Unchecked the the program is unlocked.
Please check
http://abaplovers.blogspot.com/2008/06/sap-abap-program-editor-lockunlock.html
Edited by: Neenu Jose on Oct 30, 2008 5:47 AM
Edited by: Neenu Jose on Oct 30, 2008 5:51 AM
‎2008 Oct 30 4:50 AM
‎2008 Oct 30 5:13 AM
HI Sam,
You can do it in this way.
When you create a lock object System automatically creat two function module.
1. ENQUEUE_<Lockobject name>. to insert the object in a queue.
2. DEQUEUE_<Lockobject name>. To remove the object is being queued through above FM.
Thanks,
Chidanand
‎2008 Oct 30 1:40 PM
Can we put a lock on select statment of vbak table and
release the lock after commit statment ?
So that the other session cannot access select of vbak till the sales gets updated in vbak table. so that when next parallel session selects from vbak table, then it find the entry in vbak and no sales order gets created by parallel session ...
Could you please reply earliest because i am in such a need.
‎2008 Oct 30 1:43 PM
Hi,
Hope the following Threads will help you regarding your problem.
http://help.sap.com/saphelp_nw04s/helpdata/en/41/7af4c5a79e11d1950f0000e82de14a/frameset.htm
Thanks.
Nitesh
‎2008 Oct 30 1:44 PM
Hi Sam ,
we can do it by using SAP memory (GET, SET)
create a parameter ID
and check the parameter ID at beging of the program and if it is initial pass 'X' to that parameter ID and do u r code
finally clear the value in parameter ID
Kiran
‎2008 Oct 30 1:47 PM
Kiran,
Iam not good in SET/GET
Could you please help me withclear coding.
‎2008 Oct 30 2:13 PM
Let parameter id is 'ZABCD'
data : v_abcd type c.
GET PARAMETER ID 'ZABCD' FIELD v_abcd
if v_abcd is initial.
v_abcd = 'X'.
SET PARAMETER ID 'ZABCD' FIELD v_abcd
---
---
--- u r code.
endif.
clear v_abcd.
SET PARAMETER ID 'ZABCD' FIELD v_abcd.
‎2008 Oct 30 2:18 PM
Yes, you can lock a program from getting executed more than once at the same time.
All you need to do is put a lock on the program name using the following function.
call function 'ENQUEUE_E_TRDIR'
exporting
* MODE_TRDIR = 'X'
name = sy-repid
* X_NAME = ' '
* _SCOPE = '2'
* _WAIT = ' '
* _COLLECT = ' '
EXCEPTIONS
FOREIGN_LOCK = 1
SYSTEM_FAILURE = 2
OTHERS = 3
.
if sy-subrc 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.And then of course call the DEQUEUE when you are done. If another person runs this program, they will get an error message saying who is currently locking the program.
Regards,
Rich Heilman
‎2008 Oct 30 3:07 PM
Rich,
you came into picture GREAT.
Thanks for the explation with the code.
my basic quesion :
Is there any way by which we can set up a lock at the file so that it cannot be used by another session if it is already in use by one session?
1).My problem :
basically I am in dilema, because my program runs with the file and submits file to another program then
check vbak table for exixtance of PO# in the file
and if no po is there then this second program submits to third program where Bapi builds sales order data for the PO# in file and
then commits ...so that the sales order created.
Is there any possibility, that we can lock the file so that another parallel session cannot use the same file till first session completes the execution till commit stmt ?2). if my program runs in parallel sessions,
will the first session lock the program with enque ?
is it unlocked automatically after commit stmt ?
or
What about DEQUEUE ? Is it simple same parametes passing into FM except the name of the FM as
call function 'DENQUEUE_E_TRDIR' ?
==================================
Kiran suggested set parameter ( I don't have much idea )
what parameter id , i can use ? can I use simply 'ZABCD' ( will it work ?)
I kiran's suggestion works...
Can I use this in first program
data : v_abcd type c.
GET PARAMETER ID 'ZABCD' FIELD v_abcd
if v_abcd is initial.
v_abcd = 'X'.
SET PARAMETER ID 'ZABCD' FIELD v_abcd
---
---
u r code.
endif.
and in third program
clear v_abcd.
SET PARAMETER ID 'ZABCD' FIELD v_abcd.