node.js - version3的generic-pool问题
伊谢尔伦
伊谢尔伦 2017-04-17 16:16:23
0
2
450

第三版的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();
});
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回覆(2)
伊谢尔伦

請參考 mysql 的官方文件:https://github.com/mysqljs/mysql

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'root',
  database : 'world'
});
 
connection.connect(function(err) {

});

PS: Promise 沒有執行可以斷定是 resolve 或 reject 沒有執行到,這樣可以定位到是沒有 connected 事件。而且 mysql 函式庫本身有連接池用法的,所以不需要用 generic-pool。附註:描述問題症狀而非你的猜測

左手右手慢动作

resourcePromise.then 進不去說明resolve 或reject 沒有執行到,這樣可以定位到factory 的create 中的resolve(client)沒執行,那麼再定位到是不是
client.on('connected' 沒執行呢!接著是 接著執行呢!接著查一下mysql.js 的文件是
client.connect(function(err){} 來進行資料庫連線的。 所以解決方法是:

var client = require('mysql').createConnection({
                host:'localhost',
                user     : 'root',
                password : 'root',
                database : 'world'});
 client.connect(function(err){
    if(err){
        console.log('Database connection error');
     }else{
        esolve(client);
        console.log('Database connection successful');
     }
);
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!