Connection.transaction: Reset Document#isNew
if necessary when transaction is aborted
#8852
Labels
new feature
This change adds new functionality, like a new method or class
Milestone
Do you want to request a feature or report a bug?
Question.
What is the current behavior?
I'm having a bit of hard time understanding the behaviour of the documents
save
method during transactions retries. As an transaction wrapper, I'm using mongoose-tx, only I had to modify it a bit by adding transaction abort if the database returned aTransientTransactionError
:So the transaction itself is very simple:
And it works fine when the transaction does not get errors, however, if I get a
TransientTransactionError
due to a `WriteConflictz then things get weird.So for the above example,
doc2
is the one throwing the error and then two cases occur:If
doc1
is a new document, then on the first transaction retry, it would throw aDocumentNotFound
error and checking theisNew
flag, I noticed that it was set tofalse
even though the first transaction attempt failed (yes,await session.abortTransaction()
was called.).If
doc2
is not a new document, then on retry, it saves the document without errors, however due to the transaction error, it reverts any changes that were applied after retrieving it from the database.What is the expected behavior?
I expected that
abortTransaction
would not revert the changes and for new documents it would reset theisNew
flag. I don't know if this is the intended behaviour, but if it is, how do you properly handle retries withsave
?At the moment, as a workaround, I made a simple wrapper which either calls
Model.create
orModel.updateOne
with modified paths instead of usingsave
.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose: 5.9.10
NodeJS: 12.15.0
MongoDB: 4.2.2
The text was updated successfully, but these errors were encountered: