BAE上mongoose长连接问题(新!),求可怜,进来看一眼呗
发布于 10 年前 作者 YDSS 4899 次浏览 最后一次编辑是 8 年前

##这个貌似是新问题! 之前出现长连接问题看一个帖子(地址)搞定了,最近突然发现又报错了~~~~我的连接代码如下:

var mongoose = require('mongoose'),
     settings = require('./settings.js'),
     db = mongoose.createConnection(); // connect to mongoDB with mongoose
 
 //open connection to mongoDB with db options
 db.open(settings.host, settings.db, settings.port, settings.options, functio    n() {
     console.log('mongoDB connected!');
     console.log('host: ' +settings.host + ' db: ' + settings.db + ' port:' +
       settings.port );
   });
 
 db.on('error', function (err) {
     console.error.bind(console, 'connection error:');
     //listen BAE mongodb,if except throws then close the connection
    //why have to do this?Clause it'll be disconnected if it free after 30s     by BAE 
    console.log("db on error!");    //!!跟踪错误的日志
    db.close();
 });
 
 //when close, reopen a connect
db.on('close', function() {
   console.log("db on close!");   //!!跟踪错误的日志
   db.open(settings.host, settings.db, settings.port, settings.options, funct    ion() {
     console.log("db reopen!");   //!!跟踪错误的日志
   });
 });

module.exports = {
   db: db,
   mongoose: mongoose
 }

在BAE后台查看日志,发现30s后我加的跟踪错误的日志都会打出来,也就是说当连接关闭之后再重新打开这个连接的代码是执行了的,还是报no open connections的错误,是不是需要每次都重新createConnection一次,而之前的连接不能用了?

1 回复

刚又看了后台日志,

db reopen!

db on close!

db on error!

db reopen!

db on close!

db on error!

db reopen!

db on close! 都是打出来的,一直刷,为啥就是打不开这个连接呢?

回到顶部