Mongoose: Queries do not return if there was a connection error

Created on 14 Oct 2012  路  9Comments  路  Source: Automattic/mongoose

I don't want my app to exit, if there's an error in the db connection (so, users can be shown some 503 page).
So, I listen on the connection error event, but I just log possible errors to the console.

Now, say db connect has failed, and a request comes in, the app queries the db (e.g. with <Model>.find), but the callback isn't even called. So, the request just times out at some point...

Most helpful comment

I am encountering this issue... Is there a way to at least provide a timeout so that the query will fail / throw an error if the query takes too long?

All 9 comments

please post a test case to reproduce.

var mongoose = require('mongoose')

var db = mongoose.createConnection()
var model = db.model('test', new mongoose.Schema({}))

// register logger
db.on('error', console.log)
db.on('connected', console.log.bind(null, 'Connected'))
db.on('disconnected', console.log.bind(null, 'Disconnected'))

// open connection
console.log('Conecting to db')
db.open( '127.0.0.1', 'test', 27017)


setTimeout(function() {
  console.log('model.find')
  model.find({foo: 'bar'}, null, function queryCallback(error, results) {
    console.log('error', error)
    console.log('results', results)
  })
}, 5000)

Output

Conecting to db
Disconnected
[Error: failed to connect to [127.0.0.1:27017]]
model.find

Notice, how queryCallback is never called.

the driver buffers these commands until a connection is reestablished. failing fast is not yet supported by the driver. would be nice to optionally fail fast when the connection is down. would you mind opening a ticket here ?

As a workaround, I'm checking Connection#readyState for every http request...

Btw, are you sure it's the driver, that buffers the queries?

Important! Mongoose (!) buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MongoDB in order to define models, run queries, etc.

Also, see this: https://github.com/LearnBoost/mongoose/blob/master/lib/collection.js

ah yes I'll clarify.

1) on startup, mongoose buffers command until the connection is first established
2) by default, the auto_reconnect option of the driver is set to true by mongoose. when true, and an established connection is lost, the driver buffers the commands until it is able to reconnect.
3) when auto_reconnect is false and an established connections is lost, mongoose buffers the commands until you manually reestablish the connection.

Two things to take away:

1) Bullet 3 is new in mongoose 3.x. It should be pretty simple to add a schema/query option to disable buffering in this scenario.
2) It would still be nice to have this option in the driver even if auto_reconnect is true.

Thoughts?

Personally, I'd like a mongoose option for query_buffering, decoupled from any reconnect logic.
So, if you have set query_buffering to false and mongoose is disconnected from the db, all queries fail.

Yes that is ideal. Unfortunately the driver has it coupled to auto_reconnect. Until that gets changed we'll be in the same boat either way.

I am encountering this issue... Is there a way to at least provide a timeout so that the query will fail / throw an error if the query takes too long?

For posterity's sake, it looks like an option to disable command buffering was indeed added: https://mongoosejs.com/docs/guide.html#bufferCommands

Was this page helpful?
0 / 5 - 0 ratings