处理失去与nodejs的mongo db的连接 [英] Handle lost connection to mongo db from nodejs

查看:334
本文介绍了处理失去与nodejs的mongo db的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试获取连接丢失或类似的情况下,当nodejs和mongodb服务器之间的连接丢失。
我使用原生驱动程序并具有以下代码

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!');
    });
});

没有超时或错误大约20分钟,插入冻结。

No timeout or error apear around 20 minutes and insert is freezed. After that I got "Error...insert" with connection lost error.

例如,我试图设置socketTimeoutMS = 10000和keepAlive = 1,但是socketTimeoutMS上升timeout 事件不断地10000之后,并没有考虑到keepAlive设置,甚至查询到mongodb。

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.

此外,wtimeout工作只有当我们有连接到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?

推荐答案

如果有mongodb服务器进程运行,运行这个例子。
之后,如果停止服务器进程,将会显示错误...关闭
on函数的所有可能的事件可以是在这里找到

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');
  });
});

这篇关于处理失去与nodejs的mongo db的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆