locked
How can I implement optimistic locking for a REST API? RRS feed

  • Question

  • User-2031132340 posted

    I have a client application that interacts with the backend database via an ASP.NET Web API.   (2.0) and in the web api all the controllers make changes to the database  using Entity Framework 6

    Currently if two clients are launched and both make the same additions to one of the tables, duplicate rows occur.  This is only one of the issues.

    So I want to implement optimistic locking to reject one of the updates and was looking for guidance on how to do so. 

    I found this link which describes using [ConcurrencyCheck] attribute (except I'm not using EF Core).

    https://dejanstojanovic.net/aspnet/2018/november/handling-data-concurrency-in-ef-core-and-aspnet-core-webapi

    Is this the correct way?  Are there any other recommendation or best practices I should follow ((specifically for the interaction with Web API and Entity Framework)? 

    NOTE:  My entities look like this

           Document Table ---  Keywords Table 

    (Where Document Id is a primary Key for Document Table and Foreign Key in Keywords Table.)

    User can highlight keywords in the client app and they get recorded in the Keywords table with start and end character positions.

    So when user A and B both GET document to start highlighting in the app, currently they are able to both add the same keywords to the Keywords table.  So update to a single row in the Keywords table isn't too much of a concern, Its more about the change to the set of keywords as a whole that belong to  a particular Document Id.

    Wednesday, June 24, 2020 4:23 PM

All replies