import { createPool, Pool, Connection } from 'oracledb'; import { ConnectionPool } from 'mssql'; import { Injectable, Logger } from '@nestjs/common'; import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { ConfigService } from '@nestjs/config'; import { getMssqlConfig, getOracleConfig } from './db.config'; @Injectable() export class OracleDBService { private pool: Pool | null = null; private readonly logger = new Logger(OracleDBService.name); constructor(private readonly configService: ConfigService) { this.initializePool().catch(err => { this.logger.error('Error initializing Oracle DB pool', err); throw new InternalServerException(); }); } private async initializePool(): Promise { try { const config = getOracleConfig(this.configService); this.pool = await createPool({ ...config, poolMin: 1, poolMax: 10, poolIncrement: 1, }); this.logger.log('Oracle connection pool created successfully'); } catch (error) { this.logger.error('Failed to create Oracle DB pool:', error); throw error; } } async getConnection(): Promise { if (!this.pool) { this.logger.error('Attempted to get a connection before pool was initialized'); throw new InternalServerException('Database connection pool not initialized'); } try { return await this.pool.getConnection(); } catch (error) { this.logger.error('Failed to get Oracle DB connection:', error); throw error; } } async closePool(): Promise { if (this.pool) { try { await this.pool.close(10); this.logger.log('Oracle DB pool closed successfully'); } catch (error) { this.logger.error('Error closing Oracle DB pool:', error); throw new InternalServerException(); } } } } @Injectable() export class MssqlDBService { private pool: ConnectionPool; private poolConnected = false; constructor(private readonly configService: ConfigService) { const config: any = getMssqlConfig(configService); this.pool = new ConnectionPool({ ...config, pool: { max: 10, min: 0, idleTimeoutMillis: 30000, }, }); } async getConnection(): Promise { try { if (!this.poolConnected) { this.pool.on('error', err => { console.error('SQL Pool Error:', err); this.poolConnected = false; }); await this.pool.connect(); this.poolConnected = true; } return this.pool; } catch (error) { console.error('Failed to get connection from pool:', error); throw error; } } }