2016-01-30 87 views
0

我使用mongo驅動程序2.2.2從我的rails應用程序連接。 我注意到我有太多打開的連接。 這是我的測試:正確的方法來關閉連接到mongodb從軌道

@db = Mongo::Client.new('mongodb://user:[email protected]:27017/db') 
# nothing to do! only for test! 
@db.close 

和我同時啓動 - Rails應用程序,並在此之後我是創建單個請求我在調試模式下的應用程序和檢查的mongod輸出的MongoDB

$ rails server 
$ mongod 

2016-01-30T14:08:30.047+0200 [initandlisten] connection accepted from  127.0.0.1:65005 #1 (1 connection now open) 
2016-01-30T14:08:30.048+0200 [conn1] end connection 127.0.0.1:65005 (0 connections now open) 
2016-01-30T14:08:30.048+0200 [initandlisten] connection accepted from 127.0.0.1:65006 #2 (1 connection now open) 
2016-01-30T14:08:30.051+0200 [initandlisten] connection accepted from 127.0.0.1:65007 #3 (2 connections now open) 
2016-01-30T14:08:30.051+0200 [conn3] end connection 127.0.0.1:65007 (1 connection now open) 
2016-01-30T14:08:30.051+0200 [initandlisten] connection accepted from 127.0.0.1:65008 #4 (2 connections now open) 
2016-01-30T14:08:30.052+0200 [conn4] end connection 127.0.0.1:65008 (1 connection now open) 

在我的軌輸出I見下:

D, [2016-01-30T14:08:30.046532 #12981] DEBUG -- : MONGODB | Adding localhost:27017 to the cluster. 
D, [2016-01-30T14:08:30.050656 #12981] DEBUG -- : MONGODB | Adding localhost:27017 to the cluster. 

我打算和關閉的mongod,並再次檢查導軌輸出:

D, [2016-01-30T14:08:50.060270 #12981] DEBUG -- : MONGODB | IOError 
D, [2016-01-30T14:09:00.065535 #12981] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:27017 
D, [2016-01-30T14:09:10.066730 #12981] DEBUG -- : MONGODB | Broken pipe 
D, [2016-01-30T14:09:20.073243 #12981] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:27017 
D, [2016-01-30T14:09:30.079056 #12981] DEBUG -- : MONGODB | Broken pipe 

最後我的問題:如何真正關閉連接到蒙戈? 我在做什麼錯?

回答