Reputation: 456
I just tried to use node pool in my blog app, and here is my code:
db.js
module.exports = function () {
return new Db(settings.db, new Server(settings.host, Connection.DEFAULT_PORT), {safe: true});
}
post.js
var Db = require('./Db');
var pool = poolModule.Pool({
name : 'mongoPool',
create : function(callback) {
var mongodb = Db();
callback(null, mongodb);
},
destroy : function(mongodb) {
mongodb.close();
},
max : 100,
min : 3,
idleTimeoutMillis : 30000,
log : true
});
Usage
pool.acquire(function (error, mongodb) {
mongodb.open(function (error, db) {
pool.release(mongodb)
// do something
});
})
As I set min property for the pool(here is 3), after I refresh the page for about 3 times, the page breaks up and alerts: db object already connecting, open cannot be called multiple times
While I set min to 5, then after I refresh for about 5 times, it breaks up again.
Why is this?
Thanks in advance
Upvotes: 1
Views: 772
Reputation: 802
When ever you acquire an instance from the pool is checks if there a free instance and return it. If the isn't a free instance, or it didn't get the min config of instances it will create a new one with the create function.
So in your case, after 3 time (when min=3) it will return to you an instance that you use before. Meaning you going to run on it mongodb.open on the second time.
Your mongodb.open(function (error, db) should be at the create function of the pool.
Upvotes: 1