Preface: Updating for mongodb 3.2 compatibility ... there are several 
fields that were previously set on actions that are no longer being set.
Have tried searching stackoverflow (which is where I discovered them 
originally) but have not located any info.

Specifically we look at the following (some no longer there) fields to see 
if a *cached connection* is still *valid *and can be *reused*:

[db].openCalled  :: used to be boolean once open() has been called, no 
longer there or moved
[db]._state   :: used to indicate the "state" (e.g. "connected"), no longer 
there or moved
[db].serverConfig.checkoutWriter()   ::  this would return an Error under 
certain failure conditions

Anyone have guidance on how to update the following code to achieve the 
same results?

exports.getMongoDBConnection = function(callback) {
    if (pooledConnection && pooledConnection.openCalled)
    {
      if (!!callback && typeof(callback) === "function") { callback(null, 
pooledConnection); }
    }
    else { // circa 2.2 always entering this since openCalled is gone
     logging.info("No available pooled connection, opening new mongo 
connection.");  

     MongoClient.connect(connectionString, mongoOptions, function (err, db) 
{
        if (err) {
            var error = "Error connecting to Mongo: " + err;
            logging.error(error, err);
            if (!!callback && typeof(callback) === "function") { 
callback(err); }
        } else {
            pooledConnection = db;  // cache the connection
             db.on('close', function() {
              if (db === pooledConnection) pooledConnection = null;
              if (this._callBackStore) {
               for(var key in this._callBackStore._notReplied) {
                  this._callHandler(key, null, 'Connection Closed!');   // 
notify any pending requests
                }
              }
            });
            db.on('reconnect', function() {
              logging.warn("mongoHelper.js", 'Mongo reconnect detected');
              if (this._callBackStore) {
                for(var key in this._callBackStore._notReplied) {
                  this._callHandler(key, null, 'Connection reconnected, 
waiting queries are no longer valid');
                }
              }
            });
...


exports.closeMongoDBConnection = function(db,force,callback) {
    if (db && db.openCalled) {
        var connection = db.serverConfig.checkoutWriter();
        if (!!connection && connection instanceof Error) {
          logging.warn("Closing failed mongo connection", connection);
          force = true;
        }
        if ((force || db !== pooledConnection)) {
          logging.info("Closing mongo connection in state: " + db._state);
          return db.close(true, callback);
        }
    }
    else if (!!callback && typeof(callback) === "function") 
callback(null,db);
};


re: 
http://stackoverflow.com/questions/10470123/using-db-open-with-mongodb-and-nodejs
     
http://stackoverflow.com/questions/9656688/mongodb-driver-in-nodejs-does-not-call-open-function-callback
     
http://stackoverflow.com/questions/18688282/handling-timeouts-with-node-js-and-mongodb

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/bfee3da4-5ba0-4e29-9249-9332e8fb40fb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to