Answer the question
In order to leave comments, you need to log in
Spring Mongo: how to organize a parallel change of an entity?
Hello! Newbie in java.
I use Spring + Spring Mongo. There is an essence. Requests to change it come from clients. Multiple requests may arrive at the same time. Now, when requesting, I select an entity from the database, change it, save it. But, it is clear that with simultaneous requests, I can wipe out some of the changes.
How to avoid it? How are such problems solved?
Thanks in advance!
Answer the question
In order to leave comments, you need to log in
The easiest way is to use syncronized. In general, Monga uses a lock on the document internally during change operations, so you should not bother.
If the problem does not manifest itself, or it rarely appears, and the cost of such a problem is small, then there is no point in solving it. Otherwise, you can at least try using optimistic locks , and ideally consider using Event Sourcing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question