Express server crashing due to MongoDB connection loss -
i having issues http node.js server built with:
- ubuntu 14.04
- mongodb 3.0.4
- iojs v2.3.3
express=4.10.*
mongodb=1.4.34
the following middleware being used:
app.use(response_time()); app.use(body_parser.urlencoded({extended: true})); app.use(body_parser.json()); var mongoclient = require('mongodb').mongoclient; app.use(function (req, res, next) { var connection_options = {auto_reconnect: false}; mongoclient.connect(config.server.db, connection_options, function (err, db) { if (err) { log.error(err); // logging error. return next(err); } req.db = db; next(); }); });
the server started running @ 20:40:10 , handled multiple requests.
at 02:59:02, following error started logged on every request:
02:59:02.114z error crowdstudy: failed connect [127.0.0.1:27017] error: failed connect [127.0.0.1:27017] @ null.<anonymous> (/home/ncphillips/projects/crowdstudy/node_modules/mongodb/lib/mongodb/connection/server.js:555:74) @ emitthree (events.js:97:13) @ emit (events.js:175:7) @ null.<anonymous> (/home/ncphillips/projects/crowdstudy/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:156:15) @ emittwo (events.js:87:13) @ emit (events.js:172:7) @ socket.<anonymous> (/home/ncphillips/projects/crowdstudy/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10) @ emitone (events.js:77:13) @ socket.emit (events.js:169:7) @ emiterrornt (net.js:1237:8)
my initial suspicion was connection pool filling because don't have handle calling req.db.close()
. thought passing in options {auto_reconnect: false}
fix issue automatically closing connection after time, seems wrong.
note restarting server fixes issue, believe problem has node rather mongo.
if has connection pool, there setting can pass fix this, or can have end-ware makes sure connection gets closed?
thanks lot can me out!
autoreconnect
option should passed server configuration:
mongoclient.connect(config.server.db, { server : { autoreconnect : false } }, ...);
the documentation contains errors: states default setting false
(which isn't), , states autoreconnect
should set in object called socketoptions
(which shouldn't).
you can add various event listeners db
object gets passed back, detect when connection database got closed/reconnected/...:
db.on('close', function(reason) { ... }); db.on('reconnect', function(db) { ... });
more events here.
Comments
Post a Comment