第三版的generic-pool问题,按照里面的example执行的代码,但是很郁闷的是代码不能运行,单步的话,也只是到resourcePromise.then(function(client)
就不执行了,这是为什么那?
使用的模块地址:https://github.com/coopernurs...
全部代码如下:
var genericPool = require('generic-pool');
var DbDriver = require('mysql');
/**
* Step 1 - Create pool using a factory object
*/
const factory = {
create: function(){
return new Promise(function(resolve, reject){
var client = DbDriver.createPool({
host:'localhost',
user : 'root',
password : 'root',
database : 'world'});
client.on('connected', function(){
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);
/**
* Step 2 - Use pool in your code to acquire/release resources
*/
// acquire connection - Promise is resolved
// once a resource becomes available
var resourcePromise = myPool.acquire();
resourcePromise.then(function(client) {
console.log('in ');
client.query("select * from city", [], function(err,result) {
console.log(err);
console.log(result);
// return object back to pool
myPool.release(client);
});
})
.catch(function(err){
// handle error - this is generally a timeout or maxWaitingClients
// error
});
/**
* Step 3 - Drain pool during shutdown (optional)
*/
// Only call this once in your application -- at the point you want
// to shutdown and stop using this pool.
myPool.drain(function() {
myPool.clear();
});
Please refer to the official documentation of mysql: https://github.com/mysqljs/mysql
If
resourcePromise.then cannot enter, it means that resolve or reject has not been executed. In this way, we can locate that resolve(client) in the create of the factory has not been executed, and then locate whether
client.on('connected' has not been executed! Then Check the documentation of mysql.js and it is
client.connect(function(err){} to connect to the database. So the solution is: