gpt4 book ai didi

node.js - 不需要的多个 MongoDB 连接

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:59 26 4
gpt4 key购买 nike

我在与 MongoDB 的多个连接时遇到问题。

我正在使用 Node.js 和 Mongoose 连接到 MongoDB。

我的简单网页只是连接,执行一些查询,然后关闭连接。

但是当我重定向到另一个页面时,连接会打开两次。

如果我重新加载/重定向页面,则会打开另一个连接。

我通过连接到数据库

mongoose.connection.on('connected', function () {
console.log('Connected to mongo server.');

});
mongoose.connection.on('error', function (err) {
console.log('Could not connect to mongo server!');
});


mongoose.connect(mongoUrl);

并使用

断开连接
 mongoose.connection.close(function(){
console.log('connection closed');
});

日志显示,该连接已关闭。但是如果我刷新页面 4x,我会在日志中看到

Connected to mongo server.
Connected to mongo server.
Connected to mongo server.
Connected to mongo server.

我错过了什么吗?

在 MongoDB 日志中是这样的(我确信连接函数只被调用一次)

Wed Feb 19 18:56:54.780 [initandlisten] connection accepted from 127.0.0.1:59777 #261 (1 connection now open)
Wed Feb 19 18:56:54.783 [initandlisten] connection accepted from 127.0.0.1:59778 #262 (2 connections now open)
Wed Feb 19 18:56:54.784 [initandlisten] connection accepted from 127.0.0.1:59779 #263 (3 connections now open)
Wed Feb 19 18:56:54.787 [initandlisten] connection accepted from 127.0.0.1:59780 #264 (4 connections now open)
Wed Feb 19 18:56:54.788 [initandlisten] connection accepted from 127.0.0.1:59781 #265 (5 connections now open)
Wed Feb 19 18:56:54.839 [conn261] end connection 127.0.0.1:59777 (4 connections now open)
Wed Feb 19 18:56:54.839 [conn262] end connection 127.0.0.1:59778 (3 connections now open)
Wed Feb 19 18:56:54.840 [conn263] end connection 127.0.0.1:59779 (2 connections now open)
Wed Feb 19 18:56:54.840 [conn264] end connection 127.0.0.1:59780 (2 connections now open)
Wed Feb 19 18:56:54.840 [conn265] end connection 127.0.0.1:59781 (1 connection now open)

完整代码在github上:https://github.com/kraag22/graphs/blob/master/app/mongo.js

最佳答案

我在 github 上简单浏览了一下你的代码

I'm sure that connection function is called only once

是的,每个请求一次。在您的代码中,您故意为每个请求创建一个连接,所以我不明白为什么您对这一事实感到惊讶

But when I redirect to another page, connection is opened twice. If I reload/redirect page, another connection is open.

多个请求会创建多个连接,对吗?也许您真正的问题是“为什么我在日志中看不到类似的内容?”

Connected to mongo server.
Connection closed.
Connected to mongo server.
Connection closed.
Connected to mongo server.
Connection closed.
...

第一个答案(治标不治本):mongoose.connect 创建一个单例连接,如果您确实想为每个请求创建一个连接,则必须使用 mongoose.createConnection参见http://mongoosejs.com/docs/connections.html

Multiple connections

So far we've seen how to connect to MongoDB using Mongoose's default connection. At times we may need multiple connections open to Mongo, each with different read/write settings, or maybe just to different databases for example. In these cases we can utilize mongoose.createConnection() which accepts all the arguments already discussed and returns a fresh connection for you.

第二个答案(治愈疾病):我发现您使用 NodeJS 的方式存在概念错误。你不应该为每个请求创建一个连接,NodeJS 不像 PHP 那样面向进程(每个请求一个进程,每个请求一个数据库连接),NodeJS 可以处理同一进程中的多个并发请求,共享与 MongoDB 连接相同的资源

您确实应该在服务器启动时创建一个 MongoDB 连接,并让所有请求共享同一个连接。 Here一个文件中的简单示例

由于您实际上使用的是除 AngularJS 之外的 MEAN 堆栈(MongoDB、ExpressJS、AngularJS、NodeJS),因此您应该查看 http://www.mean.io

关于node.js - 不需要的多个 MongoDB 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21888408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com