modified db con for better connection handling mssql
This commit is contained in:
parent
378b9adf4d
commit
03fc743eb4
@ -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<Connection> {
|
||||
async getConnection(): Promise<ConnectionPool> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -19,11 +19,7 @@ export class MssqlService {
|
||||
} catch (error) {
|
||||
console.error('Error checking connection:', error);
|
||||
return {error:error.message}
|
||||
} finally {
|
||||
if (connection) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user