ABAP RAP Locking – ABAP Restful Application Programming Model (RAP)
2023-12-28 09:42:37 Author: blogs.sap.com(查看原文) 阅读量:10 收藏

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.

Previous Blog Posts

ABAP RAP – File Upload Large Objects (LOB)

ABAP RAP – #ISOLATED

ABAP RAP – #CHANGE_SET

ABAP RAP – Instance Authorization

ABAP RAP – Global Authorization

ABAP RAP – Factory Actions

Ref : https://help.sap.com/docs/abap-cloud/abap-rap/pessimistic-concurrency-control-locking?version=sap_btp

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.

  1. In RAP business objects, enqueue locks are used for pessimistic concurrency control. Exclusively locking happens on data.
  2. Locking is defined in the behavior definition of the Business Object entity.
  3. The lock mechanism can only be defined in the behavior definition in the interface layer.
  4. 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.

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-


文章来源: https://blogs.sap.com/2023/12/28/abap-rap-locking-abap-restful-application-programming-model-rap/
如有侵权请联系:admin#unsafe.sh