Skip to content

Commit

Permalink
Revert "fix: nested transactions issues (typeorm#10210)"
Browse files Browse the repository at this point in the history
This reverts commit 25e6ecd.
  • Loading branch information
alumni committed Jan 27, 2025
1 parent 79960e1 commit 1c32e10
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 289 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ version: "3"
services:
# mysql
mysql:
platform: linux/amd64
image: "mysql:5.7.37"
container_name: "typeorm-mysql"
ports:
Expand Down
15 changes: 6 additions & 9 deletions src/driver/aurora-mysql/AuroraMysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ export class AuroraMysqlQueryRunner
}

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

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

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

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

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

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

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

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

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

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

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

await this.broadcaster.broadcast("AfterTransactionRollback")
}
Expand Down
20 changes: 8 additions & 12 deletions src/driver/cockroachdb/CockroachQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ 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 @@ -199,10 +198,10 @@ export class CockroachQueryRunner
)
}
} else {
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth - 1}`)
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
}

this.transactionDepth += 1
this.storeQueries = true

await this.broadcaster.broadcast("AfterTransactionStart")
Expand All @@ -218,20 +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}`,
`RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`,
)
this.transactionDepth -= 1
} else {
this.storeQueries = false
this.transactionDepth -= 1
// This was disabled because it failed tests after update to CRDB 24.2
// https://github.com/typeorm/typeorm/pull/11190
// await this.query("RELEASE SAVEPOINT cockroach_restart")
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 @@ -247,18 +244,17 @@ export class CockroachQueryRunner
await this.broadcaster.broadcast("BeforeTransactionRollback")

if (this.transactionDepth > 1) {
this.transactionDepth -= 1
await this.query(
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
)
} 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: 6 additions & 9 deletions src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,16 @@ 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 {
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth - 1}`)
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
}
this.transactionDepth += 1

await this.broadcaster.broadcast("AfterTransactionStart")
}
Expand All @@ -144,15 +143,14 @@ 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}`,
`RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`,
)
} else {
this.transactionDepth -= 1
await this.query("COMMIT")
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionCommit")
}
Expand All @@ -167,15 +165,14 @@ 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}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
)
} else {
this.transactionDepth -= 1
await this.query("ROLLBACK")
this.isTransactionActive = false
}
this.transactionDepth -= 1

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

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

await this.broadcaster.broadcast("AfterTransactionStart")
}
Expand Down Expand Up @@ -176,15 +175,14 @@ 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}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
)
} else {
this.transactionDepth -= 1
await this.query("ROLLBACK")
this.isTransactionActive = false
}
this.transactionDepth -= 1

await this.broadcaster.broadcast("AfterTransactionRollback")
}
Expand Down
15 changes: 6 additions & 9 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,16 @@ 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 {
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth - 1}`)
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
}
this.transactionDepth += 1

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

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

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

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

await this.broadcaster.broadcast("AfterTransactionRollback")
}
Expand Down
15 changes: 6 additions & 9 deletions src/driver/sqlite-abstract/AbstractSqliteQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ 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 @@ -111,9 +110,9 @@ export abstract class AbstractSqliteQueryRunner
}
await this.query("BEGIN TRANSACTION")
} else {
this.transactionDepth += 1
await this.query(`SAVEPOINT typeorm_${this.transactionDepth - 1}`)
await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
}
this.transactionDepth += 1

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

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

await this.broadcaster.broadcast("AfterTransactionCommit")
}
Expand All @@ -151,15 +149,14 @@ 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}`,
`ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
)
} else {
this.transactionDepth -= 1
await this.query("ROLLBACK")
this.isTransactionActive = false
}
this.transactionDepth -= 1

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

0 comments on commit 1c32e10

Please sign in to comment.