开发者

node.js: Recovering from database connection close

Lets say my db wrapper code looks like this:

DB.open('localhost', port, function(err, db){
  exports.withDatabase = function(callback) {
    callback(db);
  }
});

As this is a separate module, the DB is open only once when first required. This takes care of not having to open the connection on every access, 开发者_StackOverflowalso module require is synchronous, so database is not connected more than once. But if the connection goes down and comes back later, how do i get this code to reconnect.


The docs state that you can either use the auto_reconnect option when you create the server connection or react to the "close" event that fires when the DB connection is lost:

DB.on("close", function(error){
  // ...
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜