Skip to content

Conversation

@alumni
Copy link
Collaborator

@alumni alumni commented Oct 1, 2025

Description of change

This PR adds support for Explicit Resource Management in QueryRunner. See more:

The QueryRunner will be released automatically at the end of its scope:

async function updateSalaries() {
    await using queryRunner = dataSource.createQueryRunner();
    await queryRunner.manager.update(Employee, { level: "junior" }, { bonus: 0.2 });
    await queryRunner.manager.update(Employee, { level: "senior" }, { bonus: 0.1 });
    // no need to manually release the QueryRunner
}

Pull-Request Checklist

  • Code is up-to-date with the next branch
  • This pull request links relevant issues as Fixes #00000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 1, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 1, 2025

typeorm-sql-js-example

npm i https://pkg.pr.new/typeorm/typeorm@11701

commit: ee1b60d

@coveralls
Copy link

coveralls commented Oct 2, 2025

Coverage Status

coverage: 76.418% (+0.006%) from 76.412%
when pulling ee1b60d on alumni:feat/query-runner-async-dispose
into ea7a99a on typeorm:next.

@alumni alumni force-pushed the feat/query-runner-async-dispose branch 2 times, most recently from c6ccf7d to dcb6e1c Compare October 2, 2025 10:18
@alumni alumni force-pushed the feat/query-runner-async-dispose branch from dcb6e1c to efa732d Compare October 2, 2025 10:24
@gioboa
Copy link
Collaborator

gioboa commented Oct 2, 2025

Thanks for this lovely feature @alumni

@alumni
Copy link
Collaborator Author

alumni commented Oct 2, 2025

@gioboa What do you think about the transactions that are in progress in that QueryRunner that is being disposed?

Right now, we only call QueryRunner.release(), i.e. return the client connection to the pool, at least in most drivers. Not all have a pool: SQLite just flushes the data to the disk.

Not all drivers support a transaction (e.g. MongoDB, some of the SQLite drivers), but for those that do, we could have a transaction in progress. So I was wondering, should we commit the transaction? Should we roll it back if not explicitly committed?

Also, some databases have save points ("nested transactions", e.g. Postgres) - how should those be handled? Should they all be committed?

I'm thinking to add:

async [Symbol.asyncDispose](): Promise<void> {
    try {
        while (this.isTransactionActive) {
            await this.commitTransaction();
        }
    } finally {
        await this.release()
    }
}

@gioboa
Copy link
Collaborator

gioboa commented Oct 2, 2025

That's a hard decision, but I think your suggestion is a good one. We can probably have a voting session with the maintainer team to agree with this.

@alumni
Copy link
Collaborator Author

alumni commented Oct 2, 2025

Okay, I think I figured out that we don't need to release all savepoints, we can simply commit once. Should work in most drivers based on my understanding. Pushing a commit now.

@alumni alumni force-pushed the feat/query-runner-async-dispose branch from 78c5e63 to ee1b60d Compare October 2, 2025 18:21
@alumni alumni merged commit bcee921 into typeorm:next Oct 2, 2025
87 of 90 checks passed
@alumni alumni mentioned this pull request Dec 3, 2025
26 tasks
@alumni alumni added this to the Next Major milestone Dec 3, 2025
alumni added a commit to alumni/typeorm that referenced this pull request Dec 12, 2025
)

* feat: support explicit resource management in QueryRunner

* test: improve tests for explicit resource management

* feat: commit transaction on QueryRunner dispose
mgohin pushed a commit to mgohin/typeorm that referenced this pull request Jan 15, 2026
)

* feat: support explicit resource management in QueryRunner

* test: improve tests for explicit resource management

* feat: commit transaction on QueryRunner dispose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants