There are two sets, A and B, to carry out the following transactions:
1. firstly, query a set to obtain a document which is converted into java object
2. Perform the save operation of MongoTemplate after modifying the object with java code in memory
3. Finally, update again in the B set.
The confusion in the above process is that two threads t1 and t2 finish step 1 of the transaction at the same time. After making the change, t1 first writes the lock to change the document, then the data obtained by t2 is dirty data. At this time, t1 releases the lock, t2 executes save, and there is a problem with the data in the database. How to deal with this situation?
At present, the only method I can think of is java’s synchronized locking of the entire method, but in other methods, the same dirty data problem will still occur for queries on set a, so I would like to ask you for any good suggestions. I am an Android developer, and I have just started the server. please explain the trouble scheme simply. thank you
Like other databases, use the update method to update only the fields you want to update, not all fields. UseupdateFirstOrupdateMulti. Save rudely updates all fields in this way, which not only causes the problem you mentioned, but also causes more fields to be updated than necessary each time, resulting in unnecessary server-side pressure and oplog pressure.