Closed Sunkaystar closed 6 years ago
hi @Bodhivrksa do you have any logs or error messages you could paste here?
hi @sandfox I tried , I wrote console.log() in factory.create,factory.destroy and resourcePromise.then() , but none logs or error output , After a time the js is over, and error
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
@Sunkaystar I'm sure you are wrong . when i read the source code of Pool.js, print the stack and find the error:
TypeError: mysql.createClient is not a function
at /Users/young/github/ssr-press/pool.js:8:32
at Promise (<anonymous>)
at Object.create (/Users/young/github/ssr-press/pool.js:5:16)
at Pool._createResource (/Users/young/github/ssr-press/node_modules/generic-pool/lib/Pool.js:308:42)
at Pool._dispense (/Users/young/github/ssr-press/node_modules/generic-pool/lib/Pool.js:226:12)
at _trackOperation.then.catch.reason (/Users/young/github/ssr-press/node_modules/generic-pool/lib/Pool.js:320:14)
at <anonymous>
at runMicrotasksCallback (internal/process/next_tick.js:121:5)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
You have wrong handle about mysql. perhaps the local mysql'port is not default 3306.
create: function(){
return new Promise(function(resolve, reject){
console.log("run create");//repeat run
var client = mysql.createConnection({
connectionLimit : 10,
host : 'localhost',
user : 'root',
password : '',
database : 'nodepressdb',
port: 3306
});
client.connect();
resolve(client)
})
},
destroy: function(client){
return new Promise(function(resolve){
client.end()
})
}
mysql version is also 2.5.0
@royalrover thank you, I know what I made mistake,I use the Newest source code,I succeed.
const genericPool = require("generic-pool");
const mysql = require("mysql");
const factory = {
create: function() {
return mysql.createConnection({
connectionLimit : 10,
host : 'localhost',
user : 'root',
password : '123456',
database : 'mysql_test',
port : 3306
});
},
destroy: function(client) {
client.disconnect();
}
};
const opts = {
max: 10, // maximum size of the pool
min: 2 // minimum size of the pool
};
const myPool = genericPool.createPool(factory, opts);
const resourcePromise = myPool.acquire();
resourcePromise
.then(function(client) {
client.query("SELECT * from user_manage ", [], function(err,result) {
console.log(result);
myPool.release(client);
});
})
.catch(function(err) {
// handle error - this is generally a timeout or maxWaitingClients
// error
});
myPool.drain().then(function() {
myPool.clear();
});
thanks @royalrover
I used generic-pool and msyql together,but my example is not working,can you find reasons and tell me,Thank you.