I'm trying to get "connection lost" or something similar when connection lost between nodejs and mongodb server. I use native driver and has following code
var mongo = require('mongodb');
var server = new mongo.Server('host', 'port', {
auto_reconnect: true,
socketOptions: {
keepAlive: 10,
connectTimeoutMS: 1000,
socketTimeoutMS: 0
}
});
var db = new mongo.Db(
'dbname',
server,
{
w: 1,
wtimeout: 1000,
numberOfRetries: 100,
auto_reconnect: true
}
);
db.on('close', function () {
console.log('Error...close');
});
db.on('error', function (err) {
console.log('Error...error', err);
});
db.on('disconnect', function (err) {
console.log('Error...disconnect', err);
});
db.on('disconnected', function (err) {
console.log('Error...disconnected', err);
});
db.on('parseError', function (err) {
console.log('Error...parse', err);
});
db.on('timeout', function (err) {
console.log('Error...timeout', err);
});
db.collection('collectionName',function(err, collection){
if(err){
console.log('Error...collection', err);
return;
}
// set breakpoint here and break connection to mongo db server
collection.insert({}, function (err, data) {
if (err) {
console.log('Error...insert', err);
}
console.log('Fine!');
});
});
No timeout or error apear around 20 minutes and insert is freezed. After that I got "Error...insert" with connection lost error.
I tried to set socketTimeoutMS = 10000 and keepAlive = 1 for example, but socketTimeoutMS rise "timeout" event constantly after 10000 and doesn't take into account keepAlive settings or even queries to mongodb.
Also wtimeout works only if we have connection to mongodb server and has a longtime query. If connection is lost it doesn't works.
So how can I get event or err when I lost conneciton? Or reduce 20 minute query freeze?
These are some of the solutions: Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.
To connect a Node. js application to MongoDB, we have to use a library called Mongoose. mongoose. connect("mongodb://localhost:27017/collectionName", { useNewUrlParser: true, useUnifiedTopology: true });
If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.
If have the mongodb server process running, run this example.
After, if you stop the server process, you'll see Error...close
being displayed.
All possible events for the "on" function can be found here
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect('mongodb://localhost:27017/exampleDb', function(err, db) {
if(err) {
return console.dir(err);
}
console.log('We are connected');
// db.close();
db.on('close', function () {
console.log('Error...close');
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With