I’m facing timeout issues in several functions of my backend application, such as confirm-email, start-exam and get-genders. After reviewing the logs, we suspect the issue may be related to the database connection pool configuration.
Oracle
ORM/Driver: TypeORM
poolMin: 2
poolMax: 5
import 'dotenv/config'
import 'reflect-metadata'
import OracleDB from 'oracledb'
import path from 'path'
import { DataSource } from 'typeorm'
if (['development', 'test'].includes(process.env.NODE_ENV!)) {
OracleDB.initOracleClient({ libDir: path.resolve(__dirname, './oracledb') })
}
const createDataSource = () => {
return new DataSource({
type: 'oracle',
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
connectString: process.env.DB_CONNECT_STRING,
synchronize: false,
logging: ['development', 'test'].includes(process.env.NODE_ENV!),
entities: [path.resolve(__dirname, '../models/**/*{.ts,.js}')],
migrations: [],
subscribers: [],
extra: {
poolMin: Number(process.env.DB_POOL_MIN),
poolMax: Number(process.env.DB_POOL_MAX)
}
})
}
export let DB = createDataSource()
export class DatabaseService {
private static instance: DatabaseService
private reconnectTimeout: NodeJS.Timeout | null = null
private healthCheckInterval: NodeJS.Timeout | null = null
private currentReconnectInterval = 1000 // Start with 1 second
private readonly MAX_RECONNECT_INTERVAL = 10000 // 10 seconds
private readonly HEALTH_CHECK_INTERVAL = 10000 // 10 seconds
public static getInstance(): DatabaseService {
if (!DatabaseService.instance) {
DatabaseService.instance = new DatabaseService()
}
return DatabaseService.instance
}
public async initialize(): Promise<void> {
try {
await DB.initialize()
console.log('Database connection established')
this.startHealthCheck()
} catch (error) {
console.error('Failed to connect to database:', error)
this.handleConnectionError()
}
}
private startHealthCheck(): void {
if (this.healthCheckInterval) {
clearInterval(this.healthCheckInterval)
}
this.healthCheckInterval = setInterval(async () => {
try {
await DB.query('SELECT 1 FROM DUAL')
} catch (error) {
console.error('Health check failed:', error)
this.handleConnectionError()
}
}, this.HEALTH_CHECK_INTERVAL)
}
private handleConnectionError(): void {
console.log('Database connection lost. Attempting to reconnect...')
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout)
}
if (this.healthCheckInterval) {
clearInterval(this.healthCheckInterval)
}
this.reconnectTimeout = setTimeout(async () => {
try {
if (DB.isInitialized) {
DB = createDataSource()
}
await DB.initialize()
console.log('Database connection established')
this.startHealthCheck()
this.currentReconnectInterval = 1000
} catch (error) {
console.error('Reconnection attempt failed:', error)
this.currentReconnectInterval = Math.min(this.currentReconnectInterval * 2, this.MAX_RECONNECT_INTERVAL)
this.handleConnectionError()
}
}, this.currentReconnectInterval)
}
}
How to fix the timeOuts in my application?
New contributor
Rodrigo Rodrigues is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3