在开发中想使用连接池 在网上找到了一段代码 但是自己测试的时候发现了些问题 当我把setTimeout中的语句数量变成五条以上的时候 nodejs会首先执行五条 然后过一段时间才会执行剩下的 而且好像server_options的pool_size:1好像也没有奇效 不知道这个是什么问题 要怎么做修改 代码如下:
var http=require('http'),
mongodb = require("mongodb"),
poolModule = require('generic-pool');
var pool = poolModule.Pool({
name : 'mongodb',
create : function(callback) {
var server_options={'auto_reconnect':false,poolSize:1};
var db_options={w:-1};
var mongoserver = new mongodb.Server('localhost', 27017,server_options );
var db=new mongodb.Db('test', mongoserver, db_options);
db.open(function(err,db){
if(err)return callback(err);
callback(null,db);
});
},
destroy : function(db) { db.close(); },
max : 10,
idleTimeoutMillis : 30000,
log : false
});
var server=http.createServer(function(req,res){
pool.acquire(function(err, db) {
if (err) {
res.statusCode=500;
res.end(JSON.stringify(err,null,2));
} else {
db.collection('foo').save({test:1},function(err,result){
res.end(JSON.stringify(result,null,2));
pool.release(db);
});
}
});
});
server.listen(8080,function(){
console.log('server listen to %d',this.address().port);
});
setTimeout(function(){
http.get('http://localhost:8080',function(res){console.log('request ok')});
http.get('http://localhost:8080',function(res){console.log('request ok')});
},2000);
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
光阴似箭催人老,日月如移越少年。