Created
April 21, 2014 22:04
-
-
Save howarddierking/11158160 to your computer and use it in GitHub Desktop.
bad mongodb connection management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dbClient; | |
var ensureDbClient = function(callback){ | |
if(dbClient){ | |
console.info('ensureDbClient: returning existing client'); | |
return callback(null, dbClient); | |
} else { | |
console.info('ensureDbClient: returning NEW client'); | |
MongoClient.connect(mongoConnectionString, mongoConnectionOptions, function(err, db){ | |
if(err){ | |
return callback(err, null); | |
} | |
dbClient = db; | |
callback(null, dbClient); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes - and as you mentioned on the phone, the culprit is likely that multiple invocations of this fcn have occurred before the initialization of the connection can happen. Modifying the module now to initialize the connection first...