asp.net consultancy chorley ASP.NET Server Side Scripting Wigan
Website Development Radcliffe

A Coder's Notes

Email : ren@techsolus.co.uk
bespoke invoice systems Standish Information Technology Advice Worsley
    development systems Ramsbottom
data manipulation Farnworth access databases Whitefield SQL connections Atherton
online accessible software Leyland software compatible Coppull


Get connected and Online Tottington
remote management Makerfield code and design Aspull

NodeJS PostGRESql Pool Close Count Max - 08/05/2018

Here's a quirk. I had the following code...

const pg = require('pg')

const mysql = `
  SELECT *
  FROM peoplet
  `

const config = {
  user:'####',
  database:'####',
  password:'####',
  port:'####',
  host:'####
}

const pool = new pg.Pool({
  connectionString: `postgresql://${config.user}:${config.password}@${config.host}/${config.database}`,
  max:10
})


module.exports = async function () {
  try{
    const client = await pool.connect()
    const result = await client.query(mysql, [])
    client.release
    result.release
    pool.end
    return JSON.stringify(result.rows)
  }catch(error){
    console.log(error)
  }

}
 

Now - what I want you to notice is the LOCATION of the pool within the code.

const pool = new pg.Pool({
  connectionString: `postgresql://${config.user}:${config.password}@${config.host}/${config.database}`,
  max:10
})

It is BEFORE the module.exports part. Now the code works just fine EXCEPT if it is called quickly and often. If I call the code 10 times in a row then on the 11th call it bombs. No errors in the catch it just stops. Most unsatisafactory. The Solution? Move the pool code into the modeul exports bit - thus

module.exports = async function () {

  try{

const pool = new pg.Pool({
  connectionString: `postgresql://${config.user}:${config.password}@${config.host}/${config.database}`,
  max:10
})
    const client = await pool.connect()
    const result = await client.query(mysql, [])
    client.release
    result.release
    pool.end
    return JSON.stringify(result.rows)
  }catch(error){
    console.log(error)
  }

}

Now I do not know EXACTLY what is going on here but... I get the impression that if the pool is outside the block then the closes are inside the block (the releases and the end) it doesn't close them, it doesn't seem to reach out from the block. 

Meh, it works now and I can open it as many times as I like

Post A Comment

Name Comment
programming services asp.net specialist
Valid XHTML 1.0 Transitional
Admin
GD