Hello,
In this beginner blog post we are going to see how we can Implement Locking in ABAP Restful Application Programming Model using Unmanaged scenario.
Note: Creation of RAP Un-Managed application is not part of this Blog Post.
Locking in general prevents simultaneous modification access to data when more then one user tries to work on same record.
Record is Exclusively locked in case of Pessimistic Lock scenario. Locking of record is ensured by applying enqueue locks using Enqueue Lock Function Modules.
The lifetime of such an exclusive locks:
The enqueue lock is released once the ABAP session is terminated.
Locking is not Invoked by CREATE operation in any case, since there is no active instance that can be locked during the CREATE operation.
In Behavior Definition Locking can be defined by using below options:
If you do not want the standard locking mechanism by the managed business object framework, you can create an unmanaged lock in the managed scenario. This enables you to implement your own locking logic for the business object.
Create Lock Object
Highlighted are the Lock Object Name and created Function Modules which we are going to use soon.
In Behavior Definition of Root Interface view, maintain Lock Master as shown in below picture.
Activate Behavior Definition and as a result
New method available FOR LOCK in Unmanaged scenarios in Behavior Implementation class and must be implemented for the lock master entities.
Lock master only responsible to lock Root Entities.
New Lock method gets generated as shown in below picture:
Lock method is generated with Importing and Changing Parameters:
There are two changing parameters failed and reported.
Below is code to Implement Locking on Root Entity
METHOD lock.
TRY.
DATA(lock) = cl_abap_lock_object_factory=>get_instance( iv_name = 'EZLOCKSTUDENT' ).
CATCH cx_abap_lock_failure INTO DATA(exception).
RAISE SHORTDUMP exception.
ENDTRY.
LOOP AT keys ASSIGNING FIELD-SYMBOL(<lfs_student>).
TRY.
lock->enqueue(
it_parameter = VALUE #( ( name = 'ID' value = REF #( <lfs_student>-Id ) ) )
).
CATCH cx_abap_foreign_lock INTO DATA(foreign_lock).
APPEND VALUE #(
id = keys[ 1 ]-Id
%msg = new_message_with_text(
severity = if_abap_behv_message=>severity-error
text = 'Record is locked by' && foreign_lock->user_name
)
) TO reported-student.
APPEND VALUE #(
id = keys[ 1 ]-Id
) TO failed-student.
CATCH cx_abap_lock_failure INTO exception.
RAISE SHORTDUMP exception.
ENDTRY.
ENDLOOP.
ENDMETHOD.
To test this functionality, we are going to open same record in two Browser windows. Since we do not have multiple users to validate the functionality.
We have created one record which is available on both the browsers windows assuming different users are viewing same record.
user 1 (on Left) starts editing record and have not saved it.
Meanwhile User 2 (on Right) tries to Delete same record which User 1 is trying to Update. User 2 select the record and tries to Delete, gets confirmation Popup to delete record.
User 2 gets Confirmation Popup to Delete selected record and click on Delete button.
But since we have Implemented Pessimistic Locking, Record is already locked by User 1 while updating it.
and In this case User 2 will get Error Message which we have filled in Reported Parameter.
Thanks-