coopernurse / node-pool

Generic resource pooling for node.js
2.37k stars 259 forks source link

used generic-pool and msyql together #209

Closed Sunkaystar closed 6 years ago

Sunkaystar commented 6 years ago

I used generic-pool and msyql together,but my example is not working,can you find reasons and tell me,Thank you.

    var genericPool = require('generic-pool');//v3.2.0
    var mysql = require('mysql');// v2.15.0
    var  factory = {
    create: function(){
        return new Promise(function(resolve, reject){
            console.log("run create");//repeat run
            //var client=mysql.createConnection({...});//it is not working
            var client = mysql.createClient({
                connectionLimit : 10,
                host            : 'localhost',
                user            : 'root',
                password        : '123456',
                database        : 'mysql_test'
            });
            client.on('connected', function(){
               console.log("run connected");//No output
                resolve(client)
            })
        })
    },
    destroy: function(client){
        return new Promise(function(resolve){
            client.on('end', function(){
                resolve()
            });
            client.disconnect()
        })
    }
    };
    var opts = {
    max: 10, // maximum size of the pool
    min: 2 // minimum size of the pool
    };
    var myPool = genericPool.createPool(factory, opts);
    var resourcePromise = myPool.acquire();
    resourcePromise.then(function(client) {
    console.log("run resourcePromise");//No output
    client.query("SELECT * from user_manage",[], function (error, results) {
        if (error) {
            console.log(error);
        }else{
            client.release();
            console.log(results);
        }
    });
    })
    .catch(function(err){
        console.log(err);
    });
sandfox commented 6 years ago

hi @Bodhivrksa do you have any logs or error messages you could paste here?

Sunkaystar commented 6 years ago

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
royalrover commented 6 years ago

@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

Sunkaystar commented 6 years ago

@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();
});
sandfox commented 6 years ago

thanks @royalrover