-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Closed
Milestone
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.7.0
Node.js version
16.20.2
MongoDB server version
7.0
Typescript version (if applicable)
No response
Description
When trying to connect to a database using loadBalanced=true in the connection string, mongoose drops the connection after some time but never reconnects.
This happens in all versions >=8.7.0
8.6.4 doesn't have this issue and it reconnect successfully after the delay.
Steps to Reproduce
Providing sample code in which we manually add a delay so the connection drops and we never reconnect to MongoDB.
const mongoose = require("mongoose")
const { ObjectId } = require("bson")
// some connection string using `loadBalanced=true`
const connectionString = ""
const delay = ms => new Promise(res => setTimeout(res, ms));
async function execTest() {
mongoose.connection.on('connected', () => console.log('connected'));
mongoose.connection.on('open', () => console.log('open'));
mongoose.connection.on('disconnected', () => console.log('disconnected'));
mongoose.connection.on('reconnected', () => console.log('reconnected'));
mongoose.connection.on('disconnecting', () => console.log('disconnecting'));
mongoose.connection.on('close', () => console.log('close'));
mongoose.connection.on('error', () => console.log('close'));
await mongoose.connect(connectionString, { maxPoolSize: 100 })
console.log("MongoDB Connection State:", mongoose.connection.readyState)
const drawingSchema = mongoose.Schema({
name: { type: String, required: true },
prop: { type: String, required: true },
});
const someSchema = mongoose.model("someSchema", someSchema);
await someSchema.insertMany([
{ name: "Name1", prop: "abc" },
{ name: "Name2", prop: "def" },
{ name: "Name3", prop: "ghi" },
{ name: "Name4", prop: "jkl" },
{ name: "Name5", prop: "mno" },
])
await delay(20000)
console.log("MongoDB Connection State:", mongoose.connection.readyState) // 0
console.log("start session")
const session = await mongoose.startSession(); // it hangs here, since it never reconnects
console.log("start transaction")
session.startTransaction();
console.log("after start transaction")
const x = await someSchema.findOne({ name: "Drawing1" }).lean()
session.commitTransaction()
session.endSession()
console.log(x)
console.log("after update")
return
}
execTest()
Setting heartbeatFrequencyMS to a value higher that the delay makes the issue go away but I'm not sure if this would be a recommended workaround.
Expected Behavior
Mongoose should reconnect to the MongoDB database and the query should work
Metadata
Metadata
Assignees
Labels
No labels