我需要手动关闭一个mongoose连接吗?

时间:2023-02-06 02:33:03

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

新到节点,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 save method on them.

我有一个节点应用程序,它打开一个文件,将行解析为记录,并将记录保存到mongodb中。这些记录是Mongoose模型对象,为了将它们保存到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)?

现在我都在担心mongoose管理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 save on all my objects? Otherwise given the async nature of the save, it would be difficult to know exactly when shutdown the connection.

似乎mongoose不仅打开了连接,还阻止了我的脚本终止。在调用了所有对象之后,我可以安全地关闭mongoose连接吗?否则,考虑到save的async特性,很难确切地知道何时关闭连接。

3 个解决方案

#1


14  

You do need to call mongoose.disconnect() to close the connection, but you also need to wait until all save calls 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 async to do something a bit more elegant.

因此,您可以简单地计算仍有多少未完成的工作要跟踪,也可以使用像async这样的流控制框架来做一些更优雅的工作。

#2


6  

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

当发生节点POSIX信号时,应该关闭mongoose连接。当在终端上按下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.

这是信号信号断开的代码。

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

#3


2  

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

#1


14  

You do need to call mongoose.disconnect() to close the connection, but you also need to wait until all save calls 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 async to do something a bit more elegant.

因此,您可以简单地计算仍有多少未完成的工作要跟踪,也可以使用像async这样的流控制框架来做一些更优雅的工作。

#2


6  

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

当发生节点POSIX信号时,应该关闭mongoose连接。当在终端上按下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.

这是信号信号断开的代码。

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

#3


2  

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