#12913·typeorm

Explicit resource management transaction handling

Author: aattolaCreated Sep 30, 2026Updated Sep 30, 2026

Feature Description

It would be awesome that you could decide what happens to the transaction when transaction gets disposed. Currently it commits the transaction.

The Solution

Some toggle or switch to change the behavior. Default would be commit (like now) and option for rollback of transaction. Something like

typescript
await using queryRunner = this.dataSource.createQueryRunner()
queryRunner.transactionDisposeHandling = "rollback" // or commit. Commit would be default to match current functionality
await queryRunner.startTransaction()

await queryRunner.manager.getRepository(Users).save({id:1, name: "demo"})

if (validationFailure) {
	throw Error("something went wrong") // <-- would rollback instead of commit
}

// ....etc...

await queryRunner.commitTransaction()

Considered Alternatives

Manually handling error with try {} catch { await queryRunner.rollbackTransaction() }

But I think there should be an option for how dispose handles transactions.

Additional Context

I can create an PR if the api can be agreed on.

Currently the implementation looks like this

typescript
async [Symbol.asyncDispose](): Promise<void> {
    try {
        if (this.isTransactionActive) {
            this.transactionDepth = 1 // ignore all savepoints and commit directly
            await this.commitTransaction()
        }
    } finally {
        await this.release()
    }
}

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • capacitor
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don't know how to start. I would need guidance.