node.js - version3的generic-pool問(wèn)題
問(wèn)題描述
第三版的generic-pool問(wèn)題,按照里面的example執(zhí)行的代碼,但是很郁悶的是代碼不能運(yùn)行,單步的話,也只是到resourcePromise.then(function(client)就不執(zhí)行了,這是為什么那?使用的模塊地址: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 availablevar 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 poolmyPool.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();});
問(wèn)題解答
回答1:請(qǐng)參照 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 沒(méi)有執(zhí)行可以斷定是 resolve 或 reject 沒(méi)有執(zhí)行到,這樣可以定位到是沒(méi)有 connected 事件。而且 mysql 庫(kù)本身帶連接池用法的,所以不需要用 generic-pool。附個(gè)文章吧:描述問(wèn)題癥狀而非你的猜測(cè)
回答2:resourcePromise.then 進(jìn)不去說(shuō)明 resolve 或 reject 沒(méi)有執(zhí)行到 ,這樣可以定位到 factory 的 create 中的 resolve(client)沒(méi)執(zhí)行,那么再定位到 是不是client.on(’connected’ 沒(méi)執(zhí)行呢 ! 接著查一下 mysql.js 的文檔 是client.connect(function(err){} 來(lái)進(jìn)行數(shù)據(jù)庫(kù)連接的。 所以解決方法是:
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’); });
相關(guān)文章:
1. javascript - immutable配合react提升性能?2. javascript - sublime快鍵鍵問(wèn)題3. css - 寫頁(yè)面遇到個(gè)布局問(wèn)題,求大佬們幫解答,在線等,急!~4. 配置Apache時(shí),添加對(duì)PHP的支持時(shí)語(yǔ)法錯(cuò)誤5. phpstudy8.1支持win11系統(tǒng)嗎?6. javascript - nodejs關(guān)于進(jìn)程間發(fā)送句柄的一點(diǎn)疑問(wèn)7. javascript - 移動(dòng)端上不能實(shí)現(xiàn)拖拽布局嗎?8. 實(shí)現(xiàn)bing搜索工具urlAPI提交9. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問(wèn)?10. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽(tīng)數(shù)據(jù)變化

網(wǎng)公網(wǎng)安備