What is ETag in ABAP RAP?

ETag (Entity Tag) is a mechanism used for concurrency control and optimizing the performance of OData services. It plays a key role in the optimistic concurrency control used within ABAP RAP, allowing clients and servers to avoid unnecessary updates when data has not changed.

The ABAP RESTful Application Programming Model to ensure consistent data changes with the help of two approaches: Optimistic Concurrency Control and  Pessimistic Concurrency Control (Locking). In this tutorial we are going to learn about Optimistic Concurrency Control and how ETag uses it for handling unnecessary updates.

Optimistic Concurrency Control: Optimistic concurrency control allows multiple users to access data simultaneously, ensuring transaction integrity while preventing inconsistencies and unintentional modifications to data that has already been changed.

Pessimistic Concurrency Control (Locking): Pessimistic concurrency control prevents multiple users from modifying the same data in the database at the same time by locking the data during the transaction, ensuring exclusive access for one user at a time.

How ETag works?

When optimistic concurrency control is enabled for RAP business objects, the OData client must send an ETag value with every update. The server compares the sent ETag with the current one. If they match, the change is accepted; if not, the update is rejected. This ensures that the client only changes data it intended to modify and prevents changes made by others in between. When an entity is modified, the ETag is updated to record the new version of the data.

When Client 1 and Client 2 send a GET request to the database, both receive the same response, say ‘X’. When Client 1 sends updated entity records, the database first checks if the current value matches the ETag value. If it does, the database updates the records and the ETag value is also updated. Meanwhile, if Client 2 sends an UPDATE request at the same time, the ETag value on the database is now ‘Y’, which does not match the ‘X’ value. As a result, Client 2’s update request is rejected.

ETag Definition

The ETag in the behavior definition of the business object entity. ETag fields are dedicated administrative fields on the database table that log data access and modification. Check out the List of administrative fields.

ETag can be defined in two ways:
ETag Master: An entity is an ETag master, if changes of the entity are logged in a field that is part of the business object entity. This field must be specified as an ETag field in the behavior definition.
ETag master in Behavior definition
ETag Dependent: An entity is defined as ETag dependent if the changes of the entity are logged in a field of another BO entity. In this case, there must be an association to the ETag master entity.

ETag Implementation

An ETag check is only possible, if the ETag field is updated with a new value whenever the data set of the entity instance is changed or created. That means, for every modify operation, except for the delete operation, the ETag value must be uniquely updated.
Managed Scenario: In managed scenario, the administrative field can automatically updated if annotation is been used on the element.
@Semantics.systemDateTime.localInstanceLastChangedAt: true
In scenarios where the managed RAP BO provider handles the ETag fields, it is recommended that you specify the respective fields as read-only using the field characteristic read only in the behavior definition.

Unmanaged Scenario: Unlike in managed scenarios, the application developer in the unmanaged scenario must always ensure that the defined ETag field is correctly updated on every modify operation in the application code of the relevant operations, including for updates by actions.

Leave a Comment

Your email address will not be published. Required fields are marked *