How to handle Mongoose DB connection interruptions

http://stackoverflow.com/questions/10873199/how-to-handle-mongoose-db-connection-interruptions

Q:Error: connection timeout

I've been evaluating Mongoose (an ORM for node.js which uses MongoDB for persistent storage).

What I'd like to do is make sure that the app can run when the DB is not up when the app starts, and also handles the DB going down intelligently.

Currently my test app which does not work in either case does this:

var mongoose_connection = mongoose.createConnection(DATABASE_URL, {server:{poolSize:4}});

Then I use that connection when making Models.

In the case when the DB is down at the start of the app then any save() calls on instances silently fail with no error. If the DB comes back up they never get written.

So I would need to detect that the connection never happened and have the app be able to tell that at runtime so that I can handle it somehow.

When the DB goes down after the app has started though the save() calls still do not cause errors but they are queued and the written when the DB comes back.

That seems fine except I'd like to hook into the API to get events when the DB is down and to query how many save's are queued. At some point I might have so many queued events that I would want to just stop making new ones and have the app back off.

A:

Case #1: db is down on app startup. there is a minor bug preventing this use case that I'm fixing now. However, here is the work-around:

var db = mongoose.createConnection();
db.on('error', function (err) {
  if (err) // couldn't connect

  // hack the driver to allow re-opening after initial network error
  db.db.close();

  // retry if desired
  connect();
});

function connect () {
  db.open('localhost', 'dbname');
}

connect();

https://gist.github.com/2878607

An ugly but working gist. First shutdown mongo, then run this gist.

Notice the connection failures.

Then start mongo and see all of the queued up inserts complete and dumped to the console. Writes and finds will begin.

Shutdown mongo, notice that the inserts and finds are being attempted but no callbacks are executed.

Restart mongo. Notice all of the queued inserts and finds are completed.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值