统一回复: mongoose的问题,每次增删改查都要打开关闭时吗?
发布于 3个月前 作者 i5ting 337 次浏览 来自 分享

问题

同问,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');
});

还有更好的方案?

请不吝赐教

7 回复

cnode 好像连连接池都没用。。

我查了下,默认 pool 是 5。http://mongoosejs.com/docs/connections.html 线上应该够用了。有一次我看乌云上,一个线上服务的配置流出,那个服务访问量也不算小了,pool size 也是 5。

关于 pool size 的大小,这个回答不错。

The optimum size of a thread pool depends on the number of processors available and the nature of the tasks on the work queue. On an N-processor system for a work queue that will hold entirely compute-bound tasks, you will generally achieve maximum CPU utilization with a thread pool of N or N+1 threads. For tasks that may wait for I/O to complete – for example, a task that reads an HTTP request from a socket – you will want to increase the pool size beyond the number of available processors, because not all threads will be working at all times. Using profiling, you can estimate the ratio of waiting time (WT) to service time (ST) for a typical request. If we call this ratio WT/ST, for an N-processor system, you’ll want to have approximately N*(1+WT/ST) threads to keep the processors fully utilized. Processor utilization is not the only consideration in tuning the thread pool size. As the thread pool grows, you may encounter the limitations of the scheduler, available memory, or other system resources, such the number of sockets, open file handles, or database connections. So profile your application, if your threads are mostly cpu bound, then set the thread pools size to number of cores, or number of cores + 1. If you are spending most of your time waiting for database calls to complete, then experiment with a fairly large number of threads, and see how the application performs.

@alsotang

这个答案也不错

poolSize connections are opened to the db when you connect. the default is 5 which is usually plenty【充足的】.

best practice is to open a single mongoose connection and reuse it during the life of your application and close it at application shutdown. if you are opening many many mongoose connections the number of actual connections open will be nMongooseConnections * poolSize.

keep in mind mongodb has a hard limit of 20000 open connections.

Bottom line, the default is probably all you need and only play with the poolSize if you see benefits from your testing.

https://github.com/LearnBoost/mongoose/issues/1172

如果不关,就是所谓的长连接吧!你使用autoreconnect的目的同样是为了保持数据库一直处于连接状态,这样就不用每次操作都要打开 连接,频繁的打开关闭也会对数据库造成压力

@hpgt 如果不了解连接池,可以看看c3p0 或dbcp,哈哈,你就懂了

@i5ting 嗯嗯,看来默认 5 个是够用的

回到顶部