连接代码:
function connection(){
mongoose.connect("mongodb://" + username + ":" + password +"@"+ host + ":" + port + "/" + db);//需要验证账户
var db = mongoose.connection;
var options = { user: username, pass: password};
db.on('error',function callback() {
console.error.bind(console,'connection error数据库连接失败');
// console.log("connect close retry connect……");
db.open(host, db, port, options);
// console.log("connected");
});
db.once('open',function callback(){
console.log('connection success数据库连接成功');
});
}
bae的mongodb不支持长连接,不到一分钟就会断开,所以我想当断开的时候再次连接就行。目前是可以重新连接的,但是还是会报错,不知道是不是参数传错了,错误如下:
at EventEmitter.emit (events.js:106:17)
at null.<anonymous> (/home/bae/app/.bae/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:568:14)
at Server.Base._emitAcrossAllDbInstances (/home/bae/app/.bae/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:504:17)
at emit (/home/bae/app/.bae/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:254:19)
at Db.EventEmitter.emit (events.js:98:17)
at Db.<anonymous> (/home/bae/app/.bae/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:147:10)
at NativeConnection.EventEmitter.emit (events.js:95:17)
at NativeConnection.callback (/home/bae/app/models/connection.js:25:12)
at NativeConnection.Connection.open (/home/bae/app/.bae/node_modules/mongoose/lib/connection.js:212:10)
at NativeConnection.Connection.error (/home/bae/app/.bae/node_modules/mongoose/lib/connection.js:388:24)
TypeError: string is not a function
^
if (callback) return callback(err);
/home/bae/app/.bae/node_modules/mongoose/lib/connection.js:388
看来没人解决过bae的相关问题,后来我一想,这种断开重连的方式也不科学,总不能是程序与数据库一直保持连接吧, 这样必定消耗数据库资源,不够明智。或许可以在需要数据库操作时再连接。不过当前有点不明白的地方是:session用的是Mongodb,需不需要一直连接数据库?
session只不过是将会话的信息存到mongodb中,当你使用时才用到数据库,不适用就不用啊 :)
@struCoder 那就不知道为什么非要连上数据库才行,有些页面根本没用到数据库
问题已完美解决
@hpgt 怎么解决的,遇到同样的问题了。。。?大神求教啊
@hpgt thank you
@hpgt 这是什么鬼网站,第三方登陆需要注册再绑定。注册完以后说需满一天才能看。。。
@hpgt 好吧 是这个地址 http://guard.iteye.com/blog/2123573
@sjfkai 目前的连接方式这样的,一直都好好的
/**
* 连接到mongodb
* 使用mongoose而非mongodb中间件
**/
var mongoose = require('mongoose');
var settings = require('../settings');
var host = settings.host,
port = settings.port,
username = settings.uid,
password = settings.pwd,
dbName = settings.db,
url = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/" + dbName;
var count = 0;
var dbcon = null;
function getConnect() {
var opts = {
db: {
native_parser: true
},
server: {
poolSize : 5,//默认为5
auto_reconnect: true
},
user: username,
pass: password
};
(function connect(){
mongoose.connect(url,opts);//一个数据库用connect,多个用createConnection
dbcon = mongoose.connection;//获取Connection 连接对象
dbcon.on('error', function(error) {
console.log('connection error');
dbcon.readyState = "disconnected";
reConnect();
});
//监听关闭事件并重连
dbcon.on('disconnected', function() {
console.log('disconnected');
dbcon.readyState = "disconnected";
dbcon.close();
});
dbcon.on('close', function(err) {
dbcon.readyState = "disconnected";
reConnect();
console.log('close-event-to-connect');
});
dbcon.on('connecting', function() {
console.log('connecting');
});
dbcon.on('connected', function() {
console.log('connected');
});
dbcon.on('disconnecting', function() {
console.log('disconnecting');
});
})();//定义完自执行
//connected
function reConnect(){
dbcon.on('close', function(){
dbcon.open(host, dbName, port, opts,function() {
console.log('closed-reconnect');
});
})
}
}
exports.getConnect = getConnect;//包含到module.exports对象中,
exports.mongoose = mongoose;
@sjfkai 一直没监听到断开的事件,也不知道是不是支持长连接了
@hpgt 你好,请问现在这个办法还有效吗,我试了下好像不行,第二次请求的时候,直接就显示504 gateway timeout
@yanbai 也不敢肯定,正常情况下可以保证博客访问,但是也会时不时的挂掉,百度云的mongodb不推荐使用,还是MySQL保险点