Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Locking Mech not working as expected

abaper_guy
Active Participant
0 Likes
797

I wanted to test my own lock object for a custome table 'zemp_master'. I have created a lock object (WRITE LOCK). Now in my main user (SAPUSER) i have written the following program.

REPORT  ZLOCK_TEST.

CALL FUNCTION 'ENQUEUE_EZLOCK_1'
 EXPORTING
   MODE_ZEMP_MASTER       = 'E'
*   EMPNO                  = ''
*   X_EMPNO                = ' '
*   _SCOPE                 = '2'
*   _WAIT                  = ' '
*   _COLLECT               = ' '
* EXCEPTIONS
*   FOREIGN_LOCK           = 1
*   SYSTEM_FAILURE         = 2
*   OTHERS                 = 3
          .

IF SY-SUBRC <> 0.
 MESSAGE 'Lock Not done' TYPE 'I'.
ELSE.
 MESSAGE 'Lock done' TYPE 'I'.
ENDIF.

write:/ 'Hello'.

CALL FUNCTION 'DEQUEUE_EZLOCK_1'
* EXPORTING
*   MODE_ZEMP_MASTER       = 'S'
*   EMPNO                  =
*   X_EMPNO                = ' '
*   _SCOPE                 = '3'
*   _SYNCHRON              = ' '
*   _COLLECT               = ' '
          .

When I execute the above code , I get a MESSAGE 'LOCK DONE' and i dont click on the tick mark yet. Instead , i log in as different user ('Amber') and execute the following code.

REPORT  ZLOCK_ACCESS.

data: itab TYPE TABLE OF zemp_master WITH HEADER LINE.

select * FROM zemp_master into TABLE itab.

LOOP AT itab.
 write:/ itab-empno.
ENDLOOP.

Now my previous prog (for SAPUSER) is still enqued , at the same time i execute the above code from the user 'Amber' hoping that it wont allow me to read and fetch data from the database for zemp_master table. But it displays the data successfully.

So how can i test my lock object so that it doesnt allow any other user to read while the lock has not been released.

THANKS

I wanted to test my own lock object for a custome table 'zemp_master'. I have created a lock object (WRITE LOCK). Now in my main user (SAPUSER) i have written the following program.

REPORT  ZLOCK_TEST.

CALL FUNCTION 'ENQUEUE_EZLOCK_1'
 EXPORTING
   MODE_ZEMP_MASTER       = 'E'
*   EMPNO                  = ''
*   X_EMPNO                = ' '
*   _SCOPE                 = '2'
*   _WAIT                  = ' '
*   _COLLECT               = ' '
* EXCEPTIONS
*   FOREIGN_LOCK           = 1
*   SYSTEM_FAILURE         = 2
*   OTHERS                 = 3
          .

IF SY-SUBRC <> 0.
 MESSAGE 'Lock Not done' TYPE 'I'.
ELSE.
 MESSAGE 'Lock done' TYPE 'I'.
ENDIF.

write:/ 'Hello'.

CALL FUNCTION 'DEQUEUE_EZLOCK_1'
* EXPORTING
*   MODE_ZEMP_MASTER       = 'S'
*   EMPNO                  =
*   X_EMPNO                = ' '
*   _SCOPE                 = '3'
*   _SYNCHRON              = ' '
*   _COLLECT               = ' '
          .

When I execute the above code , I get a MESSAGE 'LOCK DONE' and i dont click on the tick mark yet. Instead , i log in as different user ('Amber') and execute the following code.

REPORT  ZLOCK_ACCESS.

data: itab TYPE TABLE OF zemp_master WITH HEADER LINE.

select * FROM zemp_master into TABLE itab.

LOOP AT itab.
 write:/ itab-empno.
ENDLOOP.

Now my previous prog (for SAPUSER) is still enqued , at the same time i execute the above code from the user 'Amber' hoping that it wont allow me to read and fetch data from the database for zemp_master table. But it displays the data successfully.

So how can i test my lock object so that it doesnt allow any other user to read while the lock has not been released.

THANKS

4 REPLIES 4
Read only

Former Member
0 Likes
767

You test it by setting a new lock for the other user.

Read only

AnjaneyaBhardwaj
Contributor
0 Likes
767

Not sure why are you not passing the field whose check has to be done ....You need to pass other parameter's as well and here sy-subrc eq 0 meand lock done you are checking other way around .....

Revert back if you need more info....


CALL FUNCTION 'ENQUEUE_*'
                              EXPORTING
                                mode_ycat_mrn  = 'E'
                                mandt          = sy-mandt
                                field1         =  wa_val-field1    
                                 x_field1       = ' '
                                _scope         = '2'
                                _wait          = ' '
                                _collect       = ' '
                              EXCEPTIONS
                                foreign_lock   = 1
                                system_failure = 2
                                OTHERS         = 3.
                            IF sy-subrc EQ 0.
write 'lock obtained  i am feeling lucky as i will update this as i have locked it '
else .
write 'locknot not obtained some one else is updating need to check after some time '
endif . 

to test you can keep it locked in debugging mode and dry to put another lock in another session by calling enqueue module again ...

Hope it solves ,

Thanks,

Anjaneya .

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
767

Hello Amber,

Please note that the SAP locks are logical locks, i.e., they don't lock the records physically(unlike database locks).

When you create the lock actually a lock argument is written in the enqueue table. Now if any other lock is requested, the system checks the arguments & based on the mode of the lock object(param MODE_ZEMP_MASTER) may or may not raise a lock collision!

In your program ZLOCK_ACCESS you need to check if a lock can be made(via ENQUEUE_EZLOCK_1) before selecting the data!

Hope you get the point.

BR,

Suhas

Read only

Former Member
0 Likes
767

1) Your code says, if sy-subrc = 0; 'Lock not done'. While if sy-subrc is 0, it means lock is done

2) Even if lock is done, you can access the table for READ. Hence, if you need to test the lock, try UPDATING the table