From ae3cf0efb1bd05418a7c3f0a8249a867d1e19a93 Mon Sep 17 00:00:00 2001 From: Jaan Oras Date: Tue, 8 Sep 2020 17:31:12 +0300 Subject: [PATCH] fix: handle 'error' events from pool connection (#6262) --- src/driver/postgres/PostgresQueryRunner.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/driver/postgres/PostgresQueryRunner.ts b/src/driver/postgres/PostgresQueryRunner.ts index e4ce86a8b2..e0ff11ebb5 100644 --- a/src/driver/postgres/PostgresQueryRunner.ts +++ b/src/driver/postgres/PostgresQueryRunner.ts @@ -102,10 +102,14 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner * Releases used database connection. * You cannot use query runner methods once its released. */ - release(): Promise { + release(err?: any): Promise { + if (this.isReleased) { + return Promise.resolve(); + } + this.isReleased = true; if (this.releaseCallback) - this.releaseCallback(); + this.releaseCallback(err); const index = this.driver.connectedQueryRunners.indexOf(this); if (index !== -1) this.driver.connectedQueryRunners.splice(index); @@ -164,7 +168,13 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner this.driver.connection.logger.logQuery(query, parameters, this); const queryStartTime = +new Date(); + const onError = (err: any) => { + this.release(err); + }; + databaseConnection.once("error", onError); + databaseConnection.query(query, parameters, (err: any, result: any) => { + databaseConnection.removeListener("error", onError); // log slow queries if maxQueryExecution time is set const maxQueryExecutionTime = this.driver.connection.options.maxQueryExecutionTime;