I'm using MongoSkin in my Node.js application and after a period of inactivity it loses connectivity with the database

StackOverflow https://stackoverflow.com/questions/15168632

Frage

I'm building my first Node.js application and I've run into an issue with MongoDB. I'm using the MongoSkin driver.

Once I start my application it all works great. I can sit and toy with it for an hour and it will continue to work. However, if I let it sit for a few minutes without any activity then the next request will result in an error:

Error: connection closed at Server.connect.connectionPool.on.server._serverState (X:\Code\CodeTunnel\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\connection\server.js:520:35) at EventEmitter.emit (events.js:126:20) at connection.on._self._poolState (X:\Code\CodeTunnel\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:109:15) at EventEmitter.emit (events.js:99:17) at Socket.closeHandler (X:\Code\CodeTunnel\node_modules\mongoskin\node_modules\mongodb\lib\mongodb\connection\connection.js:427:12) at Socket.EventEmitter.emit (events.js:96:17) at Socket._destroy.destroyed (net.js:358:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9)

I've tried adding auto_reconnect=true to the connection string as is suggested on the mongoskin documentation but that didn't help.

War es hilfreich?

Lösung

I eventually figured this one out. The solution is rather simple.

USE MONGOOSE

...or something else.

I've had nothing but problems with MongoSkin and after finally taking the time to learn Mongoose I prefer it anyway. The model paradigm is wonderful, especially since each model has its own persistence methods for saving changes to the document. The use of promises to create a fluent query API is so powerful and sooo much easier to read than the standard callback hell provided by the native MongoDB driver. Callback hell is only slightly mitigated by MongoSkin and it's still very hard to read the more complicated queries.

Edit: Downvote.....really? What thoughts were going through your head Mr. downvote? "Mongoose isn't my preferred ORM so here's a downvote."?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top