应用程序的Mongoose连接随机关闭

时间:2012-06-23 00:21:50

标签: node.js express mongoose

我有一个使用node.js的简单webapp,它读取数据库并将这些数据显示给用户。存在此问题,在未主动使用应用程序后,最初查看连接错误时出现[错误:连接已关闭]。重新加载连接似乎没问题,虽然它似乎在第一次发生后随机轰炸并且你点击了(我很确定是一个工人死于连接错误并重新启动)。这几乎是代码的所有重要部分......

//POSTPROVIDER.JS
var mongoose = require('mongoose');
var async = require('async');

var settings = require('./settings.js').mongodb;
if(!settings) {
  console.log('Could not connect to MongoDB server - missing MongoDB settings');
}

var connectString = "mongodb://" +
  settings.username + ":" + settings.password + "@" +
  settings.hostname + ":" + settings.hostport + "/" +
  settings.database;


mongoose.connect(connectString);

mongoose.connection.on('error', function(error) {
  console.log(error)
  mongoose.connect(connectString);
})

var Schema = mongoose.Schema;

var FeedbackSchema  = new Schema({
  //Schema
});

var feedbackDoc = mongoose.model('Feedback', FeedbackSchema);

//Example query
PostProvider.prototype.findByUuid = function(_id, callback) {

  feedbackDoc.findById( _id).exclude('screenshot').exec(function (err, feedback) {
    if (err) return callback(err);

    var feedbacks = JSON.stringify(convertFeedback(feedback), null, '    ');

    callback( null, feedbacks )
  });

};

exports.PostProvider = PostProvider;

服务器代码......

//APP.JS    
var express = require('express');
var request = require('request');
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

var app = module.exports = express.createServer();

// Express Config
var pub = __dirname + '/public';

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.compiler({ src: pub, enable: ['sass'] }));
  app.use(express.static(pub));
  app.use(app.router);
});

var error = require('./error');

app.configure('development', function(){
  app.use(error({ showMessage: true, dumpExceptions: true, showStack: true, logErrors: __dirname + '/log/error_log' }));
});

app.configure('production', function(){
  app.use(error({ showMessage: true, dumpExceptions: true, showStack: true, logErrors: __dirname + '/log/error_log' }));
});

var PostProvider = require('./postprovider').PostProvider;
var PostProvider= new PostProvider();


if (cluster.isMaster) {
  // Fork workers.

  console.log("Master is running.  [ %d ]", process.pid );
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('death', function(worker) {
    console.log('worker ' + worker.pid + ' died');
    console.log(worker)
    cluster.fork();
  });
} else {
  // Worker processes have a http server.();
    console.log("Worker is running.  [ %d ]", process.pid );

  //Main View
  app.get('/', function(req, res){

    var pageNum = 1;

      res.render('layout', {
            locals: {
              title: 'View Portal',
              pageNum: pageNum
            }
      });
  });

  app.listen(8900);
  console.log("Express server listening on port 8900");    
}

有没有办法检查连接状态,如果有办法在处理查询之前正确重新连接?

1 个答案:

答案 0 :(得分:0)

使用新版本的Node,必须重写有问题的应用程序。回答我自己的问题来解决这个问题。