Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: changed to use transaction only when needed
  • Loading branch information
javierdelafuentesales committed Feb 21, 2024
commit 880aaccdabf027bf2e314fcded20a2f054ecb104
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
54 changes: 26 additions & 28 deletions src/driver/spanner/SpannerQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -69,14 +64,14 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner {
* Returns obtained database connection.
*/
async connect(): Promise<any> {
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
}

/**
Expand All @@ -85,10 +80,10 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner {
*/
async release(): Promise<void> {
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()
}

Expand All @@ -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")
Expand All @@ -116,12 +111,12 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner {
* Error will be thrown if transaction was not started.
*/
async commitTransaction(): Promise<void> {
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

Expand All @@ -133,12 +128,12 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner {
* Error will be thrown if transaction was not started.
*/
async rollbackTransaction(): Promise<void> {
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

Expand All @@ -157,7 +152,6 @@ export class SpannerQueryRunner extends BaseQueryRunner implements QueryRunner {

try {
const queryStartTime = +new Date()
await this.connect()
let rawResult:
| [
any[],
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down