[[{“value”:”
Etag is used to achieve Optimistic concurrency control.
Now comes the question : What is Optimistic Concurrency?
Optimistic concurrency control ensures data consistency by preventing lost updates when multiple users read the same data simultaneously and attempt to modify it.
Scenario without ETag:
User A and user B reading the same record from database
User A update the data and save the record in database.
User B without knowing data has been already updated, updates the same record and saves the data , at this time data saved by user A will be overwritten by data of user B. This will result in data inconsistency. To handle such scenario we will go for ETag (Entity tag) .
Scenario with ETag:
For implementing ETag, we will include a ETag field which will uniquely identify each updating of the record whenever its updated.
User A and user B reading the same record from database. Both get ETag value E1.
User A update the data and save the record in database. Sets the ETag value to E2.
User B without knowing data has been already updated, tries to update the same record but this time the Etag value in database is E2 which is not matching with ETag value in E1. So, update fails for User B with error code 412. This will ensure data consistency.
Steps to implement ETag in RAP:
Step1: Take one field of the database table with datatype ‘abp_locinst_lastchange_tstmpl’. This would be our etag field.
@EndUserText.label : ‘Student table’
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zjd_student_tbl {
key client : abap.clnt not null;
key id : sysuuid_x16 not null;
name : abap.char(30);
address : abap.char(50);
marks : abap.int1;
percentage : abap.int1;
createdby : abap.char(15);
createdon : abp_locinst_lastchange_tstmpl;
}
Step2: Add “etag master Createdon” in behaviour definition
Step3: Make the field read only.
Once the above three steps are done, RAP ensures optimistic concurrency control.
By leveraging ETags, SAP RAP provides an efficient and scalable approach to concurrency management. Understanding and implementing ETags is essential for building robust, reliable, and enterprise-ready RAP applications that can safely handle concurrent user updates.
Keep learning! Keep exploring 🙂
“}]]
Read More Technology Blog Posts by Members articles
#abap