ERPClimb logoERPClimb
RAP Business Objects
RAP / CDS / ODataintermediate 9 min read

RAP Business Objects Part 4: Locking, ETags and Concurrency Control

Preview · a free account unlocks the full lesson

Key takeaway

Understand how RAP protects data from conflicting changes: exclusive locks, ETags for stale data, the total ETag with draft, and how responsibilities differ between managed and unmanaged objects.

In this lessonShow sections

Explanation

Part 3 explained the transaction phases. Several users and systems often work on the same data at the same time, and this part explains the three mechanisms that keep their changes from destroying each other; Managed RAP Part 2 shows the actual implementation for the Purchase Request.

There are really three distinct problems, each with its own mechanism. Two transactions changing the same instance at the same moment calls for a lock. A user changing data based on an outdated screen calls for an ETag. A draft being edited for a long time while the active data might change underneath it calls for a total ETag together with the draft's own exclusive lock.

Locks are pessimistic. They're declared with "lock master" on the root and "lock dependent by _Parent" on children. Before an instance is changed, RAP sets an exclusive lock on the root key, so changing even a single item locks the whole BO.

A second transaction trying to change the same BO gets an error naming who holds the lock, and locks are released at the end of the transaction — without draft, that's typically a matter of seconds.

In managed RAP the framework handles the locking itself; in unmanaged RAP, the lock method has to be implemented with a lock object, as covered in Unmanaged RAP Part 1.

ETags are optimistic. They're declared with "etag master <field>", for example LocalLastChangedAt, on each entity that can be changed on its own.

The consumer receives the current ETag value with every read, via the OData ETag header, and when it sends a change, it includes that value via If-Match; if the stored value has since changed, RAP rejects the update, OData returns HTTP 412, and the user has to reload.

The ETag field has to change on every change — managed RAP does this automatically for fields annotated with @Semantics.systemDateTime.localInstanceLastChangedAt, while in unmanaged RAP, it has to be set by hand on every update, with the read method returning the buffered value.

Children can carry their own etag master, so changing one item doesn't invalidate the others, or they can use "etag dependent by _Parent" instead.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.