create connection pool TypeOrm
Solution 1
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
}
}
]
MySQL Connection pooling options which can be passed under extra
, if required.
Solution 2
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.
Solution 3
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.
prranay
Updated on July 15, 2022Comments
-
prranay almost 2 years
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', }), }, ];
-
user2010955 over 3 yearsso 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?
-
user2010955 over 3 yearsso 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
-
Curtis Fonger almost 3 yearsThis 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 -
Alexy about 2 years1 year later, still not documented. Thanks you so much