diff --git a/src/db/db.service.ts b/src/db/db.service.ts index b1f8c60..23cd3d9 100644 --- a/src/db/db.service.ts +++ b/src/db/db.service.ts @@ -29,6 +29,8 @@ export class OracleDBService { @Injectable() export class MssqlDBService { private pool: ConnectionPool; + private poolConnected: boolean = false; + private readonly config = { ...MssqlConfig, pool: { @@ -39,22 +41,25 @@ export class MssqlDBService { }; constructor() { - this.initializePool(); - } - - private initializePool() { this.pool = new ConnectionPool(this.config); } - async getConnection(): Promise { + async getConnection(): Promise { try { - const connection = await this.pool.connect(); - console.log('Database connection initialized successfully for Mssql.'); - // console.log(`Connection established to server: ${this.config.server}`); // Log server info - return connection; + if (!this.poolConnected) { + this.pool.on('error', err => { + console.error('SQL Pool Error:', err); + this.poolConnected = false; + }); + + await this.pool.connect(); + this.poolConnected = true; + console.log('Database connection pool initialized.'); + } + return this.pool; } catch (error) { - console.error('Error getting connection from pool', error.stack); - throw error; // Rethrow the error after logging + console.error('Failed to get connection from pool:', error); + throw error; } } } diff --git a/src/mssql/manage-clients/manage-clients.service.ts b/src/mssql/manage-clients/manage-clients.service.ts index 102e7f1..997d7d6 100644 --- a/src/mssql/manage-clients/manage-clients.service.ts +++ b/src/mssql/manage-clients/manage-clients.service.ts @@ -114,11 +114,7 @@ export class ManageClientsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; GetPreparerByClientid = async (body: GetPreparerByClientidContactsByClientidLocByClientidDTO) => { @@ -132,11 +128,7 @@ export class ManageClientsService { return { data: result.recordset }; } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; UpdateClient = async (body: UpdateClientDTO) => { @@ -193,11 +185,7 @@ export class ManageClientsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; CreateClientContact = async (body: CreateAdditionalClientContactsDTO) => { @@ -270,11 +258,7 @@ export class ManageClientsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; UpdateClientContacts = async (body: UpdateClientContactsDTO) => { @@ -329,11 +313,7 @@ export class ManageClientsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; @@ -387,11 +367,7 @@ export class ManageClientsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; @@ -463,11 +439,7 @@ export class ManageClientsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; @@ -518,11 +490,7 @@ export class ManageClientsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; @@ -571,11 +539,7 @@ export class ManageClientsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; @@ -623,11 +587,7 @@ export class ManageClientsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; } diff --git a/src/mssql/manage-holders/manage-holders.service.ts b/src/mssql/manage-holders/manage-holders.service.ts index 96e609d..7b2a6a7 100644 --- a/src/mssql/manage-holders/manage-holders.service.ts +++ b/src/mssql/manage-holders/manage-holders.service.ts @@ -97,11 +97,7 @@ export class ManageHoldersService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; @@ -118,11 +114,7 @@ export class ManageHoldersService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; UpdateHolder = async (body: UpdateHolderDTO) => { @@ -154,11 +146,7 @@ export class ManageHoldersService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; UpdateHolderContact = async (body: UpdateHolderContactDTO) => { @@ -186,11 +174,7 @@ export class ManageHoldersService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; GetHolderContacts = async (body: GetHolderDTO) => { @@ -207,11 +191,7 @@ export class ManageHoldersService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; InactivateHolder = async (body: HolderActivateOrInactivateDTO) => { @@ -230,11 +210,7 @@ export class ManageHoldersService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; ReactivateHolder = async (body: HolderActivateOrInactivateDTO) => { @@ -251,11 +227,7 @@ export class ManageHoldersService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; InactivateHolderContact = async (body: HolderContactActivateOrInactivateDTO) => { @@ -272,11 +244,7 @@ export class ManageHoldersService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; ReactivateHolderContact = async (body: HolderContactActivateOrInactivateDTO) => { @@ -293,10 +261,6 @@ export class ManageHoldersService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } }; } diff --git a/src/mssql/mssql.service.ts b/src/mssql/mssql.service.ts index 309b8e7..ac362eb 100644 --- a/src/mssql/mssql.service.ts +++ b/src/mssql/mssql.service.ts @@ -19,11 +19,7 @@ export class MssqlService { } catch (error) { console.error('Error checking connection:', error); return {error:error.message} - } finally { - if (connection) { - connection.close(); - } - } + } } } diff --git a/src/mssql/param-table/param-table.service.ts b/src/mssql/param-table/param-table.service.ts index 2ee357d..94f70cb 100644 --- a/src/mssql/param-table/param-table.service.ts +++ b/src/mssql/param-table/param-table.service.ts @@ -34,11 +34,7 @@ export class ParamTableService { return { data: result.recordset }; } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } @@ -53,11 +49,7 @@ export class ParamTableService { return { data: result.recordset }; } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async CREATEPARAMRECORD(body: CreateParamRecordDTO) { @@ -80,11 +72,7 @@ export class ParamTableService { return { data: result.recordset }; } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async UPDATEPARAMRECORD(body: UpdateParamRecordDTO) { @@ -106,11 +94,7 @@ export class ParamTableService { return { data: result.recordset }; } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async INACTIVATEPARAMRECORD(body: ActivateOrInactivateParamRecordDTO) { @@ -125,11 +109,7 @@ export class ParamTableService { return { data: result.recordset }; } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async REACTIVATEPARAMRECORD(body: ActivateOrInactivateParamRecordDTO) { @@ -143,10 +123,6 @@ export class ParamTableService { return { data: result.recordset }; } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } } diff --git a/src/mssql/uscib-managed-sp/carnet-sequence/carnet-sequence.service.ts b/src/mssql/uscib-managed-sp/carnet-sequence/carnet-sequence.service.ts index 39804d7..987de46 100644 --- a/src/mssql/uscib-managed-sp/carnet-sequence/carnet-sequence.service.ts +++ b/src/mssql/uscib-managed-sp/carnet-sequence/carnet-sequence.service.ts @@ -21,11 +21,7 @@ export class CarnetSequenceService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async createCarnetSequence(body: CreateCarnetSequenceDTO) { @@ -44,10 +40,6 @@ export class CarnetSequenceService { } catch (error) { console.error('Error executing stored procedure:', error); return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } } diff --git a/src/mssql/uscib-managed-sp/region/region.service.ts b/src/mssql/uscib-managed-sp/region/region.service.ts index 97adc41..1b25feb 100644 --- a/src/mssql/uscib-managed-sp/region/region.service.ts +++ b/src/mssql/uscib-managed-sp/region/region.service.ts @@ -18,12 +18,10 @@ export class RegionService { return { data: result.recordset }; } catch (error) { + console.log("Error is here ..."); + return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async insetNewRegions(body: InsertRegionsDto) { @@ -39,11 +37,7 @@ export class RegionService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async updateRegions(body: UpdateRegionDto){ @@ -58,10 +52,6 @@ export class RegionService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } } diff --git a/src/mssql/uscib-managed-sp/sp-contacts/sp-contacts.service.ts b/src/mssql/uscib-managed-sp/sp-contacts/sp-contacts.service.ts index e9e376b..cee6478 100644 --- a/src/mssql/uscib-managed-sp/sp-contacts/sp-contacts.service.ts +++ b/src/mssql/uscib-managed-sp/sp-contacts/sp-contacts.service.ts @@ -21,11 +21,7 @@ export class SpContactsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async getSPDefaultcontacts(body: getSPDefaultcontactDTO) { @@ -40,11 +36,7 @@ export class SpContactsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async inactivateSPContact(body: inactivateSPContactDTO) { @@ -59,11 +51,7 @@ export class SpContactsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async insertSPContacts(body: InsertSPContactsDTO) { @@ -87,11 +75,7 @@ export class SpContactsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async setSPDefaultcontact(body: setSPDefaultcontactDTO) { @@ -105,11 +89,7 @@ export class SpContactsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async updateSPContacts(body: UpdateSPContactsDTO) { @@ -132,10 +112,6 @@ export class SpContactsService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } } diff --git a/src/mssql/uscib-managed-sp/sp/sp.service.ts b/src/mssql/uscib-managed-sp/sp/sp.service.ts index 2d882e2..2577200 100644 --- a/src/mssql/uscib-managed-sp/sp/sp.service.ts +++ b/src/mssql/uscib-managed-sp/sp/sp.service.ts @@ -20,11 +20,7 @@ export class SpService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async getSpBySpid(body: getSelectedServiceproviderDTO) { @@ -39,11 +35,7 @@ export class SpService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async insertNewServiceProvider(body: InsertNewServiceProviderDTO) { @@ -72,11 +64,7 @@ export class SpService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } async updateServiceProvider(body: UpdateServiceProviderDTO) { @@ -106,10 +94,6 @@ export class SpService { } catch (error) { return { error: error.message }; - } finally { - if (connection) { - connection.close(); - } - } + } } }