
Concurrency control prevents concurrent and interfering data access of different users. It ensures that data can only be changed if data consistency is assured.
RESTful applications are designed to be usable by multiple users in parallel. If more than one user has transactional database access, you must make sure that every user only executes changes based on the current state of the data so the data stays consistent. In addition, you must make sure that sure that users do not change the same data at the same time.
There are two approaches to regulate concurrent writing access to data. Both of them must be used in the ABAP RESTful Application Programming Model to ensure consistent data changes.
Pessimistic Concurrency Control
Pessimistic concurrency control prevents simultaneous modification access to data on the database by more than one user.
Pessimistic concurrency control is done by exclusively locking data sets for the time that a modification request is executed. The data set that is being modified by one user cannot be changed by another user at the same time by using a global lock table.
The lifetime of such an exclusive lock is tied to the session life cycle. The lock expires once the lock is actively removed after the successful transaction or with the timeout of the ABAP session.
In managed scenarios, the business object framework assumes all locking tasks. You do not have to implement the locking mechanism in that case. If you do not want the standard locking mechanism by the managed business object framework, you can create an unmanaged lock in the managed scenario. In unmanaged scenarios, the application developer has to implement the method for lock and implement the locking mechanism. It is covered later in this course.
Optimistic Concurrency Control
Optimistic concurrency control enables transactional access to data by multiple users at the same time, while avoiding inconsistencies and unintentional changes of already modified data.
The approach of optimistically controlling data relies on the concept that every change on a data set is logged by a specified field, called the ETag field. Often, the ETag field contains a timestamp, a hash value, or any other versioning that precisely identifies the version of the data set.
Concurrency control based on ETags does not lock the data set. Instead, it refuses data changes if the ETag value on the database changed since the client last read the data set. This mechanism is independent from ABAP sessions and data sets are not blocked to be used by other clients. It is important for RESTful protocols like OData, because such protocols are always stateless and each request is handled in a different session.



