How do you implement coarse optimistic blocking in REST?

I implemented optimistic locking for REST resources that have a 1 to 1 mapping to the database tables, passing back the version number that was in the GET, back to the PUT call. If the version number changed in the database between the time I did GET and PUT, an optimistic blocking exception occurred. Pretty simple design.

Now, how do I do the same for composite REST resources that map to multiple database tables? I would not have to skip several version fields (one for each data table related to a composite resource). A simple example of a compound resource would be / FooBar, where / Foo and / Bar are non-composite resources.

Basically I am looking for an example of REST implementation of the Fowler Coarse Grained Locking pattern: http://martinfowler.com/eaaCatalog/coarseGrainedLock.html

+7
source share
1 answer

This is what the ETag header was designed for . A very common way to implement it is to create a payload on your answer, make a hash (it should not be safe, just with a low collision), and then use this hash as the ETag value. Note that this approach does not know how many sources are involved in creating the answer.

Then the client sends the received ETag to If-Match , which the server can use to check the freshness of the request.

+5
source

All Articles