node.js 我需要手动关闭猫鼬连接吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19371821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:59:22  来源:igfitidea点击:

Do I need to manually close a mongoose connection?

javascriptnode.jsmongodbmongoose

提问by hba

New to Node, Mongoose & Mongodb - haven't read the source code...

Node、Mongoose 和 Mongodb 的新手 - 还没有阅读源代码......

I have a Node application which opens a file, parses the lines into records and saves the records to mongodb. The records are Mongoose model objects, and to save them to mongodb all I do is invoke the savemethod on them.

我有一个 Node 应用程序,它打开一个文件,将行解析为记录并将记录保存到 mongodb。记录是猫鼬模型对象,为了将它们保存到 mongodb,我所做的就是调用save它们的方法。

So now I'm all worried about the connection that mongoose is managing db = mongoose.connect(url). Do I need to manually close it? If so, when should I close it (since everything is happening async it is hard to say when to close the connection)?

所以现在我都很担心猫鼬正在管理的连接db = mongoose.connect(url)。我需要手动关闭它吗?如果是这样,我应该什么时候关闭它(因为一切都是异步发生的,所以很难说什么时候关闭连接)?

It seems that mongoose doesn't only keep the connection open, but also it keeps my script from terminating. Can I safely close the mongoose connection after I've called saveon all my objects? Otherwise given the async nature of the save, it would be difficult to know exactly when shutdown the connection.

似乎猫鼬不仅保持连接打开,而且还防止我的脚本终止。调用完save所有对象后,我可以安全地关闭猫鼬连接吗?否则,鉴于保存的异步性质,很难确切知道何时关闭连接。

采纳答案by JohnnyHK

You do need to call mongoose.disconnect()to close the connection, but you also need to wait until all savecalls have completed their async work (i.e. called their callback) before doing that.

您确实需要调用mongoose.disconnect()来关闭连接,但您还需要等到所有save调用都完成了它们的异步工作(即调用它们的回调)之后再这样做。

So either keep a simple count of how many are still outstanding to keep track or use a flow control framework like asyncto do something a bit more elegant.

因此,要么简单地计算有多少还没有完成,要么使用流控制框架async来做一些更优雅的事情。

回答by Endre Simo

You should close a mongoose connection when a Node POSIX signal is happening. SIGINTprocess is triggered when Ctrl-C has been pressed on terminal or a server shutdown.

当 Node POSIX 信号发生时,您应该关闭猫鼬连接。在终端或服务器关闭时按下 Ctrl-C 会触发SIGINT进程。

Another possible scenario is to close a connection when a data streaming is done. Anyway is more recommended to connect on startup and disconnect on shutdown.

另一种可能的情况是在数据流完成后关闭连接。无论如何,更建议在启动时连接并在关闭时断开连接。

This is the code for disconnection on a SIGINT signal.

这是在 SIGINT 信号上断开连接的代码。

// If the Node process ends, close the Mongoose connection
process.on('SIGINT', function() {
  mongoose.connection.close(function () {
    console.log('Mongoose disconnected on app termination');
    process.exit(0);
  });
});

回答by phani

What JohnnyHK said is correct. Add "SIGTERM" as well.

JohnnyHK 说的是正确的。添加“SIGTERM”。

Simple example to use connection.close()

使用 connection.close() 的简单示例

https://gist.github.com/pasupulaphani/9463004#file-mongoose_connet-js

https://gist.github.com/pasupulaphani/9463004#file-mongoose_connet-js