Skip to content

Commit fc057f6

Browse files
perf: release connections before running post commit steps (#274)
1 parent 39595b5 commit fc057f6

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import connect, {sql} from '..';
2+
3+
jest.setTimeout(10_000);
4+
5+
const db = connect({
6+
bigIntMode: 'bigint',
7+
poolSize: 1,
8+
});
9+
10+
afterAll(async () => {
11+
await db.dispose();
12+
});
13+
14+
test('transaction in task', async () => {
15+
await db.task(async (db) => {
16+
const taskDb = db;
17+
await db.tx(async (db) => {
18+
await db.query(sql`SELECT 1 + 1`);
19+
await db.addPostCommitStep(async () => {
20+
// This code runs after the transaction completes
21+
await taskDb.query(sql`SELECT 1 + 1`);
22+
});
23+
});
24+
});
25+
});
26+
27+
test('tx in pool', async () => {
28+
const poolDb = db;
29+
await db.tx(async (db) => {
30+
await db.query(sql`SELECT 1 + 1`);
31+
await db.addPostCommitStep(async () => {
32+
// This code runs after the transaction completes
33+
await poolDb.query(sql`SELECT 1 + 1`);
34+
});
35+
});
36+
});

packages/shared/src/BaseConnection.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,26 @@ export default class BaseConnection<
5959
options?: TransactionOptions<TDriver>,
6060
): Promise<TResult> {
6161
this._throwIfDisposed();
62+
const postCommitSteps: (() => Promise<void>)[] = [];
6263
await this._lock.acquireLock();
64+
let result: TResult;
6365
try {
64-
return await txInternal(this._driver, this._factories, fn, options);
66+
result = await txInternal(
67+
this._driver,
68+
this._factories,
69+
fn,
70+
options,
71+
(fn) => {
72+
postCommitSteps.push(fn);
73+
},
74+
);
6575
} finally {
6676
this._lock.releaseLock();
6777
}
78+
for (const step of postCommitSteps) {
79+
await step();
80+
}
81+
return result;
6882
}
6983

7084
async query(query: SQLQuery): Promise<any[]>;

packages/shared/src/BaseConnectionPool.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import createConnectionPool, {
44
} from '@databases/connection-pool';
55
import splitSqlQuery from '@databases/split-sql-query';
66
import type {SQLQuery} from '@databases/sql';
7-
import Factory, {Disposable} from './Factory';
7+
import Factory, {Disposable, TransactionFactory} from './Factory';
88
import Driver from './Driver';
99
import QueryableType from './QueryableType';
1010
import {
@@ -94,7 +94,22 @@ export default class BaseConnectionPool<
9494
options?: TransactionOptions<TDriver>,
9595
): Promise<TResult> {
9696
this._throwIfDisposed();
97-
return this._withDriverFromPool(txInternal, this._factories, fn, options);
97+
const postCommitSteps: (() => Promise<void>)[] = [];
98+
const result = await this._withDriverFromPool<
99+
[
100+
TransactionFactory<TDriver, TTransaction>,
101+
(connection: TTransaction) => Promise<TResult>,
102+
TransactionOptions<TDriver> | undefined,
103+
(fn: () => Promise<void>) => void,
104+
],
105+
TResult
106+
>(txInternal, this._factories, fn, options, (fn) => {
107+
postCommitSteps.push(fn);
108+
});
109+
for (const step of postCommitSteps) {
110+
await step();
111+
}
112+
return result;
98113
}
99114

100115
async query(query: SQLQuery): Promise<any[]>;

packages/shared/src/utils.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,12 @@ export async function txInternal<
8585
factories: TransactionFactory<TDriver, TTransaction>,
8686
fn: (connection: TTransaction) => Promise<TResult>,
8787
options: TTransactionOptions | undefined,
88+
addPostCommitStep: (fn: () => Promise<void>) => void,
8889
): Promise<TResult> {
8990
let failureCount = 0;
9091
while (true) {
9192
await driver.beginTransaction(options);
92-
const postCommitSteps: (() => Promise<void>)[] = [];
93-
const tx = factories.createTransaction(driver, {
94-
addPostCommitStep: (fn) => {
95-
postCommitSteps.push(fn);
96-
},
97-
});
93+
const tx = factories.createTransaction(driver, {addPostCommitStep});
9894
let result;
9995
try {
10096
result = await fn(tx);
@@ -110,9 +106,6 @@ export async function txInternal<
110106
}
111107
throw ex;
112108
}
113-
for (const step of postCommitSteps) {
114-
await step();
115-
}
116109
return result;
117110
}
118111
}

0 commit comments

Comments
 (0)