Merge pull request #5796 from Budibase/fix/pg-connection-issues

more strategic connection closing in pg
This commit is contained in:
Martin McKeaveney 2022-05-10 11:41:19 +01:00 committed by GitHub
commit 61b97b68de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 45 additions and 37 deletions

View File

@ -147,7 +147,7 @@ module PostgresModule {
return parts.join(" || ") return parts.join(" || ")
} }
async internalQuery(query: SqlQuery) { async internalQuery(query: SqlQuery, close: boolean = true) {
const client = this.client const client = this.client
this.index = 1 this.index = 1
// need to handle a specific issue with json data types in postgres, // need to handle a specific issue with json data types in postgres,
@ -164,10 +164,11 @@ module PostgresModule {
try { try {
return await client.query(query.sql, query.bindings || []) return await client.query(query.sql, query.bindings || [])
} catch (err) { } catch (err) {
await this.client.end()
// @ts-ignore // @ts-ignore
throw new Error(err) throw new Error(err)
} finally { } finally {
await this.client.end() if (close) await this.client.end()
} }
} }
@ -204,11 +205,11 @@ module PostgresModule {
} }
} catch (err) { } catch (err) {
tableKeys = {} tableKeys = {}
} finally {
await this.client.close()
} }
try {
const columnsResponse = await this.client.query(this.COLUMNS_SQL) const columnsResponse = await this.client.query(this.COLUMNS_SQL)
const tables: { [key: string]: Table } = {} const tables: { [key: string]: Table } = {}
for (let column of columnsResponse.rows) { for (let column of columnsResponse.rows) {
@ -246,6 +247,12 @@ module PostgresModule {
const final = finaliseExternalTables(tables, entities) const final = finaliseExternalTables(tables, entities)
this.tables = final.tables this.tables = final.tables
this.schemaErrors = final.errors this.schemaErrors = final.errors
} catch (err) {
// @ts-ignore
throw new Error(err)
} finally {
await this.client.end()
}
} }
async create(query: SqlQuery | string) { async create(query: SqlQuery | string) {
@ -274,8 +281,9 @@ module PostgresModule {
if (Array.isArray(input)) { if (Array.isArray(input)) {
const responses = [] const responses = []
for (let query of input) { for (let query of input) {
responses.push(await this.internalQuery(query)) responses.push(await this.internalQuery(query, false))
} }
await this.client.end()
return responses return responses
} else { } else {
const response = await this.internalQuery(input) const response = await this.internalQuery(input)