fix(Firebird SQL): connection pool issue

This commit is contained in:
Fabio Di Stasio 2022-11-16 10:12:44 +01:00
parent 1b5cc315dd
commit 7ff8e2149e
4 changed files with 40 additions and 16 deletions

View File

@ -37,7 +37,7 @@ export const customizations: Customizations = {
// Structure // Structure
schemas: false, schemas: false,
tables: true, tables: true,
views: false, views: true,
triggers: true, triggers: true,
routines: false, routines: false,
functions: false, functions: false,

View File

@ -65,7 +65,6 @@ export const customizations: Customizations = {
unsigned: true, unsigned: true,
nullable: true, nullable: true,
zerofill: true, zerofill: true,
tableOptions: true,
autoIncrement: true, autoIncrement: true,
comment: true, comment: true,
collation: true, collation: true,

View File

@ -97,7 +97,7 @@ export default (connections: {[key: string]: antares.Client}) => {
ipcMain.handle('get-engines', async (event, uid) => { ipcMain.handle('get-engines', async (event, uid) => {
try { try {
const result: any = await connections[uid].getEngines(); const result: unknown = await connections[uid].getEngines();
return { status: 'success', response: result }; return { status: 'success', response: result };
} }

View File

@ -9,7 +9,7 @@ export class FirebirdSQLClient extends AntaresCore {
private _schema?: string; private _schema?: string;
private _runningConnections: Map<string, number>; private _runningConnections: Map<string, number>;
private _connectionsToCommit: Map<string, firebird.Transaction>; private _connectionsToCommit: Map<string, firebird.Transaction>;
protected _connection?: firebird.Database; protected _connection?: firebird.Database | firebird.ConnectionPool;
_params: firebird.Options; _params: firebird.Options;
private _types: {[key: number]: string} ={ private _types: {[key: number]: string} ={
@ -92,7 +92,7 @@ export class FirebirdSQLClient extends AntaresCore {
if (!this._poolSize) if (!this._poolSize)
this._connection = await this.getConnection(); this._connection = await this.getConnection();
else else
this._connection = await this.getConnectionPool(); this._connection = this.getConnectionPool();
} }
async getConnection () { async getConnection () {
@ -104,18 +104,21 @@ export class FirebirdSQLClient extends AntaresCore {
}); });
} }
async getConnectionPool () { getConnectionPool () {
const pool = firebird.pool(this._poolSize, { ...this._params, blobAsText: true }); const pool = firebird.pool(this._poolSize, { ...this._params, blobAsText: true });
return new Promise<firebird.Database>((resolve, reject) => { // return new Promise<firebird.Database>((resolve, reject) => {
pool.get((err, db) => { // pool.get((err, db) => {
if (err) reject(err); // if (err) reject(err);
else resolve(db); // else resolve(db);
}); // });
}); // });
return pool;
} }
destroy () { destroy () {
return this._connection.detach(); if (this._poolSize)
return (this._connection as firebird.ConnectionPool).destroy();
} }
use (): void { use (): void {
@ -162,7 +165,14 @@ export class FirebirdSQLClient extends AntaresCore {
AND RDB$RELATION_TYPE = 0 AND RDB$RELATION_TYPE = 0
`); `);
tablesArr.push(...tables); const { rows: views } = await this.raw<antares.QueryResult<ShowTableResult>>(`
SELECT
DISTINCT RDB$VIEW_NAME AS name,
'view' AS type
FROM RDB$VIEW_RELATIONS
`);
tablesArr.push(...tables, ...views);
const { rows: triggers } = await this.raw<antares.QueryResult<ShowTriggersResult>>(` const { rows: triggers } = await this.raw<antares.QueryResult<ShowTriggersResult>>(`
SELECT SELECT
@ -821,12 +831,14 @@ export class FirebirdSQLClient extends AntaresCore {
: [sql]; : [sql];
let connection: firebird.Database | firebird.Transaction; let connection: firebird.Database | firebird.Transaction;
const isPool = this._poolSize;
if (!args.autocommit && args.tabUid) { // autocommit OFF if (!args.autocommit && args.tabUid) { // autocommit OFF
if (this._connectionsToCommit.has(args.tabUid)) if (this._connectionsToCommit.has(args.tabUid))
connection = this._connectionsToCommit.get(args.tabUid); connection = this._connectionsToCommit.get(args.tabUid);
else { else {
connection = await this.getConnection(); connection = await this.getConnection();
const transaction = await new Promise<firebird.Transaction>((resolve, reject) => { const transaction = await new Promise<firebird.Transaction>((resolve, reject) => {
(connection as firebird.Database).transaction(firebird.ISOLATION_READ_COMMITED, (err, transaction) => { (connection as firebird.Database).transaction(firebird.ISOLATION_READ_COMMITED, (err, transaction) => {
if (err) reject(err); if (err) reject(err);
@ -837,8 +849,19 @@ export class FirebirdSQLClient extends AntaresCore {
this._connectionsToCommit.set(args.tabUid, transaction); this._connectionsToCommit.set(args.tabUid, transaction);
} }
} }
else// autocommit ON else { // autocommit ON
connection = this._connection; if (isPool) {
const pool = this._connection as firebird.ConnectionPool;
connection = await new Promise<firebird.Database>((resolve, reject) => {
pool.get((err, db) => {
if (err) reject(err);
else resolve(db);
});
});
}
else
connection = this._connection as firebird.Database;
}
for (const query of queries) { for (const query of queries) {
if (!query) continue; if (!query) continue;
@ -993,6 +1016,8 @@ export class FirebirdSQLClient extends AntaresCore {
resultsArr.push({ rows, report, fields, keys, duration }); resultsArr.push({ rows, report, fields, keys, duration });
} }
(connection as firebird.Database).detach();
const result = resultsArr.length === 1 ? resultsArr[0] : resultsArr; const result = resultsArr.length === 1 ? resultsArr[0] : resultsArr;
return result as unknown as T; return result as unknown as T;