Concurrent Transactions

Can anyone elaborate on this topic?

Let me give an example to make it more simple.(I’m using node.js, Mongo DB and Mongoose with transactions)

  1. I make a query to the inventory collection to check if I have sufficient stock
  2. If stock is sufficient then the order will be placed
    So my question is, can some other user manipulate the inventory while a transaction is ongoing?

Yes, another user can manipulate the inventory while the read transaction is happening.

In that scenario, the transaction will succeed because it’s reading the value that existed before someone else wrote to that document.

If you want to take a lock on that document while doing the transaction, you must do a write to that document inside of that transaction.

2 Likes

Hi Garaudy,

I am facing the same problem. I didn’t understood your solution. do you have any example of it?

Thanks

@Garaudy_Etienne Thank you for your response that was very helpful. Actually I cant really do write operations before querying the right documents first. I guess in that case I am left with no other option then to have a boolean flag for inventory documents and then when the transaction starts, do a write operations and turn the flag to true for all the possible required inventory documents before querying the right inventory documents. Or is there a better solution?

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.