Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Note: Creation of RAP Un-Managed application is not part of this Blog Post.
Pessimistic Concurrency Control (Locking)
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.
Exclusive lock is released when RAP LUW is terminated with a COMMIT or ROLLBACK.
In case of Draft record, scenarios, the lock is not dependent to LUW or the ABAP sessions. Lock remains active till draft Instance is available. The lock is released when draft is discarded or deleted or if the draft instance is activated and the LUW 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 RAP business objects, enqueue locks are used for pessimistic concurrency control. Exclusively locking happens on data.
Locking is defined in the behavior definition of the Business Object entity.
The lock mechanism can only be defined in the behavior definition in the interface layer.
Its must not be specified in a projection behavior definition.
In Behavior Definition Locking can be defined by using below options:
Lock master – This is used to Implement Locking in un-Managed scenario, In this case RAP Framework will not handle any Locking for BO. Developer needs to write logic to Handle locking.
Lock master unmanaged – Implemented in Managed scenario, if application developer do not want framework to take care of Locking on Business Object.
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.
Implementing Locking
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.
The Failed parameter is used to log the causes when a lock fails.
The Reported parameter is used to store messages about the fail cause. and can also be used to report Error message to frontend.
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.