0

I am trying to add data to my databases. There are multiple entries in an array

dataArryay.forEach((element) => {

    queryData(elementData)
}

The function is called multiple times

async function queryData(data) {

    const queryString = `INSERT............')`;

    const query = {
        text: queryString
    };

    const pool = await new pg.Pool({
        host: 'localhost',
        port: 'xxxx',
        user: 'xxxxxx',
        database: 'xxxxx',
        max: 100,
            idleTimeoutMillis: 50000,
            connectionTimeoutMillis: 3000,
        });
        await pool.connect();
        await pool.query(query)
        await pool.end()
    }

It does the insert but it does throw the too many connections error. I have tried .release() and .end()

When querying the connections I get

max_conn = 500
used = 6
res_for_super = 3
res_for_normal = 491

I don't really know what these men but they seem to add up to 500.

TommyD
  • 913
  • 3
  • 17
  • 32
  • 1
    Do not construct a new Pool inside `queryData`! There should be ideally one single pool in your entire application, from which you then create clients. – Bergi Nov 27 '19 at 23:03
  • How large is your `dataArryay`? You might need to [limit concurrency](https://stackoverflow.com/a/38778887/1048572) of your iteration. At least, [don't use `forEach`](https://stackoverflow.com/q/37576685/1048572). – Bergi Nov 27 '19 at 23:06
  • yes had tried it both ways, thought the pool may be killed and released if inside the loop. – TommyD Nov 28 '19 at 08:15

0 Answers0