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..921162366f8 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,16 @@ 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) + } + + protected async initTransaction() { + if (this.currentTransaction) return this.currentTransaction - const [session] = await this.driver.instanceDatabase.createSession({}) - this.session = session - this.sessionTransaction = await session.transaction() - return this.session + const [transaction] = + await this.driver.instanceDatabase.getTransaction() + this.currentTransaction = transaction + return this.currentTransaction } /** @@ -85,10 +82,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 +101,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 +113,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 +130,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 +154,6 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { try { const queryStartTime = +new Date() - await this.connect() let rawResult: | [ any[], @@ -171,14 +167,15 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner { ] | undefined = undefined const isSelect = query.startsWith("SELECT") + if (!this.isTransactionActive && !isSelect) { + await this.initTransaction() + await this.currentTransaction.begin() + } + const executor = isSelect && !this.isTransactionActive ? this.driver.instanceDatabase - : this.sessionTransaction - - if (!this.isTransactionActive && !isSelect) { - await this.sessionTransaction.begin() - } + : this.currentTransaction try { this.driver.connection.logger.logQuery(query, parameters, this) @@ -193,13 +190,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 }