From 08b68ef8ba895cb82ad763bd5ab924991a53469a Mon Sep 17 00:00:00 2001 From: Mononaut Date: Tue, 14 Nov 2023 05:33:48 +0000 Subject: [PATCH] handle exception in db transaction rollback --- backend/src/database.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/backend/src/database.ts b/backend/src/database.ts index 21d90261d..a9893fdac 100644 --- a/backend/src/database.ts +++ b/backend/src/database.ts @@ -72,6 +72,15 @@ import { execSync } from 'child_process'; } } + private async $rollbackAtomic(connection: PoolConnection): Promise { + try { + await connection.rollback(); + await connection.release(); + } catch (e) { + logger.warn('Failed to rollback incomplete db transaction: ' + (e instanceof Error ? e.message : e)); + } + } + public async $atomicQuery(queries: { query, params }[]): Promise<[T, FieldPacket[]][]> { @@ -90,9 +99,8 @@ import { execSync } from 'child_process'; return results; } catch (e) { - logger.err('Could not complete db transaction, rolling back: ' + (e instanceof Error ? e.message : e)); - connection.rollback(); - connection.release(); + logger.warn('Could not complete db transaction, rolling back: ' + (e instanceof Error ? e.message : e)); + this.$rollbackAtomic(connection); throw e; } finally { connection.release();