Skip to content

Commit 2d7942c

Browse files
committed
f
1 parent 9ae7675 commit 2d7942c

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/connection.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert';
12
import { promisify } from 'node:util';
23
import { Operator } from './operator';
34
import type { PoolConnectionPromisify } from './types';
@@ -6,8 +7,11 @@ const kWrapToRDS = Symbol('kWrapToRDS');
67

78
export class RDSConnection extends Operator {
89
conn: PoolConnectionPromisify;
10+
#released: boolean;
11+
912
constructor(conn: PoolConnectionPromisify) {
1013
super(conn);
14+
this.#released = false;
1115
this.conn = conn;
1216
if (!this.conn[kWrapToRDS]) {
1317
[
@@ -23,6 +27,8 @@ export class RDSConnection extends Operator {
2327
}
2428

2529
release() {
30+
assert(!this.#released, 'connection was released');
31+
this.#released = true;
2632
return this.conn.release();
2733
}
2834

src/transaction.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import type { RDSConnection } from './connection';
22
import { Operator } from './operator';
33

4+
let id = 0;
45
export class RDSTransaction extends Operator {
56
isCommit = false;
67
isRollback = false;
78
conn: RDSConnection | null;
9+
id: number;
10+
811
constructor(conn: RDSConnection) {
912
super(conn.conn);
13+
this.id = id++;
1014
this.conn = conn;
1115
}
1216

test/client.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,9 @@ describe('test/client.test.ts', () => {
299299
// recovered after unlock.
300300
await conn.query('select * from `myrds-test-user` limit 1;');
301301
} catch (err) {
302-
conn.release();
303302
throw err;
303+
} finally {
304+
conn.release();
304305
}
305306
});
306307

@@ -354,7 +355,8 @@ describe('test/client.test.ts', () => {
354355
});
355356

356357
it('should throw rollback error with cause error when rollback failed', async () => {
357-
mm(RDSTransaction.prototype, 'rollback', async () => {
358+
mm(RDSTransaction.prototype, 'rollback', async function(this: RDSTransaction) {
359+
this.conn!.release();
358360
throw new Error('fake rollback error');
359361
});
360362
await assert.rejects(
@@ -502,7 +504,9 @@ describe('test/client.test.ts', () => {
502504
});
503505
};
504506

505-
const [ p1Res, p2Res ] = await Promise.all([ p1(), p2().catch(err => err) ]);
507+
const [ p1Res, p2Res ] = await Promise.all([ p1(), p2().catch(err => {
508+
return err;
509+
}) ]);
506510
assert.strictEqual(p1Res, true);
507511
assert.strictEqual(p2Res.code, 'ER_PARSE_ERROR');
508512
const rows = await db.query('select * from ?? where email=? order by id',
@@ -681,6 +685,7 @@ describe('test/client.test.ts', () => {
681685
});
682686
return db;
683687
});
688+
conn.release();
684689
assert(connQuerySql);
685690
assert(!transactionQuerySql);
686691
});
@@ -1508,10 +1513,11 @@ describe('test/client.test.ts', () => {
15081513
for (let i = 0; i < 10; i++) {
15091514
tasks.push(longQuery());
15101515
}
1516+
const tasksPromise = Promise.all(tasks);
15111517
await assert.rejects(async () => {
15121518
await longQuery();
15131519
}, /get connection timeout after/);
1514-
await Promise.all(tasks);
1520+
await tasksPromise;
15151521
});
15161522

15171523
it('should release conn to pool', async () => {

0 commit comments

Comments
 (0)