sapui5tutors SAPUI5 • Fiori • SAP BTP Step-by-step tutorials Real project examples Interview Q&A
Practical SAPUI5 • Fiori • SAP BTP tutorials and interview prep

RAP Optimistic Locking: ETag Handling Explained

Two users, one record, no warning — the problem

Two people open the same sales order. Both change the delivery date. The second save silently overwrites the first, and nobody notices until the customer calls. Optimistic locking exists to prevent exactly this: instead of locking the record for everyone (pessimistic), the system detects the collision at save time and refuses the second write.

In RAP, the mechanism is the ETag — a version stamp on each entity, checked automatically on every modify.


Declaring the ETag in the behavior definition

You pick one field to serve as the version stamp — typically a last-changed timestamp — with etag master:

define behavior for ZI_SalesOrder
  persistent table zsalesorder
  lock master
  etag master LastChangedAt
{
  create;
  update;
  delete;
}

For child entities, etag dependent by _Parent ties the child's version to the parent's. And in the behavior projection, use etag re-exposes ETag handling for that service.

The ETag field must change on every write. A timestamp maintained by the framework (or a determination) is the standard choice — never let users edit it.


How the check works at runtime

The flow follows the standard OData optimistic-concurrency pattern:

  • Read: the client fetches the entity and receives its current ETag (derived from the ETag field).
  • Modify: the client sends the change with an If-Match header carrying the ETag it saw.
  • Match: ETags agree — nobody touched the record in between — and the write succeeds. The ETag field advances.
  • Mismatch: someone else saved first. RAP rejects the write with a concurrency error instead of overwriting their changes.

In a managed scenario the framework does all of this for you — the ETag comparison, the rejection, the error propagation. Your code never touches If-Match directly.


What the user sees on conflict

On a Fiori Elements UI, a failed ETag check surfaces as an error message on the object page — the user's changes are not lost, but they're not saved either. The standard recovery is: refresh to see the current data, reapply the intended change, save again.

For draft-enabled objects this is mostly painless, since the draft holds the user's in-progress work while they reconcile. For EML callers, the failed modify returns entries in FAILED/REPORTED with a concurrency message — check those structures instead of assuming success.

Optimistic locking assumes conflicts are rare but costly. It keeps the UI lock-free and fast, and only interrupts the user when two people genuinely collide — which is exactly when a human decision is needed.

If your RAP object doesn't declare an ETag, add one before go-live. Silent overwrites are the kind of bug that only shows up in production, at the worst possible moment.