Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

node.js - Connection reset while executing multiple queries using Postgres pg and node

Here is my code that performs multiple select queries one after other.

const {Pool} = require('pg');    
const pool   = new Pool(POSTGRES_CONFIG);

var aggregateDomainCount = [], domainCount={};

pool.connect((err, client, release) => {
    if (err) {
        return console.error('Connection error', err.stack)
    }
    console.log('Connection established');
    client.query(firstQuery, function(err, results) {
          // call `done()` to release the client back to the pool      
          if(err) {
            return console.error('error running query', err);
          }
          domainCount = {"data1":results.rows[0].count};
          aggregateDomainCount.push(domainCount);
     });
     ......

     client.query(lastQuery, function(err, results) {
          // call `done()` to release the client back to the pool      
          if(err) {
            return console.error('error running query', err);
          }
          domainCount = {"dataN":results.rows[0].count};
          aggregateDomainCount.push(domainCount);
          release();
          response.json(aggregateDomainCount);
     });
});

The issue is while executing above queries, console first prints('Connection established') and then after series of execution, again it tries to enter pool.connect before completing all queries and prints('Connection established') and finally results in unknown error to user interface. If i reduce number of queries by some, then I get expected results but which is again inconsistent. So far, I have tried

  1. nesting client.query one inside other
  2. nesting pool.connect by grouping set of queries
  3. setting different timeout parameters
  4. using await client.query()

But all the above resulted in same issue. Could you please point me in right direction.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...