Skip to content

TypeORM becomes unresponsive after this code is run 10x #4866

Closed

Description

@bengotow

Issue type:

[ ] question
[X] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[X] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ ] latest
[ ] @next
[X] 0.2.16 (or put your version here)

Steps to reproduce or a small repository showing the problem:

Hey! Ok - so I've finally narrowed this issue down to a reproducible case. I'm building an express application and one of my routes does this:


router.get('/testtest', async (req, res) => {
  const db = await dbconnection;
  const runner = db.createQueryRunner();
  await runner.startTransaction();
  // queries
  await runner.commitTransaction();
  return res.json({ success: true });
});

After running this 10x, (presumably the size of the default connection pool), TypeORM stops responding to all queries. On the 11th run, this code awaits forever and other code which uses Model.findOne(), etc. also locks.

Activity

  1. bengotow commented on Oct 6, 2019

    @bengotow
    Author

    Edit: After reading the docs more carefully, it turns out I was missing a call to runner.release() and this behavior was entirely appropriate!

    If anyone comes across this, I think I'm going to wrap my code into a function like this that binds the runner to a nested scope, ensuring it is not possible to forget the release. It's hard to identify that this is a problem in development because it doesn't cause issues until the 10th execution.

    async function inTransaction(cb: (runner: QueryRunner) => void) {
      const db = await dbconnection;
      const runner = db.createQueryRunner();
      await runner.startTransaction();
      try {
        await cb(runner);
        await runner.commitTransaction();
      } catch (err) {
        await runner.rollbackTransaction();
      } finally {
        runner.release();
      }
    }
    
  2. roger-oliver commented on Oct 26, 2020

    @roger-oliver

    It helped me a lot mate! Thanks!

  3. denes16 commented on Mar 12, 2022

    @denes16

    Im facing the same issue. Im using nestjs/mysql, im running a trasaction, but afer 8,9 o 10 request becomes unresponsive.
    Im releasing the query runner too.

  4. denes16 commented on Mar 12, 2022

    @denes16

    My code looks like this

    constructor(
        private connection: Connection,
      ) {}
    async create(
        createChatMessageDto: CreateChatMessageDto,
        currentUser: CurrentUser,
        file?: UploadedS3File,
      ) {
        const queryRunner = this.connection.createQueryRunner();
        await queryRunner.connect();
        await queryRunner.startTransaction();
        try {
    ...someCode
    await queryRunner.commitTransaction();
    await queryRunner.release();
    // send reponse to user
    } catch (error) {
          await queryRunner.rollbackTransaction();
          await queryRunner.release();
          throw error;
        }
    }
    
  5. added a commit that references this issue on May 5, 2022
    9e38b7a
  6. added a commit that references this issue on Jun 29, 2022
    ec05f1d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions