Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Thanks for all the help guys - I have managed to solve this issue on both localhost and deployed to a live server.</p> <p>Here is my now working connect code:</p> <pre><code>var MONGO = { username: "username", password: "pa55W0rd!", server: '******.mongolab.com', port: '*****', db: 'dbname', connectionString: function(){ return 'mongodb://'+this.username+':'+this.password+'@'+this.server+':'+this.port+'/'+this.db; }, options: { server:{ auto_reconnect: true, socketOptions:{ connectTimeoutMS:3600000, keepAlive:3600000, socketTimeoutMS:3600000 } } } }; var db = mongoose.createConnection(MONGO.connectionString(), MONGO.options); db.on('error', function(err) { console.log("DB connection Error: "+err); }); db.on('open', function() { console.log("DB connected"); }); db.on('close', function(str) { console.log("DB disconnected: "+str); }); </code></pre> <p>I think the biggest change was to use "createConnection" over "connect" - I had used this before, but maybe the options help now. This article helped a lot <a href="http://journal.michaelahlers.org/2012/12/building-with-nodejs-persistence.html">http://journal.michaelahlers.org/2012/12/building-with-nodejs-persistence.html</a></p> <p>If I'm honest I'm not overly sure on why I have added those options - as mentioned by @jareed, i also found some people having success with "MaxConnectionIdleTime" - but as far as i can see the javascript driver doesn't have this option: this was my attempt at trying to replicate the behavior.</p> <p>So far so good - hope this helps someone.</p> <p><strong>UPDATE: 18 April 2013</strong> <em>note, this is a second app with a different setup</em></p> <p>Now I thought i had this solved but the problem rose it's ugly head again on <strong>another app</strong> recently - with the same connection code. Confused!!!</p> <p>However the set up was slightly different…</p> <p><strong>This new app was running on a windows box using IISNode.</strong> I didn't see this as significant initially.</p> <p>I read there were possibly some issues with mongo on Azure (@jareed), so I moved the DB to AWS - still the problem persisted.</p> <p>So i started playing about with that options object again, reading up quite a lot on it. Came to this conclusion:</p> <pre><code>options: { server:{ auto_reconnect: true, poolSize: 10, socketOptions:{ keepAlive: 1 } }, db: { numberOfRetries: 10, retryMiliSeconds: 1000 } } </code></pre> <p>That was a bit more educated that my original options object i state. However - it's still no good.</p> <p>Now, for some reason i had to get off that windows box (something to do with a module not compiling on it) - it was easier to move than spend another week trying to get it to work.</p> <p>So i moved my app to nodejitsu. <strong>Low and behold my connection stayed alive! Woo!</strong></p> <p>So…. what does this mean… I have no idea! What i do know is is those options seem to work on Nodejitsu…. for me.</p> <p>I believe IISNode uses some kind of "forever" script for keeping the app alive. Now to be fair the app doesn't crash for this to kick in, but i think there must be some kind of "app cycle" that is refreshed constantly - this is how it can do continuous deployment (ftp code up, no need to restart app) - maybe this is a factor; but i'm just guessing now.</p> <p>Of course all this means now, is this isn't solved. It's still not solved. It's just solved for me in my setup.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload