Hi!
I have a stateless EJB for saving documents to couchbase. For each save this EJB makes an update to a field of another document. But if the API receives many concurrent requests the document wont have the correct values at the end of requests. Because all EJBs will execute at the same time the first line of code below and they get the same document to update, instead of wait one another to finish its job and get the correct document to update.
An example of code:
- Document docToUpdate = bucket.get(docKey); --> document to be updated in every save
- bucket.insert(docToSave);
- // some code to update fields of docToUpdate (for example counters, strings, etc)
- bucket.update(docToUpdate);
What is the best practice to avoid this, using couchbase?