question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running simultaneous queries with relations stalls typeorm

See original GitHub issue

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 [x] @next [ ] 0.x.x (or put your version here)

I ran into an issue where typeorm would just stop responding occasionally, and when looking at the postgres stats I noticed that typeorm kept 10 (the size of the pool) connections open and in idle and didn’t seem to ever close them afterwards.

The connections showed up as Idle | WaitEvent: Client: ClientRead.

Some more investigations seem to indicate that it the issue presents itself when trying to find entities with relations (with the getManager().find(Entity, options) syntax).

Increasing the pool size can help mitigate the problem, and using the query builder works as a workaround, but is obviously something we’d want to avoid as much as possible in favour of shorter, and clearer syntax.

I don’t believe it’s a config issue either


let entities = ['./dist/api/src/entities/**/*.js'];

module.exports = {
  type: 'postgres',
  synchronize, // True locally
  ssl,
  host,
  database,
  username,
  password,
  entities,
  timezone: 'Z',
  logging,
  logger,
  maxQueryExecutionTime: 1000,
  extra: {
    poolSize, // 10
    idleTimeoutMillis: 5000, // Drop connections that are stalled
    connectionTimeoutMillis: 10000, // Drop connections that are stalled
  },

  cli: {
    entitiesDir: 'api/src/entities/**',
    migrationsDir: 'api/src/migration',
    subscribersDir: 'api/src/subscriber',
  },
};

Steps to reproduce or a small repository showing the problem:

/* eslint-disable @typescript-eslint/no-use-before-define */
import { createConnection, Entity, getManager, ManyToOne, OneToMany, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Entity2 {
  @PrimaryGeneratedColumn()
  id!: number;

  @ManyToOne(() => Entity1, ent1 => ent1.ent2)
  ent1!: Entity1;
}

@Entity()
export class Entity1 {
  @PrimaryGeneratedColumn()
  id!: number;

  @OneToMany(() => Entity2, ent2 => ent2.ent1)
  ent2!: Entity2[];
}

const getWithRelations = () => {
  getManager()
    .find(Entity1, { relations: { ent2: true } })
    .then(e => {
      console.log(e);
    });
};

const getSimple = () => {
  getManager()
    .find(Entity1)
    .then(console.log);
};

const getWithQb = () => {
  getManager()
    .getRepository(Entity1)
    .createQueryBuilder('ent')
    .leftJoinAndSelect('ent.ent2', 'ent2')
    .getMany()
    .then(console.log);
};

(async () => {
  await createConnection();

  const parent = await getManager().save(new Entity1());

  const child1 = new Entity2();
  child1.ent1 = parent;
  await getManager().save(child1);

  const child2 = new Entity2();
  child2.ent1 = parent;
  await getManager().save(child2);

  for (let i = 0; i < 35; i++) {
    getWithRelations(); // Will stall
  }

  for (let i = 0; i < 35; i++) {
    getSimple(); // Runs as expected
  }

  for (let i = 0; i < 35; i++) {
    getWithQb(); // Runs as expected
  }
})();

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:27
  • Comments:7

github_iconTop GitHub Comments

1reaction
mkeemoncommented, Aug 11, 2022

@Stene3 I have been running into the same issue using relationLoadStrategy: 'query'. The connection pool seems to be at the heart of the issue. I increased the max pool size to 50 against my local Postgres instance, and no longer run into the app locking up. Not a solution by any means, but helpful in debugging.

0reactions
Stene3commented, Jul 1, 2022

Same happenned to me. Seems like issue with relations loading when relationLoadStrategy of type query is used. Single queries works, but more concurrently connections cause typeorm, and most probably all connection in pool, to be stalled. Any idea what could be the reason? I was trying to debug but no luck yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeORM - Multiple DB Calls vs Single DB Call
The article demonstrates how we should select either multiple DB Calls or Single DB Call with TypeORM in Node.js, TypeScript.
Read more >
Typescript: performance problem with typeorm package
WEB-36600 WebStorm 2018.3 code analysis hangs at 100% CPU usage (typeorm) ... Most of the time the highlighting just stops. ... at java.util.concurrent....
Read more >
TypeORM: Supports MySQL, PostgreSQL, MariaDB, SQLite ...
In this tutorial, we will share a repository of TypeORM. It Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL...
Read more >
Working with Relations - typeorm - GitBook
RelationQueryBuilder is a special type of QueryBuilder which allows you to work with your relations. Using it, you can bind entities to each...
Read more >
Is it just me who doesn't agree with db first ORM model? - Reddit
With complex relations and complex queries. ... that ignoring the SQL that gets run is a problem, so now you have db-first ORM...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found