如何关闭与MongoDB服务器的所有连接 [英] How to close all connections to the MongoDB server

查看:271
本文介绍了如何关闭与MongoDB服务器的所有连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Node MongoDB本机驱动程序连接到MongoDB数据库:

I am using Node MongoDB native driver to connect to MongoDB database:

// Open a connection
db.open(function (err, db) {
  db.close();
});

// Open another connection
db.open(function (err, db) {
  db.close();
});

我看到mongodb.log文件中接受了两个连接,但是只有一个末端连接.而且程序没有退出,我认为它仍在等待关闭第二个连接.您如何关闭所有连接?

I saw two connection accepted in mongodb.log file, but only one end connection. And the program did not exit, I think it's still waiting for the second connection to be closed. How do you close all connections?

推荐答案

您正在使用回调参数隐藏变量db.

You are hiding your variable db with your callback parameter.

试试看.

// Open a connection
db.open(function (err, p_db) {
  db.close();
}); 

// Open another connection
db.open(function (err, p_db) {
  db.close();
});

更新:好的,我想我对回调中的参数感到困惑,对不起. 您的问题(从要点来看)是因为要在关闭两个db.opens之前使用同一个db对象执行它们.另外,除非您按顺序执行操作,否则您似乎需要单独的服务器对象.

Update: Ok, I guess I was confused about the parameters in the callback, sorry. Your problem (from the gist), is because you are executing two db.opens with the same db object before you're closing them. Also, it looks like you need separate server objects unless you do things in sequence.

因此,如果您想按顺序保证它,可以做的是(而且,我所拥有的mongodb版本似乎没有定义_serverState,因此我将其更改为connected):

So, what you could do, instead, if you wanted to guarantee it in sequence, is (also, the version of mongodb I have doesn't seem to have the _serverState defined, so I changed it to connected):

console.log('[1]', db.serverConfig.connected);
db.open(function (err, db) {
  console.log('[2]', err, db.serverConfig.connected);
  db.close();
  console.log('[3]', err, db.serverConfig.connected);

  console.log('[4]', db.serverConfig.connected);
  db.open(function (err, db) {
    console.log('[5]', err, db.serverConfig.connected);
    db.close();
    console.log('[6]', err, db.serverConfig.connected);
  });
});

哪些印刷品:

[1] false
[2] null true
[3] null false
[4] false
[5] null true
[6] null false

或者,您可以定义两个db变量-但您还需要定义两个服务器变量,否则它会在第一个服务器变量关闭后挂起.像这样:

Alternatively, you could define two db variables--but you also need to define two server variables, or it hangs after the first one closes. Like so:

var mongodb  = require('mongodb')
  , server   = new mongodb.Server('localhost', 27017, {})
  , server2   = new mongodb.Server('localhost', 27017, {})
  , db       = new mongodb.Db('test', server, {})
  , db2       = new mongodb.Db('test', server2, {})
  ;

console.log('[1]', db.serverConfig.connected);
db.open(function (err, db) {
  console.log('[2]', err, db.serverConfig.connected);
  db.close();
  console.log('[3]', err, db.serverConfig.connected);
});

console.log('[4]', db2.serverConfig.connected);
db2.open(function (err, db2) {
  console.log('[5]', err, db2.serverConfig.connected);
  db2.close();
  console.log('[6]', err, db2.serverConfig.connected);
});

哪些印刷品(对我来说,我想你不能指望1,4,2,3 ...):

Which prints (for me, and I guess you can't count on the order of 1,4,2,3...):

[1] false
[4] false
[2] null true
[3] null false
[5] null true
[6] null false

这篇关于如何关闭与MongoDB服务器的所有连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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