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

第三版的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

リーリー

追記: Promiseが実行されない場合は、resolveまたはrejectが実行されていないと判断できるため、接続されたイベントがないと判断できます。また、mysql ライブラリ自体には接続プールの使用があるため、generic-pool を使用する必要はありません。記事を添付します: 推測ではなく問題の症状を説明してください

いいねを押す +0
左手右手慢动作

resourcePromise.thenが入力できない場合は、resolveまたはrejectが実行されていないことを意味します。このようにして、ファクトリの作成時にresolve(client)が実行されていないことを確認できます。
client.on('connected' は実行されていません) を実行してください。次に mysql.js のドキュメントを確認すると、データベースに接続するために
client.connect(function(err){} となっています。解決策は次のとおりです:

リーリー
いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!