We are facing mongo force connection as the system is idle for more time
we are using node js version 16 and Mongo version 7.3.1
Welcome to the MongoDB community!
Could you provide some more details about the issues you are seeing, such as:
- What specific error or behavior are you seeing that indicates the connections are timing out?
- How long your system is being kept idle, could you quantify?
- How are you creating and managing the MongoDB connections in your Node.js application?
- Are the connection timeouts intermittent or happening consistently?
- Does increasing the connection pool size or using maxIdleTimeMS seem to help at all?
- What version of the MongoDB node.js driver are you using?
These additional details will help the community better understand the issue and assist you better. Looking forward to your response.
Regards,
Kushagra
1.What specific error or behavior are you seeing that indicates the connections are timing out?
Ans) It does not trigger any mongoose.connection.on(‘error’) or mongoose.connection.on(‘disconnected’) we are getting an error in then Block.
try {
let readDtaFromMongo = await getDataFromMongo();
} catch (error) {
// Triggred this catch block
console.log(error)
}
getDataFromMongo(){
const myCollection = mongoose.connection.collection('collectionName');
let res = await myCollection.aggregate([
{
$lookup: {
from: 'collection2',
localField: 'feild1',
foreignField: 'feild1',
as: "resultSet"
}
},
{
$unwind: "$resultSet"
}
]).toArray();
}
2.How long your system is being kept idle, could you quantify?
Ans) We are not sure when the error is getting triggered.
3.How are you creating and managing the MongoDB connections in your Node.js application?
// Connection creation
mongoose.connect(mongoUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
logger.info('MongoDB connected successfully');
resolve(mongoose.connection);
}).catch((err) => {
});
// Saving Data
await mongoose.connection.collection('collectionName').insertMany(data);
// Retrieving data declared this function in the above code snippet
getDataFromMongo();
-
Are the connection timeouts intermittent or happening consistently?
Ans) Yes, continuously we are facing the mongo force connection issue. -
Does increasing the connection pool size or using maxIdleTimeMS seem to help at all?
Ans) Not sure how it can help with this problem -
What version of the MongoDB node.js driver are you using
Ans) Currently, we are using the mongo atlas
Any update here?
We are waiting for response
Thank you for providing the details. Could you please share the error logs you are encountering? Additionally, could you provide more information about how long your system remains in an idle state? Also, kindly share the MongoDB Atlas tier you are currently using.
Regards,
Kushagra
Hi Kushagra_Kesav,
Thanks for the responding
Please find the screenshot below for error logs
Reading mongo
we are using M0 Sandbox (Shared RAM, 512 MB Storage)
Even though we are facing an error on local Mongo we have faced this issue on db version v6.0.8
Hi Team,
I hope you’re doing well.
We are waiting for the response. Thanks in advance.
Hi Team,
I hope you’re doing well.
We are waiting for the response. Thanks in advance.
Apologies for the delayed response!
I suspect the problem occurs when pooled connections return a connection closed
error due to long-running applications, possibly triggered by the “insertMany
” operation in the application code. As per the mongoose documentation, one workaround is to add keepAlive
to the options object passed into the “connect
” function, which may help prevent this error.
Also, could you help me understand the nature of the ‘lehman-caves-core-server
’ node_module? It appears to be invoking the ‘deviceStatus()
’ function from the ‘deviceMonitoring.js
’ file. To me, this seems to be associated with the application code for monitoring device status.
Please provide more insights into your codebase so the community can assist you better.
Best regards,
Kushagra
Hey @Kushagra_Kesav
Thanks for responding.
deviceMonitoring.js This is my control where the functions are declared and lehman-caves-core-server
this is my custom module
For keepAlive, we are using the 7.3.1 Mongoose module
As per the documentation
Before Mongoose 5.2.0, you needed to enable the keepAlive
option to initiate TCP keepalive to prevent " connection closed"
errors. However, keepAlive
has been true
by default since Mongoose 5.2.0, and the keepAlive
is deprecated as of Mongoose 7.2.0. Please remove the keepAlive
and keepAliveInitialDelay
options from your Mongoose connections.
It is enabled by default
Hey @Kushagra_Kesav
I hope you’re doing well.
I am still looking for a solution can you please help me with this?
I suspect that a forceful closure of the connection may be occurring, either from the application side or due to the server shutting down. This results in the error you are encountering in this particular scenario. See reference
Please check your application code and ensure that your MongoDB server is operational.
Best regards,
Kushagra