Skip to content

feat: support explicit resource management in QueryRunner - #11701

Merged
alumni merged 3 commits into
typeorm:nextfrom
alumni:feat/query-runner-async-dispose
Oct 2, 2025
Merged

alumni merged 3 commits into
typeorm:nextfrom
alumni:feat/query-runner-async-dispose

Conversation

@alumni

@alumni alumni commented Oct 1, 2025 •

Copy link
Copy Markdown
Collaborator

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

coderabbitai Bot commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

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

pkg-pr-new Bot commented Oct 1, 2025 •

Copy link
Copy Markdown

typeorm-sql-js-example

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

commit: ee1b60d

@coveralls

coveralls commented Oct 2, 2025 •

Copy link
Copy Markdown

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

gioboa commented Oct 2, 2025

Copy link
Copy Markdown
Collaborator

Thanks for this lovely feature @alumni

@alumni

alumni commented Oct 2, 2025

Copy link
Copy Markdown
Collaborator Author

@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

gioboa commented Oct 2, 2025 •

Copy link
Copy Markdown
Collaborator

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

alumni commented Oct 2, 2025 •

Copy link
Copy Markdown
Collaborator Author

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
@alumni alumni mentioned this pull request Dec 3, 2025
18 tasks done
@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
kranners pushed a commit to kranners/typeorm that referenced this pull request Mar 1, 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

Development

Successfully merging this pull request may close these issues.

3 participants