Skip to content

Commit

Permalink
performance / fix remove listeners on query and connection (#388)
Browse files Browse the repository at this point in the history
* fix remove listeners on query and connection

* Update src/database/connection.ts

Co-authored-by: Kamil Ogórek <[email protected]>

---------

Co-authored-by: Kamil Ogórek <[email protected]>
  • Loading branch information
fenos and kamilogorek authored Oct 23, 2023
1 parent ffc4926 commit 7daee60
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/database/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ export class TenantConnection {

async dispose() {
if (this.options.isExternalPool) {
return this.pool.destroy()
await this.pool.destroy()
this.pool.client.removeAllListeners()
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/storage/database/knex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class StorageKnexDB implements Database {
try {
await this.connection.setScope(tnx)

tnx.on('query-error', (error) => {
tnx.once('query-error', (error) => {
throw DBError.fromDBError(error)
})

Expand All @@ -62,6 +62,8 @@ export class StorageKnexDB implements Database {
} catch (e) {
await tnx.rollback()
throw e
} finally {
tnx.removeAllListeners()
}
} catch (e) {
error = e
Expand Down Expand Up @@ -463,8 +465,7 @@ export class StorageKnexDB implements Database {

protected async runQuery<T extends (db: Knex.Transaction) => Promise<any>>(
queryName: string,
fn: T,
isolation?: Knex.IsolationLevels
fn: T
): Promise<Awaited<ReturnType<T>>> {
const timer = DbQueryPerformance.startTimer({
name: queryName,
Expand All @@ -481,7 +482,7 @@ export class StorageKnexDB implements Database {

if (!tnx || needsNewTransaction) {
tnx = await this.connection.transactionProvider(this.options.tnx)()
tnx.on('query-error', (error: DatabaseError) => {
tnx.once('query-error', (error: DatabaseError) => {
throw DBError.fromDBError(error)
})
}
Expand Down Expand Up @@ -512,6 +513,10 @@ export class StorageKnexDB implements Database {
}
timer()
throw e
} finally {
if (needsNewTransaction) {
tnx.removeAllListeners()
}
}
}
}
Expand Down

0 comments on commit 7daee60

Please sign in to comment.