2012-12-06 88 views
2

當前使用node.js(Native MongoDB)驅動程序與Mongodb一起工作時,我知道可以設置池連接計數,但不管是否遵守相同的行爲。Mongo DB初始連接池保持打開狀態?

有問題的代碼:

(function init(){ 
    db.connect("mongodb://localhost/test",function(err, database){ 

    }); 
})(); 

這是日誌從服務器:

Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58663 #6 
(1 connection now open) 
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58664 #7 
(2 connections now open) 
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58665 #8 
(3 connections now open) 
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58666 #9 
(4 connections now open) 
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58667 #10 
(5 connections now open) 
Thu Dec 06 20:19:36 [conn9] end connection 127.0.0.1:58666 (4 connections now op 
en) 
Thu Dec 06 20:19:36 [conn10] end connection 127.0.0.1:58667 (4 connections now o 
pen) 
Thu Dec 06 20:19:36 [conn8] end connection 127.0.0.1:58665 (4 connections now op 
en) 
Thu Dec 06 20:19:36 [conn6] end connection 127.0.0.1:58663 (4 connections now op 
en) 
Thu Dec 06 20:19:36 [conn7] end connection 127.0.0.1:58664 (4 connections now op 
en) 

的連接顯然尚未關閉當前打開的連接數不減少,即使連接正確遞增。意圖行爲?

PS。請不要使用用於mongodb的nodejs包裝來回答。

+1

我無法用該代碼重現此行爲。無論我是否終止進程,或是在回調函數中放入'database.close();'調用,連接計數都會回落到0。 – JohnnyHK

回答

0

這是MongoDB計數器的問題。數據庫在關閉所有連接(多個關閉信號)時異步工作,並且發生得非常快,以至於無法跟上更新計數器的時間。在瀏覽MongoDB源代碼之後,您可以看到消息輸出遠不及計數器更新。

下面的類似的輸出,但高達1ms的精度,這說明問題IMO好一點。

Mon Dec 2 17:42:09.059 [conn675] end connection 127.0.0.1:65198 (9 connections now open) 
Mon Dec 2 17:42:09.059 [conn676] end connection 127.0.0.1:65199 (8 connections now open) 
Mon Dec 2 17:42:09.059 [conn677] end connection 127.0.0.1:65200 (8 connections now open) 
Mon Dec 2 17:42:09.059 [conn678] end connection 127.0.0.1:65201 (7 connections now open) 
Mon Dec 2 17:42:09.059 [conn679] end connection 127.0.0.1:65202 (6 connections now open) 
Mon Dec 2 17:42:09.059 [conn680] end connection 127.0.0.1:65203 (5 connections now open) 
Mon Dec 2 17:42:09.059 [conn681] end connection 127.0.0.1:65204 (4 connections now open) 
Mon Dec 2 17:42:09.059 [conn682] end connection 127.0.0.1:65205 (3 connections now open) 
Mon Dec 2 17:42:09.059 [conn683] end connection 127.0.0.1:65206 (2 connections now open) 
Mon Dec 2 17:42:09.059 [conn684] end connection 127.0.0.1:65207 (2 connections now open)