Skip to content

Commit 1defbb2

Browse files
committed
...
1 parent c5dae56 commit 1defbb2

2 files changed

Lines changed: 33 additions & 19 deletions

File tree

src/dialect/mysql/mysql-dialect-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface MysqlPool {
4242
}
4343

4444
export interface MysqlPoolConnection {
45+
destroy(): void
4546
query(
4647
sql: string,
4748
parameters: ReadonlyArray<unknown>,

src/dialect/mysql/mysql-driver.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,6 @@ export class MysqlDriver implements Driver {
6060
return connection
6161
}
6262

63-
async #acquireConnection(): Promise<MysqlPoolConnection> {
64-
return new Promise((resolve, reject) => {
65-
this.#pool!.getConnection(async (err, rawConnection) => {
66-
if (err) {
67-
reject(err)
68-
} else {
69-
resolve(rawConnection)
70-
}
71-
})
72-
})
73-
}
74-
7563
async beginTransaction(
7664
connection: DatabaseConnection,
7765
settings: TransactionSettings,
@@ -158,6 +146,18 @@ export class MysqlDriver implements Driver {
158146
})
159147
})
160148
}
149+
150+
async #acquireConnection(): Promise<MysqlPoolConnection> {
151+
return new Promise((resolve, reject) => {
152+
this.#pool!.getConnection(async (err, rawConnection) => {
153+
if (err) {
154+
reject(err)
155+
} else {
156+
resolve(rawConnection)
157+
}
158+
})
159+
})
160+
}
161161
}
162162

163163
function isOkPacket(obj: unknown): obj is MysqlOkPacket {
@@ -166,7 +166,7 @@ function isOkPacket(obj: unknown): obj is MysqlOkPacket {
166166

167167
class MysqlConnection implements DatabaseConnection {
168168
readonly #rawConnection: MysqlPoolConnection
169-
#pid: unknown
169+
#cid: unknown
170170

171171
constructor(rawConnection: MysqlPoolConnection) {
172172
this.#rawConnection = rawConnection
@@ -175,15 +175,28 @@ class MysqlConnection implements DatabaseConnection {
175175
async cancelQuery(
176176
controlConnectionProvider: ControlConnectionProvider,
177177
): Promise<void> {
178-
if (!this.#pid) {
178+
if (!this.#cid) {
179179
return logOnce(
180180
'kysely:warning: cannot cancel query because the connection has not been made cancelable.',
181181
)
182182
}
183183

184-
return await controlConnectionProvider(async (controlConnection) => {
184+
try {
185+
// this removes the connection from the pool.
186+
// this is done to avoid picking it up after the `kill` command next, which
187+
// would cause an error when attempting to query.
188+
this.#rawConnection.destroy()
189+
} catch {
190+
// noop
191+
}
192+
193+
await controlConnectionProvider(async (controlConnection) => {
194+
// this kills the query and the connection database-side.
195+
// we're not using `kill query <connection_id>` here because it doesn't
196+
// guarantee that the query is killed immediately. we saw that in tests,
197+
// the query can still run for a while after - including registering writes.
185198
await controlConnection.executeQuery(
186-
CompiledQuery.raw('kill connection ?', [this.#pid]),
199+
CompiledQuery.raw('kill connection ?', [this.#cid]),
187200
)
188201
})
189202
}
@@ -284,14 +297,14 @@ class MysqlConnection implements DatabaseConnection {
284297
}
285298

286299
async #setupCancelability(): Promise<void> {
287-
if (this.#pid) {
300+
if (this.#cid) {
288301
return
289302
}
290303

291304
const results = await this.#executeQuery(
292-
CompiledQuery.raw('select connection_id() as pid', []),
305+
CompiledQuery.raw('select connection_id() as cid', []),
293306
)
294307

295-
this.#pid = (results as any[])[0]?.pid
308+
this.#cid = (results as any[])[0]?.cid
296309
}
297310
}

0 commit comments

Comments
 (0)