I have an express endpoint that updates a user entity - just putting some data into its document.
The update is done using transaction with a session.
It works - if I abort, the user entity remains without any change - and if I commit the change applies.
However there some weird issues with that. Because if I do the same operation, or just any other HTTP requests to the backend api - even requests that sent to another endpoints - over and over again, then I try the same operation of updating the same user entity, and I got this error:
unhandledRejection MongoServerError: Transaction with { txnNumber: 4 } has been committed.
I have read the docs carefully and there is not a single place stating how to work with the sessions.
-
Should I create one session for the whole mongo client to be reused for every transaction, and just when gracefully shutting down the node process, should I also end the session? (just before doing
client.close())? -
Should I create a new session for every transaction (on the same mongo client), and just after either commit or abort to also end the session?
I have tried both to solve the issue I have with these but both of them yield the same results.
Also, a general question: should I interact with the database 100% with transactions? is it a good or bad practice?