Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Handle Optimistic Concurrency in Cosmos DB Stored procedure by generalized way ?

I have Stored procedure which Simply updates the Documents based on Etag.

i have fetched 3 types of documents, updated some property of all 3 documents. I'm trying to update all 3 documents in stored procedure. but the 3rd document is updated by another user before the stored procedure updates the 3rd documents. so in this scenario, I do get a concurrency error. I can fetch the Document inside the Stored procedure but I'm not sure which property I have to update because I don't know which property is get modified by me or by other users inside Stored procedure bcoz it just replaces the Documents. the same stored procedure used everywhere the documents need to update so I can't hardcode the property in it.

How can I handle this?

in entity framework code first approach, we do have a DbUpdateConcurrencyException class given for SQL from which we can see which property get modify and take the Action Accordingly. is there an exception class similar to the DbUpdateConcurrencyException is provided in Cosmos DB?

I do have readed DocumentDB revisited Part 3 – Concurrency in DocumentDB & DocumentDB – Optimistic Concurrency in a Stored Procedure both the articles.which tell us how to Use Etag to Handle Optimistic Concurrency.

like image 738
Satyaray Singh Avatar asked Jul 24 '18 12:07

Satyaray Singh


1 Answers

You cannot identify what property has changed if your stored procedure only takes the new documents to store.

If your stored procedure takes both the existing document (e.g., the version the ETag is for) and the new document to store then you could do a diff to determine what properties have been updated. When an ETag check fails, re-fetch the latest document and update the properties as required using you in-memory diff.

like image 186
Joel Oughton Avatar answered Oct 31 '22 00:10

Joel Oughton