Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nested transactions issues #10210

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: "3"
services:
# mysql
mysql:
platform: linux/amd64
image: "mysql:5.7.37"
container_name: "typeorm-mysql"
ports:
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

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

15 changes: 9 additions & 6 deletions src/driver/aurora-mysql/AuroraMysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ export class AuroraMysqlQueryRunner
}

if (this.transactionDepth === 0) {
this.transactionDepth += 1
await this.client.startTransaction()
} else {
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth - 1}`)
}
this.transactionDepth += 1

await this.broadcaster.broadcast("AfterTransactionStart")
}
Expand All @@ -118,14 +119,15 @@ export class AuroraMysqlQueryRunner
await this.broadcaster.broadcast("BeforeTransactionCommit")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`RELEASE SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.client.commitTransaction()
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionCommit")
}
Expand All @@ -140,14 +142,15 @@ export class AuroraMysqlQueryRunner
await this.broadcaster.broadcast("BeforeTransactionRollback")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.client.rollbackTransaction()
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionRollback")
}
Expand Down
15 changes: 9 additions & 6 deletions src/driver/aurora-postgres/AuroraPostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ export class AuroraPostgresQueryRunner
}

if (this.transactionDepth === 0) {
this.transactionDepth += 1
await this.client.startTransaction()
} else {
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth} - 1`)
}
this.transactionDepth += 1

await this.broadcaster.broadcast("AfterTransactionStart")
}
Expand All @@ -127,14 +128,15 @@ export class AuroraPostgresQueryRunner
await this.broadcaster.broadcast("BeforeTransactionCommit")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`RELEASE SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.client.commitTransaction()
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionCommit")
}
Expand All @@ -149,14 +151,15 @@ export class AuroraPostgresQueryRunner
await this.broadcaster.broadcast("BeforeTransactionRollback")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.client.rollbackTransaction()
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionRollback")
}
Expand Down
16 changes: 9 additions & 7 deletions src/driver/cockroachdb/CockroachQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export class CockroachQueryRunner
}

if (this.transactionDepth === 0) {
this.transactionDepth += 1
await this.query("START TRANSACTION")
await this.query("SAVEPOINT cockroach_restart")
if (isolationLevel) {
Expand All @@ -197,10 +198,10 @@ export class CockroachQueryRunner
)
}
} else {
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth - 1}`)
}

this.transactionDepth += 1
this.storeQueries = true

await this.broadcaster.broadcast("AfterTransactionStart")
Expand All @@ -216,18 +217,18 @@ export class CockroachQueryRunner
await this.broadcaster.broadcast("BeforeTransactionCommit")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`RELEASE SAVEPOINT typeorm_${this.transactionDepth}`,
)
this.transactionDepth -= 1
} else {
this.storeQueries = false
this.transactionDepth -= 1
await this.query("RELEASE SAVEPOINT cockroach_restart")
await this.query("COMMIT")
this.queries = []
this.isTransactionActive = false
this.transactionRetries = 0
this.transactionDepth -= 1
}

await this.broadcaster.broadcast("AfterTransactionCommit")
Expand All @@ -243,17 +244,18 @@ export class CockroachQueryRunner
await this.broadcaster.broadcast("BeforeTransactionRollback")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.storeQueries = false
this.transactionDepth -= 1
await this.query("ROLLBACK")
this.queries = []
this.isTransactionActive = false
this.transactionRetries = 0
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionRollback")
}
Expand Down
15 changes: 9 additions & 6 deletions src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,17 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
throw err
}
if (this.transactionDepth === 0) {
this.transactionDepth += 1
if (isolationLevel) {
await this.query(
"SET TRANSACTION ISOLATION LEVEL " + isolationLevel,
)
}
await this.query("START TRANSACTION")
} else {
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth - 1}`)
}
this.transactionDepth += 1

await this.broadcaster.broadcast("AfterTransactionStart")
}
Expand All @@ -142,14 +143,15 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
await this.broadcaster.broadcast("BeforeTransactionCommit")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`RELEASE SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.query("COMMIT")
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionCommit")
}
Expand All @@ -164,14 +166,15 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
await this.broadcaster.broadcast("BeforeTransactionRollback")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.query("ROLLBACK")
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionRollback")
}
Expand Down
10 changes: 6 additions & 4 deletions src/driver/oracle/OracleQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,14 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
}

if (this.transactionDepth === 0) {
this.transactionDepth += 1
await this.query(
"SET TRANSACTION ISOLATION LEVEL " + isolationLevel,
)
} else {
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth - 1}`)
}
this.transactionDepth += 1

await this.broadcaster.broadcast("AfterTransactionStart")
}
Expand Down Expand Up @@ -174,14 +175,15 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
await this.broadcaster.broadcast("BeforeTransactionRollback")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.query("ROLLBACK")
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionRollback")
}
Expand Down
15 changes: 9 additions & 6 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,17 @@ export class PostgresQueryRunner
}

if (this.transactionDepth === 0) {
this.transactionDepth += 1
await this.query("START TRANSACTION")
if (isolationLevel) {
await this.query(
"SET TRANSACTION ISOLATION LEVEL " + isolationLevel,
)
}
} else {
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth - 1}`)
}
this.transactionDepth += 1

await this.broadcaster.broadcast("AfterTransactionStart")
}
Expand All @@ -197,14 +198,15 @@ export class PostgresQueryRunner
await this.broadcaster.broadcast("BeforeTransactionCommit")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`RELEASE SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.query("COMMIT")
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionCommit")
}
Expand All @@ -219,14 +221,15 @@ export class PostgresQueryRunner
await this.broadcaster.broadcast("BeforeTransactionRollback")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.query("ROLLBACK")
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionRollback")
}
Expand Down
15 changes: 9 additions & 6 deletions src/driver/sqlite-abstract/AbstractSqliteQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export abstract class AbstractSqliteQueryRunner
}

if (this.transactionDepth === 0) {
this.transactionDepth += 1
if (isolationLevel) {
if (isolationLevel === "READ UNCOMMITTED") {
await this.query("PRAGMA read_uncommitted = true")
Expand All @@ -110,9 +111,9 @@ export abstract class AbstractSqliteQueryRunner
}
await this.query("BEGIN TRANSACTION")
} else {
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth - 1}`)
}
this.transactionDepth += 1

await this.broadcaster.broadcast("AfterTransactionStart")
}
Expand All @@ -127,14 +128,15 @@ export abstract class AbstractSqliteQueryRunner
await this.broadcaster.broadcast("BeforeTransactionCommit")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`RELEASE SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.query("COMMIT")
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionCommit")
}
Expand All @@ -149,14 +151,15 @@ export abstract class AbstractSqliteQueryRunner
await this.broadcaster.broadcast("BeforeTransactionRollback")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth}`,
)
} else {
this.transactionDepth -= 1
await this.query("ROLLBACK")
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionRollback")
}
Expand Down
Loading