MongoDB Transactions Write Conflict

I have a value called x in my database and two transactions, T1 & T2. Assume both transactions use the multi-document ACID API MongoDB provides.

Each transaction reads the value of x and decrements it by 1. When reading, the code checks that the value of x is greater than 0.

Now, the value of x is 1 and T1 & T2 are concurrent. Assume the following execution thread

T1 reads x = 1
T2 reads x = 1
T1 writes x = 0
T2 writes x = 0 

In this situation will T2 detect that the value of x that it read has been changed and abort the transaction (presumably based on some internal timestamp) or will it continue with the write?

I have tried looking through the MongoDB docs and I can’t find a satisfactory explanation to this.

Olá, bem-vindo à comunidade MongoDB. Acredito que este link pode ajudá-lo a entender

Thanks Samuel, that helped me figure out a way to replicate the issue locally and confirm that a WriteConflict does indeed occur.

1 Like

You’re welcome, if this solved your problem, leave the topic as resolved so that if other people have the same problem, look at this topic as a reference =D

I’ve marked your previous answer as the solution. Hope that’s enough!

1 Like

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