问题
同问,mongoos的问题,每次增删改查都要打开关闭时吗?
问题是,我不关,他就挂在那了。。
我的解决方案
使用mongoose内置的连接池,配置如下
options = {
server: {
auto_reconnect: true,
poolSize: 10
}
};
说明:
- poolSize是连接池大小
- auto_reconnect是否自动重连接
代码
// mongoose config
var mongoose = require('mongoose')
, connectionString = 'mongodb://localhost:27017/exam_weixin'
, options = {};
options = {
server: {
auto_reconnect: true,
poolSize: 10
}
};
mongoose.connect(connectionString, options, function(err, res) {
if(err) {
console.log('[mongoose log] Error connecting to: ' + connectionString + '. ' + err);
} else {
console.log('[mongoose log] Successfully connected to: ' + connectionString);
}
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'mongoose connection error:'));
db.once('open', function callback () {
// yay!
console.log('mongoose open success');
});
还有更好的方案?
请不吝赐教