简体   繁体   English

MongoDB连接在Node.js测试中未关闭

[英]Mongodb connections not closing in nodejs tests

So I have a suite of cucumber tests and in the BeforeFeatures section I create connections to 2 databases on the same server, I have set the pooling to 2 so I can see when I connect 4 connections occur (2 databases with 2 pools). 因此,我有一套黄瓜测试,在“ BeforeFeatures部分中,我在同一服务器上创建了2个数据库的连接,将池设置为2,这样我就能看到何时连接4个连接(2个数据库和2个池)。

Then at the end I close the databases like so: 然后最后,我像这样关闭数据库:

var closeDatabase = function(callback) {
    db1.close(function(){
        db2.close(function(){
            console.log("DONE");
            callback();
        });
    });
};

Now this is invoked and I see the console output and the process closes so both database connections should be closed. 现在调用它,我看到控制台输出,并且进程关闭,因此应该关闭两个数据库连接。 However if I look at the mongodb logs: 但是,如果我查看mongodb日志:

2016-01-14T11:45:05.813+0000 [initandlisten] MongoDB starting : pid=888 port=27017 dbpath=C:\Tools\mongodb\data 64-bit host=SomePC
2016-01-14T11:45:05.818+0000 [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2016-01-14T11:45:05.821+0000 [initandlisten] db version v2.6.7
2016-01-14T11:45:05.822+0000 [initandlisten] git version: a7d57ad27c382de82e9cb93bf983a80fd9ac9899
2016-01-14T11:45:05.826+0000 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2016-01-14T11:45:05.829+0000 [initandlisten] allocator: system
2016-01-14T11:45:05.830+0000 [initandlisten] options: { storage: { dbPath: "C:\Tools\mongodb\data" } }
2016-01-14T11:45:05.846+0000 [initandlisten] journal dir=C:\Tools\mongodb\data\journal
2016-01-14T11:45:05.848+0000 [initandlisten] recover : no journal files present, no recovery needed
2016-01-14T11:45:05.871+0000 [initandlisten] waiting for connections on port 27017
2016-01-14T11:45:11.639+0000 [initandlisten] connection accepted from 127.0.0.1:51841 #1 (1 connection now open)
2016-01-14T11:45:11.646+0000 [conn1] end connection 127.0.0.1:51841 (0 connections now open)
2016-01-14T11:45:11.650+0000 [initandlisten] connection accepted from 127.0.0.1:51842 #2 (1 connection now open)
2016-01-14T11:45:11.653+0000 [initandlisten] connection accepted from 127.0.0.1:51843 #3 (2 connections now open)
2016-01-14T11:45:11.659+0000 [initandlisten] connection accepted from 127.0.0.1:51844 #4 (3 connections now open)
2016-01-14T11:45:11.662+0000 [conn4] end connection 127.0.0.1:51844 (2 connections now open)
2016-01-14T11:45:11.665+0000 [initandlisten] connection accepted from 127.0.0.1:51845 #5 (3 connections now open)
2016-01-14T11:45:11.669+0000 [initandlisten] connection accepted from 127.0.0.1:51846 #6 (4 connections now open)
2016-01-14T11:45:11.689+0000 [conn5] end connection 127.0.0.1:51845 (3 connections now open)
2016-01-14T11:45:11.689+0000 [conn6] end connection 127.0.0.1:51846 (3 connections now open)
2016-01-14T11:45:11.690+0000 [conn2] end connection 127.0.0.1:51842 (3 connections now open)
2016-01-14T11:45:11.690+0000 [conn3] end connection 127.0.0.1:51843 (3 connections now open)

So at the end although the 2 connections have ended it seems to leave 3 of the 4 connections active. 因此,最后虽然2个连接已结束,但似乎使4个连接中的3个处于活动状态。 This being said if we look at ports it seems to activate 6 ports even though it only has 4 connections. 这就是说,如果我们查看端口,即使它只有4个连接,它似乎也可以激活6个端口。

So is this common? 那么这很常见吗? should I be worried or am I doing anything wrong? 我应该担心还是做错什么? The whole reason this is an issue is because when I try to use the connections I get the error: 这是一个问题的全部原因是因为当我尝试使用连接时出现错误:

MongoError: server localhost:27017 sockets closed

So I am a bit baffled as to why its seemingly over-allocating and bombing out when I attempt to use the connections. 因此,当我尝试使用这些连接时,为什么它似乎过度分配并被炸毁,对此我有些困惑。

So the error is correct, it was closing the connections then using them, due to they way console outputs happen on windows they are not always 100% in the right order. 因此错误是正确的,因为先关闭连接再使用连接,这是因为它们在Windows上发生控制台输出的方式并不总是以正确的顺序100%发生。 It turned out cucumber was bombing out silently in an area and was having a knock on effect here. 事实证明,黄瓜正在一个地区默默轰炸,并在这里起到了作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM