I am using mongoose for connecting node.js with mongoDB, now i wrote below query
var trans = new transmodel({method: method, trans_id: r});
trans.save(fun
You are using mongoose, it emits events (the EventEmitter pattern) when the database is down and when the database is reconnecting and up again.
from mongoose code found here we can see that the library db connection - connection.js
has the following events that are emitted: * @param {Mongoose} base a mongoose instance * @inherits NodeJS EventEmitter
http://nodejs.org/api/events.html#events_class_events_eventemitter
* @event connecting
: Emitted when connection.{open,openSet}()
is executed on this connection.
@event connected
: Emitted when this connection successfully connects to the db. May be emitted multiple times in reconnected
scenarios.
@event open
: Emitted after we connected
and onOpen
is executed on all of this connections models.
@event disconnecting
: Emitted when connection.close()
was executed.
@event disconnected
: Emitted after getting disconnected from the db.
@event close
: Emitted after we disconnected
and onClose
executed on all of this connections models.
@event reconnected
: Emitted after we connected
and subsequently disconnected
, followed by successfully another successfull connection.
@event error
: Emitted when an error occurs on this connection.
@event fullsetup
: Emitted in a replica-set scenario, when primary and at
least one seconaries specified in the connection string are connected.
@event all
: Emitted in a replica-set scenario, when all nodes specified in the connection string are connected.
When the database is down you will receive two events: 1. disconnected 2. error (the error that driver encountered)
When the database is up again you will receive the reconnect event.
So you don't need to try catch the error rather you should listen to these events.
More helpful information about connection failures and reconnecting can be found here.
This article explain how to use and configure the autoReconnect and the bufferMaxEntries according to your settings.