11

How to create a connection pool using TypeOrm? While exploring TypeOrm, I wanted to create pool of connections for working with MySql

Below is the code snippet :

import { createConnection } from 'typeorm';

export const databaseProviders = [
  {
    provide: 'DbConnectionToken',
    useFactory: async () => await createConnection({
      type: 'mysql',
      host: 'localhost',
      port: 8889,
      username: 'root',
      password: 'root',
      database: 'typeorm_test',
      entities: [
        __dirname + '/../**/**.entity{.ts,.js}',
      ],
      autoSchemaSync: true,
      logging: 'all',
    }),
  },
];
0

4 Answers 4

29

TypeORM by default uses a connection pool which defaults to 10 connections. If you want to have custom pooling limit (advisable), the same can be mentioned for connectionLimit under extra options which are passed to the underlying MySQL driver.

 [
    {
        "name": "default",
        "type": "mysql",
        "host": "mysql.db",
        "port": 3306,
        "username": "appUser",
        "password": "appRandomPassword",
        "database": "entity_schema",
        "entities": [
            "dist/models/entities/**/*.js"
        ],
        "logging": [
            "error"
        ],
        "extra": {
            "connectionLimit": 5
        }
    }
]

TypeORM Docs

MySQL Connection pooling options which can be passed under extra, if required.

4
  • so meaning that if I have 10 clients running a 'long' query, the next concurrent clients will be in a queue waiting for an available connection? Commented Aug 25, 2020 at 22:16
  • so queueLimit = 0 meaning unlimited queue, so the 11th concurrent client will be in a queue unless one of the previous 10 clients has done, and its connection to the db can be assigned to the 11th, is it correct? :) thanks Commented Aug 26, 2020 at 7:49
  • doc link is expired. in their current docs can't find anywhere about default connections
    – Charan Sai
    Commented Apr 2, 2023 at 12:14
  • Adjusted the link. Commented Apr 16, 2023 at 10:13
5

TypeORM always creates you a connection pool out of the box, you don't need to setup anything. It uses one connection from the pool per one request to repository/entity manager method, or per one transaction.

4

Starting with version 0.3.10 there is a unified property to handle the connection pool maximum size, by using poolSize: number in your connection configuration.

Your config should now be something like

import { createConnection } from 'typeorm';

export const databaseProviders = [
  {
    provide: 'DbConnectionToken',
    useFactory: async () => await createConnection({
      type: 'mysql',
      host: 'localhost',
      port: 8889,
      username: 'root',
      password: 'root',
      database: 'typeorm_test',
      entities: [
        __dirname + '/../**/**.entity{.ts,.js}',
      ],
      autoSchemaSync: true,
      logging: 'all',
      poolSize: 10,
    }),
  },
];
1
  • for some reason it doesn't show a valid option in PostgresConnectionOptions
    – mleko
    Commented Nov 22, 2022 at 6:07
1

You need to pass an extra arguments to underlying database. From the official doc it looks like in typeorm, only mssql and mongo that support connection pool.

4
  • 2
    This is true, however you can still set connection limits for other databases by passing the correct (undocumented) options. For example with Postgres, you can pass extra: { max: 10 } to set the pool size to 10. See this for reference: PostgresDriver.ts and pg-pool/index.js Commented Jul 1, 2021 at 23:48
  • 1 year later, still not documented. Thanks you so much
    – Alexy
    Commented Mar 29, 2022 at 6:13
  • doesn't work in pg
    – juztcode
    Commented Dec 26, 2022 at 4:21
  • when I set this poolSize, and bombard the database with requests, the active connection actually exceeds to 500, 600.... and so definitely think it doesn't work in pg
    – mleko
    Commented Feb 2, 2023 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.