简体   繁体   English

尝试在Node.js中连接到Postgresql池时如何修复ECONNREFUSED错误

[英]How to fix an ECONNREFUSED error when try to connect to a postgresql pool in nodejs

When I try bulk query in nodejs with promise.all I got ECONNREFUSED error after some number of query resulted in success. 当我尝试使用promise.all在nodejs中进行批量查询时,经过一定数量的查询导致成功后,我得到了ECONNREFUSED错误。

I am building node server for my web application. 我正在为我的Web应用程序构建节点服务器。 I made some queries resulted from AJAX requests from client. 我从客户端的AJAX请求中得出了一些查询。 They all worked fine till now. 到目前为止,它们都工作良好。

I tried to load data from excel to some number of tables (i use pg module). 我试图将数据从excel加载到一些表中(我使用pg模块)。 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. 但是它进行了一些查询,然后开始出现ECONNREFUSED错误。

  • I have changed max_connections to 10000 and shared_buffers to 25000MB. 我已将max_connections更改为10000,将shared_buffers更改为25000MB。 And restart postgres server 并重启postgres服务器
  • I have changed max_connections to 1000 and shared_buffers to 2500MB.And restart postgres server 我将max_connections更改为1000,shared_buffers更改为2500MB。并重新启动postgres服务器
  • I have changed max_connections to 300 and shared_buffers to 2500MB. 我已将max_connections更改为300,将shared_buffers更改为2500MB。 And restart postgres server 并重启postgres服务器
  • I have changed my code from POSTGRESQL Pool to POSTGRESQL Client 我已将代码从POSTGRESQL Pool更改为POSTGRESQL Client

I have omitted some query in promise array 我在promise数组中省略了一些查询

but nothing changed. 但什么都没有改变。 Almost 180 records were inserted at all. 总共插入了将近180条记录。 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. 以前有Windows 7,但现在有Windows 10。

Your code seems to be in bits and pieces, So cannot point exact line for the error, But the error is coming because of one reason, connection pooling. 您的代码似乎是零零碎碎的,因此无法为错误指出确切的路线,但是由于一种原因,即连接池出现了错误。

Your code is attempting to create a connection with POSTGRES every time you query the database hence for initial runs some data is entered in the database and then starts failing, you need to remove every time connecting part from the query section and use a single instance to deal with the query and also close connection once completed. 您的代码每次尝试查询数据库时都试图与POSTGRES建立连接,因此,为使首次运行在数据库中输入一些数据,然后开始失败,您需要每次从查询部分中删除连接部分,并使用单个实例处理查询,并在完成后关闭连接。

You also mentioned you have upgraded from Win 7 to Win 10, their is no problem with windows version, but your hardware might have got higher configuration too (Ram and number of cores), the way event loop works you some time don't get this error with low configuration systems but with larger config systems you get these errors. 您还提到过您已经从Win 7升级到Win 10,Windows版本没有问题,但是您的硬件可能也具有更高的配置(Ram和核心数),事件循环的工作方式有时还没有解决配置较低的系统但配置较大的系统会出现此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM