1

When I try bulk query in nodejs with promise.all I got ECONNREFUSED error after some number of query resulted in success.

I am building node server for my web application. I made some queries resulted from AJAX requests from client. They all worked fine till now.

I tried to load data from excel to some number of tables (i use pg module). I wrote a code that insert records after reading if there is not a same record. I used promise to make queries. But it made some queries and then start to get ECONNREFUSED error.

  • I have changed max_connections to 10000 and shared_buffers to 25000MB. And restart postgres server
  • I have changed max_connections to 1000 and shared_buffers to 2500MB.And restart postgres server
  • I have changed max_connections to 300 and shared_buffers to 2500MB. And restart postgres server
  • I have changed my code from POSTGRESQL Pool to POSTGRESQL Client

I have omitted some query in promise array

but nothing changed. Almost 180 records were inserted at all. Then got en error.


    function loadData(auditArrayObject){
        return new Promise(function (resolve, reject) {
            let promises=[
                loadAuditItem(auditArrayObject.audit,
                             auditArrayObject.auditVersion),
                loadProcesses(auditArrayObject.processArray),
                loadControlAims(auditArrayObject.controlAimArray),
                loadCriterias(auditArrayObject.criteriaArray),
                loadParameters(auditArrayObject.parameterArray),
            ]
            Promise.all(promises)
            .then(objectsWithId=>{
              ......
            }
    }

    function loadProcesses(processArray){
        return new Promise(function (resolve, reject) {
            let promises=[];
            for(let i=0;i<processArray.length;i++){
                let process= new Process(null,processArray[i],false)
                let promise= postGreAPI.readProcessByName(process.name)
                             .then(resultProcess=>{
                                if (!resultProcess) {
                                    postGreAPI.createProcess(process)
                                    .then(createdProcess=>{
                                        resolve(createdProcess)
                                    })
                                    .catch(err=>{
                                        reject({msg:"createProcess 
                                               hata aldı",err:err})
                                    })
                                } else {
                                    return (resultProcess)
                                }
                             })
                             .catch(err=>{
                                reject({msg:"readProcessByName 
                                        hata aldı",err:err})
                             })
                promises.push(promise)
            }
            Promise.all(promises)
            .then(processArray=>{
                resolve({key:"Process",value:processArray})
            })
            .catch(err=>{
                reject({msg:"Processlerden birisi insert edilemedi",err:err})
            })
        });
    }

    postGreAPI.readProcessByName:

    var readProcessByName = function (name){

            return new Promise(function (resolve, reject) {
                let convertedName=convertApostrophe(name)
                let query = "SELECT * FROM process WHERE name='" 
                          + convertedName + "'"
                queryDb(query)
                .then(result=>{
                    if (result.rows.length>0){
                        let process = new Process(result.rows[0].id,
                                                  result.rows[0].name,
                                           result.rows[0].isactive);
                        resolve(process)
                    }else{
                        resolve(null)
                    }
                })
                .catch(err=>{
                    reject(err)
                })
            })

        }

    queryDb:

    var queryDb2 = function (query,params) {
            return new Promise(function (resolve, reject) {
                let pool = new PostGre.Pool(clientConfig);
                pool.connect(function(err, client, done) {
                    if(err) {
                        return reject (err);
                    }
                    client.query(query,params, function(err, result) {
                        done();
                        if(err) {
                            return reject (err);
                        }
                        resolve(result)
                    });
                });
            })
        }

And error is :

     Error: connect ECONNREFUSED 127.0.0.1:5432
        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
      errno: 'ECONNREFUSED',
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '127.0.0.1',
      port: 5432 }

Actually i have succed that load before my laptop configuration changed. Before there was a windows 7 but now windows 10.

4

1 回答 1

0

您的代码似乎是零碎的,因此无法为错误指出确切的行,但是错误的出现是由于一个原因,连接池。

每次查询数据库时,您的代码都会尝试创建与 POSTGRES 的连接,因此对于初始运行,在数据库中输入了一些数据然后开始失败,您需要从查询部分中删除每次连接部分并使用单个实例处理查询并在完成后关闭连接。

您还提到您已经从 Win 7 升级到 Win 10,Windows 版本没有问题,但是您的硬件可能也有更高的配置(Ram 和内核数),事件循环的工作方式您有些时候不明白低配置系统出现此错误,但配置系统较大时,您会收到这些错误。

于 2019-01-09T11:36:52.277 回答