I’m using NestJs, and Typeorm (MySQL driver). From time to time I get the ‘Connection lost: The server closed the connection.’ error.
The server that I’m using closes the connection after 10-15 seconds of inactivity.
I tried different pool configurations but that didn’t help.
Here is my TypeOrm config:
TypeOrmModule.forRootAsync({
useFactory: async () => {
return {
type: 'mysql',
charset: 'utf8mb4',
extra: {
pool: {
createTimeoutMillis: 30000,
destroyTimeoutMillis: 5000,
reapIntervalMillis: 1000,
createRetryIntervalMillis: 200,
acquireTimeoutMillis: 30000,
idleTimeoutMillis: 10000,
},
},
logging: ['error', 'warn'],
};
},
}),
I also tried using a factory. The idea was to validate the connection before a query. But it also didn’t help
factory: {
validate: async (connection) => {
return new Promise((resolve, reject) => {
connection
.raw('SELECT 1 + 1 AS health_check')
.then(() => {
// Connection is healthy, resolve the promise
resolve(true);
})
.catch((error) => {
// Connection is invalid, reject the promise
reject(error);
});
});
},
},