In the world of ABAP RAP (RESTful Application Programming Model), ensuring data consistency when multiple users interact with the same record is critical. Without safeguards, simultaneous edits can lead to data conflicts and inconsistencies.
This is where ETag comes into play, offering a robust solution to synchronize data updates across users.
What is ETag?
An ETag (Entity Tag) is a mechanism used to handle concurrent updates in RAP. When multiple users attempt to modify the same record, the ETag system ensures they are working with the latest version of the data.
If a user tries to update a record that has been modified since they last fetched it, the system prompts them to refresh the record, maintaining data integrity and preventing conflicts.
This is achieved by leveraging a timestamp field (commonly LASTCHANGEDAT
), which is used as a version indicator for the record.
How is ETag Implemented in RAP?
Here’s a step-by-step guide to implementing ETag in RAP:
Step 1: Define the ETag Master Field
In the behavior definition of your root interface view, specify an ETag master field (typically a timestamp field like LASTCHANGEDAT
). This field will be used to compare versions of the record.
Step 2: Annotate the Timestamp Field
Mark the timestamp field with the appropriate annotation so that RAP recognizes it for ETag functionality.
Step 3: Enable ETag in the Projection
In the projection behavior definition, use the use etag
keyword to expose ETag functionality to the OData service.
How Does ETag Work in RAP?
🔹 Scenario:
-
User A opens a record (e.g., Travel ID 3) and starts editing the Description field.
-
The system includes an
If-Match
condition in the OData request payload, based on the current ETag value (timestamp). -
User B also opens the same record and modifies the Description field at the same time.
-
When User B submits the change, the If-Match value sent doesn’t match the current ETag (since User A modified it).
-
The system rejects the update and notifies User B that the record is not up-to-date.
This ensures that only the most recent version of the record is updated, preventing conflicts and data loss.