From 880aaccdabf027bf2e314fcded20a2f054ecb104 Mon Sep 17 00:00:00 2001 From: Javier De La Fuente Sales Date: Wed, 21 Feb 2024 17:06:33 +0100 Subject: [PATCH 1/3] feat: changed to use transaction only when needed --- package-lock.json | 2 +- package.json | 2 +- src/driver/spanner/SpannerQueryRunner.ts | 54 ++++++++++++------------ 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/package-lock.json b/package-lock.json index eb5e480b1fd..90034cb802d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@streamyard/typeorm", - "version": "0.3.16-4", + "version": "0.3.16-5-beta", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 6ee4f5d138a..f7cd4501278 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@streamyard/typeorm", "private": true, - "version": "0.3.16-4", + "version": "0.3.16-5-beta", "description": "Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB, Spanner databases.", "license": "MIT", "readmeFilename": "README.md", diff --git a/src/driver/spanner/SpannerQueryRunner.ts b/src/driver/spanner/SpannerQueryRunner.ts index 183782a8870..c4bb385e30c 100644 --- a/src/driver/spanner/SpannerQueryRunner.ts +++ b/src/driver/spanner/SpannerQueryRunner.ts @@ -39,14 +39,9 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { driver: SpannerDriver /** - * Real database connection from a connection pool used to perform queries. + * Transaction currently executed. */ - protected session?: any - - /** - * Transaction currently executed by this session. - */ - protected sessionTransaction?: any + protected currentTransaction?: any // ------------------------------------------------------------------------- // Constructor @@ -69,14 +64,14 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { * Returns obtained database connection. */ async connect(): Promise { - if (this.session) { - return Promise.resolve(this.session) - } + return Promise.resolve(this.driver.instanceDatabase) + } - const [session] = await this.driver.instanceDatabase.createSession({}) - this.session = session - this.sessionTransaction = await session.transaction() - return this.session + protected async initTransaction() { + const [transaction] = + await this.driver.instanceDatabase.getTransaction() + this.currentTransaction = transaction + return this.currentTransaction } /** @@ -85,10 +80,10 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { */ async release(): Promise { this.isReleased = true - if (this.session) { - await this.session.delete() + if (this.currentTransaction) { + await this.currentTransaction.end() } - this.session = undefined + this.currentTransaction = undefined return Promise.resolve() } @@ -104,8 +99,8 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { throw err } - await this.connect() - await this.sessionTransaction.begin() + await this.initTransaction() + await this.currentTransaction.begin() this.connection.logger.logQuery("START TRANSACTION") await this.broadcaster.broadcast("AfterTransactionStart") @@ -116,12 +111,12 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { * Error will be thrown if transaction was not started. */ async commitTransaction(): Promise { - if (!this.isTransactionActive || !this.sessionTransaction) + if (!this.isTransactionActive || !this.currentTransaction) throw new TransactionNotStartedError() await this.broadcaster.broadcast("BeforeTransactionCommit") - await this.sessionTransaction.commit() + await this.currentTransaction.commit() this.connection.logger.logQuery("COMMIT") this.isTransactionActive = false @@ -133,12 +128,12 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { * Error will be thrown if transaction was not started. */ async rollbackTransaction(): Promise { - if (!this.isTransactionActive || !this.sessionTransaction) + if (!this.isTransactionActive || !this.currentTransaction) throw new TransactionNotStartedError() await this.broadcaster.broadcast("BeforeTransactionRollback") - await this.sessionTransaction.rollback() + await this.currentTransaction.rollback() this.connection.logger.logQuery("ROLLBACK") this.isTransactionActive = false @@ -157,7 +152,6 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { try { const queryStartTime = +new Date() - await this.connect() let rawResult: | [ any[], @@ -171,13 +165,17 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { ] | undefined = undefined const isSelect = query.startsWith("SELECT") + if (!isSelect && !this.isTransactionActive) { + await this.initTransaction() + } + const executor = isSelect && !this.isTransactionActive ? this.driver.instanceDatabase - : this.sessionTransaction + : this.currentTransaction if (!this.isTransactionActive && !isSelect) { - await this.sessionTransaction.begin() + await this.currentTransaction.begin() } try { @@ -193,13 +191,13 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { json: true, }) if (!this.isTransactionActive && !isSelect) { - await this.sessionTransaction.commit() + await this.currentTransaction.commit() } } catch (error) { try { // we throw original error even if rollback thrown an error if (!this.isTransactionActive && !isSelect) - await this.sessionTransaction.rollback() + await this.currentTransaction.rollback() } catch (rollbackError) {} throw error } From 2ceac682f5db418c72ea93fb31b2700af9c14719 Mon Sep 17 00:00:00 2001 From: Javier De La Fuente Sales Date: Wed, 21 Feb 2024 17:23:24 +0100 Subject: [PATCH 2/3] fix: prevent creating another transaction if there is one active --- src/driver/spanner/SpannerQueryRunner.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/driver/spanner/SpannerQueryRunner.ts b/src/driver/spanner/SpannerQueryRunner.ts index c4bb385e30c..cf44ea75b6d 100644 --- a/src/driver/spanner/SpannerQueryRunner.ts +++ b/src/driver/spanner/SpannerQueryRunner.ts @@ -68,6 +68,8 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { } protected async initTransaction() { + if (this.currentTransaction) return this.currentTransaction + const [transaction] = await this.driver.instanceDatabase.getTransaction() this.currentTransaction = transaction From 622d37356232e12d47749a0bed3600374d505f6b Mon Sep 17 00:00:00 2001 From: Javier De La Fuente Sales Date: Wed, 21 Feb 2024 17:27:12 +0100 Subject: [PATCH 3/3] refactor: improve readability --- src/driver/spanner/SpannerQueryRunner.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/driver/spanner/SpannerQueryRunner.ts b/src/driver/spanner/SpannerQueryRunner.ts index cf44ea75b6d..921162366f8 100644 --- a/src/driver/spanner/SpannerQueryRunner.ts +++ b/src/driver/spanner/SpannerQueryRunner.ts @@ -167,8 +167,9 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { ] | undefined = undefined const isSelect = query.startsWith("SELECT") - if (!isSelect && !this.isTransactionActive) { + if (!this.isTransactionActive && !isSelect) { await this.initTransaction() + await this.currentTransaction.begin() } const executor = @@ -176,10 +177,6 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { ? this.driver.instanceDatabase : this.currentTransaction - if (!this.isTransactionActive && !isSelect) { - await this.currentTransaction.begin() - } - try { this.driver.connection.logger.logQuery(query, parameters, this) rawResult = await executor.run({