Hi, I created some tests to better understand transactions.
This is one of the tests, and what I did:
Start Session, Start Transaction
Find document id:1 with a random integer. Logs it.
Another process outside the transaction find the same document and modify the integer
Repeat step 2.
I expected to log number X, modified to number Y and after that on the second find to see the Y again.
But the transaction is acting as a snapshot when I`m clearly setting readConcern to local.
I there anything else that I’m missing here? Shouldn’t the last find read the latest document state?
As i recall, Mongodb document doesn’t say much about transaction isolation (e.g. unlike the detailed info in sql databases).
In SQL databases, every single statement is a transaction implicitly. I’m guessing this is also true for a “simple operation” in nosql world. (e.g. updateOne).
I believe I found a reason, but if anyone with more experience knows if this is correct, please chime in.
I had some misconceptions about how transactions work:
It seems that readConcern is applied to the transaction as whole, as if everything is one big operation. So the readConcern local will not read local (new data) on each find statement, it will read local at the moment that I call session.startTransaction.
From this moment on, the transaction will read a snapshot of the data. Which has nothing to do with readConcern:snapshot - The naming here is very confusing.
So, if the transaction readTimestamp is set to something data returned until that point in time, if not set or set to zero: data returned is at the point in time of the the transaction start.
Now this is my operation with readConcern:local:
This is with readConcern:snapshot
This is with readConcern:majority
Difference between snapshot and majority when using transactions is basically shard snapshot consistency with the former. (something like shardedSafeMajority)
I believe that’s it.
If anyone has more experience with this topic, don’t be shy.
Cheers,