Skip to content

Commit

Permalink
fx: Ensure own connection is always released even if exception occurs.
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerpadilla committed Sep 27, 2024
1 parent 8b288fe commit 17e5128
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ date format is [yyyy-mm-dd]

## [1.4.4] - 2024-09-26

- Ensure own connection is always released even if exception occurs.
- Correct issue when empty or null list is passed to `insertMany` operations.

## [1.4.3] - 2024-09-25
Expand Down
18 changes: 12 additions & 6 deletions packages/core/src/querier/abstractSqlQuerier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,23 @@ export abstract class AbstractSqlQuerier extends AbstractQuerier {
if (!this.hasPendingTransaction) {
throw TypeError('not a pending transaction');
}
await this.run('COMMIT');
this.hasPendingTransaction = undefined;
await this.release();
try {
await this.run('COMMIT');
} finally {
this.hasPendingTransaction = undefined;
await this.release();
}
}

override async rollbackTransaction() {
if (!this.hasPendingTransaction) {
throw TypeError('not a pending transaction');
}
await this.run('ROLLBACK');
this.hasPendingTransaction = undefined;
await this.release();
try {
await this.run('ROLLBACK');
} finally {
this.hasPendingTransaction = undefined;
await this.release();
}
}
}
20 changes: 13 additions & 7 deletions packages/maria/src/mariadbQuerier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ export class MariadbQuerier extends AbstractSqlQuerier {
override async all<T>(query: string) {
this.extra?.logger?.(query);
await this.lazyConnect();
const res: T[] = await this.conn.query(query);
await this.releaseIfFree();
return res.slice(0, res.length);
try {
const res: T[] = await this.conn.query(query);
return res.slice(0, res.length);
} finally {
await this.releaseIfFree();
}
}

override async run(query: string) {
this.extra?.logger?.(query);
await this.lazyConnect();
const res = await this.conn.query(query);
await this.releaseIfFree();
const ids = res.length ? res.map((r: any) => r.id) : [];
return { changes: res.affectedRows, ids, firstId: ids[0] } satisfies QueryUpdateResult;
try {
const res = await this.conn.query(query);
const ids = res.length ? res.map((r: any) => r.id) : [];
return { changes: res.affectedRows, ids, firstId: ids[0] } satisfies QueryUpdateResult;
} finally {
await this.releaseIfFree();
}
}

async lazyConnect() {
Expand Down
28 changes: 17 additions & 11 deletions packages/mysql/src/mysql2Querier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,28 @@ export class MySql2Querier extends AbstractSqlQuerier {
override async all<T>(query: string) {
this.extra?.logger?.(query);
await this.lazyConnect();
const [res] = await this.conn.query(query);
await this.releaseIfFree();
return res as T[];
try {
const [res] = await this.conn.query(query);
return res as T[];
} finally {
await this.releaseIfFree();
}
}

override async run(query: string) {
this.extra?.logger?.(query);
await this.lazyConnect();
const [res]: any = await this.conn.query(query);
await this.releaseIfFree();
const ids = res.insertId
? Array(res.affectedRows)
.fill(res.insertId)
.map((i, index) => i + index)
: [];
return { changes: res.affectedRows, ids, firstId: ids[0] } satisfies QueryUpdateResult;
try {
const [res]: any = await this.conn.query(query);
const ids = res.insertId
? Array(res.affectedRows)
.fill(res.insertId)
.map((i, index) => i + index)
: [];
return { changes: res.affectedRows, ids, firstId: ids[0] } satisfies QueryUpdateResult;
} finally {
await this.releaseIfFree();
}
}

async lazyConnect() {
Expand Down
20 changes: 13 additions & 7 deletions packages/postgres/src/pgQuerier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ export class PgQuerier extends AbstractSqlQuerier {
override async all<T>(query: string) {
this.extra?.logger?.(query);
await this.lazyConnect();
const res = await this.conn.query<T>(query);
await this.releaseIfFree();
return res.rows;
try {
const res = await this.conn.query<T>(query);
return res.rows;
} finally {
await this.releaseIfFree();
}
}

override async run(query: string) {
this.extra?.logger?.(query);
await this.lazyConnect();
const { rowCount: changes, rows = [] }: any = await this.conn.query(query);
await this.releaseIfFree();
const ids = rows.map((r: any) => r.id);
return { changes, ids, firstId: ids[0] } satisfies QueryUpdateResult;
try {
const { rowCount: changes, rows = [] }: any = await this.conn.query(query);
const ids = rows.map((r: any) => r.id);
return { changes, ids, firstId: ids[0] } satisfies QueryUpdateResult;
} finally {
await this.releaseIfFree();
}
}

async lazyConnect() {
Expand Down

0 comments on commit 17e5128

Please sign in to comment.