@@ -42,7 +42,10 @@ export class MysqlDriver implements Driver {
4242 let connection = this . #connections. get ( rawConnection )
4343
4444 if ( ! connection ) {
45- connection = new MysqlConnection ( rawConnection )
45+ connection = new MysqlConnection (
46+ rawConnection ,
47+ this . #config. createConnection ,
48+ )
4649 this . #connections. set ( rawConnection , connection )
4750
4851 // The driver must take care of calling `onCreateConnection` when a new
@@ -165,9 +168,14 @@ function isOkPacket(obj: unknown): obj is MysqlOkPacket {
165168}
166169
167170class MysqlConnection implements DatabaseConnection {
171+ readonly #createConnection: MysqlDialectConfig [ 'createConnection' ]
168172 readonly #rawConnection: MysqlPoolConnection
169173
170- constructor ( rawConnection : MysqlPoolConnection ) {
174+ constructor (
175+ rawConnection : MysqlPoolConnection ,
176+ createConnection : MysqlDialectConfig [ 'createConnection' ] ,
177+ ) {
178+ this . #createConnection = createConnection
171179 this . #rawConnection = rawConnection
172180 }
173181
@@ -183,14 +191,38 @@ class MysqlConnection implements DatabaseConnection {
183191 // noop
184192 }
185193
194+ const { config, threadId } = this . #rawConnection
195+
196+ // this kills the query and the connection database-side.
197+ // we're not using `kill query <connection_id>` here because it doesn't
198+ // guarantee that the query is killed immediately. we saw that in tests,
199+ // the query can still run for a while after - including registering writes.
200+ const cancelQuery = `kill connection ${ threadId } `
201+
202+ if ( this . #createConnection && config ) {
203+ const controlConnection = await this . #createConnection( { ...config } )
204+
205+ return await new Promise ( ( resolve , reject ) => {
206+ controlConnection . connect ( ( connectError ) => {
207+ if ( connectError ) {
208+ return reject ( connectError )
209+ }
210+
211+ controlConnection . query ( cancelQuery , [ ] , ( error ) => {
212+ controlConnection . destroy ( )
213+
214+ if ( error ) {
215+ return reject ( error )
216+ }
217+
218+ resolve ( )
219+ } )
220+ } )
221+ } )
222+ }
223+
186224 await controlConnectionProvider ( async ( controlConnection ) => {
187- // this kills the query and the connection database-side.
188- // we're not using `kill query <connection_id>` here because it doesn't
189- // guarantee that the query is killed immediately. we saw that in tests,
190- // the query can still run for a while after - including registering writes.
191- await controlConnection . executeQuery (
192- CompiledQuery . raw ( 'kill connection ?' , [ this . #rawConnection. threadId ] ) ,
193- )
225+ await controlConnection . executeQuery ( CompiledQuery . raw ( cancelQuery , [ ] ) )
194226 } )
195227 }
196228
@@ -231,7 +263,6 @@ class MysqlConnection implements DatabaseConnection {
231263 async * streamQuery < O > (
232264 compiledQuery : CompiledQuery ,
233265 _chunkSize : number ,
234- options ?: QueryOptions ,
235266 ) : AsyncIterableIterator < QueryResult < O > > {
236267 const stream = this . #rawConnection
237268 . query ( compiledQuery . sql , compiledQuery . parameters )
0 commit comments