startSession() hangs
I am using mongoose to connect to my Mongodb Atlas cluster through my nodejs server.
There is a certain operation which is done as a transaction. I call mongoose.startSession() to start the transaction. Very infrequently, this mongoose.startSession() call hangs indefinitely.
I noticed that there had been a primary node failover in the cluster at the time at which the issue occurred, so this could be caused due to not correctly identifying the failover. I do not have capability to manually do a failover, so I am unable to try to reproduce this by doing failovers.
Following is the code I use to start session.
log.info('starting session');
const mongoSession = await mongoose.startSession();
log.info('session started');
In above code, starting session. gets logged, but session started doesn't get logged when issue occurs.
I connect to the db like below:
const dburl = 'mongodb+srv://myuser:[email protected]/mydb?retryWrites=true&w=majority';
mongoose.connect(dburl, {useNewUrlParser: true}, err => {
if (err) {
log.warn('Error occurred when connecting to database. ' + err);
}
});
Can this be an issue in Mongoose?
I'm currently facing the exact same issue.
Starting my app normally with 3 Replicas. Transactions works perfectly. I just shutdown on 1 Replica. Transactions doesn't work anymore, till I restart mongoose and starting the replica again.
@lahiruchandima @endbay what version of Mongoose are you using?
Newest but I found out, it was probably the MongoDB bug with useUnifiedTopology: true here:
https://jira.mongodb.org/browse/NODE-2231?jql=text%20~%20%22useUnifiedTopology%22
Since I've using old deprecated useUnifiedTopology, everything is working without issues anymore.
In my case, I was creating session the wrong way like you did.
mongoose.startSession()
Rather you should create the session with the mongo connection you created.
I have written a stackoverflow answer after I was able to solve my issue. Please refer if you are still stuck. https://stackoverflow.com/a/77698358/9148833