Skip to content

Commit b0d8935

Browse files
committed
fix: improve async calls on disconnect
1 parent 42913b9 commit b0d8935

File tree

13 files changed

+81
-66
lines changed

13 files changed

+81
-66
lines changed

sample/playground/package-lock.json

Lines changed: 31 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/playground/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
"reflect-metadata": "^0.2.2",
1313
"sql.js": "^1.13.0",
1414
"ts-node": "^10.9.2",
15-
"typeorm": "0.3.22"
15+
"typeorm": "*"
1616
},
1717
"devDependencies": {
18-
"@types/node": "^20.11.24",
18+
"@types/node": "^20.19.0",
1919
"@types/sql.js": "^1.4.9",
20-
"typescript": "^5.3.3"
20+
"typescript": "^5.8.3"
2121
}
2222
}

src/driver/cockroachdb/CockroachDriver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,9 @@ export class CockroachDriver implements Driver {
346346
* Closes connection with database.
347347
*/
348348
async disconnect(): Promise<void> {
349-
if (!this.master)
350-
return Promise.reject(new ConnectionIsNotSetError("cockroachdb"))
349+
if (!this.master) {
350+
throw new ConnectionIsNotSetError("cockroachdb")
351+
}
351352

352353
await this.closePool(this.master)
353354
await Promise.all(this.slaves.map((slave) => this.closePool(slave)))

src/driver/mongodb/MongoDriver.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,13 @@ export class MongoDriver implements Driver {
260260
* Closes connection with the database.
261261
*/
262262
async disconnect(): Promise<void> {
263-
if (!this.queryRunner) throw new ConnectionIsNotSetError("mongodb")
264-
// const handler = (err: any) => (err ? fail(err) : ok())
265-
this.queryRunner.databaseConnection.close()
263+
const queryRunner = this.queryRunner
264+
if (!queryRunner) {
265+
throw new ConnectionIsNotSetError("mongodb")
266+
}
267+
266268
this.queryRunner = undefined
267-
// return ok()
269+
await queryRunner.databaseConnection.close()
268270
}
269271

270272
/**

src/driver/mysql/MysqlDriver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,9 @@ export class MysqlDriver implements Driver {
445445
* Closes connection with the database.
446446
*/
447447
async disconnect(): Promise<void> {
448-
if (!this.poolCluster && !this.pool)
449-
return Promise.reject(new ConnectionIsNotSetError("mysql"))
448+
if (!this.poolCluster && !this.pool) {
449+
throw new ConnectionIsNotSetError("mysql")
450+
}
450451

451452
if (this.poolCluster) {
452453
return new Promise<void>((ok, fail) => {

src/driver/oracle/OracleDriver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ export class OracleDriver implements Driver {
343343
* Closes connection with the database.
344344
*/
345345
async disconnect(): Promise<void> {
346-
if (!this.master)
347-
return Promise.reject(new ConnectionIsNotSetError("oracle"))
346+
if (!this.master) {
347+
throw new ConnectionIsNotSetError("oracle")
348+
}
348349

349350
await this.closePool(this.master)
350351
await Promise.all(this.slaves.map((slave) => this.closePool(slave)))

src/driver/postgres/PostgresDriver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,9 @@ export class PostgresDriver implements Driver {
585585
* Closes connection with database.
586586
*/
587587
async disconnect(): Promise<void> {
588-
if (!this.master)
589-
return Promise.reject(new ConnectionIsNotSetError("postgres"))
588+
if (!this.master) {
589+
throw new ConnectionIsNotSetError("postgres")
590+
}
590591

591592
await this.closePool(this.master)
592593
await Promise.all(this.slaves.map((slave) => this.closePool(slave)))

src/driver/sap/SapDriver.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
ColumnType,
3+
ConnectionIsNotSetError,
34
DataSource,
45
EntityMetadata,
56
ObjectLiteral,
@@ -324,9 +325,13 @@ export class SapDriver implements Driver {
324325
* Closes connection with the database.
325326
*/
326327
async disconnect(): Promise<void> {
327-
const promise = this.master.clear()
328+
const pool = this.master
329+
if (!pool) {
330+
throw new ConnectionIsNotSetError("sap")
331+
}
332+
328333
this.master = undefined
329-
return promise
334+
await pool.clear()
330335
}
331336

332337
/**

src/driver/sqlserver/SqlServerDriver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,9 @@ export class SqlServerDriver implements Driver {
330330
* Closes connection with the database.
331331
*/
332332
async disconnect(): Promise<void> {
333-
if (!this.master)
334-
return Promise.reject(new ConnectionIsNotSetError("mssql"))
335-
333+
if (!this.master) {
334+
throw new ConnectionIsNotSetError("mssql")
335+
}
336336
await this.closePool(this.master)
337337
await Promise.all(this.slaves.map((slave) => this.closePool(slave)))
338338
this.master = undefined

test/functional/driver/mysql/connection-options/enable-query-timeout.ts

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import {
88
reloadTestingDatabases,
99
} from "../../../../utils/test-utils"
1010

11-
describe("mysql driver > enableQueryTimeout connection option", () => {
11+
describe("driver > mysql > connection options > enableQueryTimeout", () => {
1212
let dataSources: DataSource[]
1313
const commonConnectionOptions: TestingOptions = {
1414
entities: [__dirname + "/entity/*{.js,.ts}"],
1515
schemaCreate: true,
1616
dropSchema: true,
1717
enabledDrivers: ["mysql"],
1818
}
19-
const timeoutMs = 10
20-
const longQueryTimeSec = 0.02
19+
const timeoutMs = 500
20+
const longQueryTimeSec = 1
2121
const shortQueryTimeSec = 0.005
2222

2323
describe("when enableQueryTimeout is true", () => {
@@ -37,13 +37,10 @@ describe("mysql driver > enableQueryTimeout connection option", () => {
3737
it("should throw a query execution timeout error for the query when it exceeds the maxQueryExecutionTime", async () => {
3838
await Promise.all(
3939
dataSources.map(async (dataSource) => {
40-
let errorThrown = false
41-
try {
42-
await dataSource.manager.query(
43-
`SELECT SLEEP(${longQueryTimeSec})`,
44-
)
45-
} catch (err) {
46-
errorThrown = true
40+
await expect(
41+
dataSource.manager
42+
.sql`SELECT SLEEP(${longQueryTimeSec})`,
43+
).to.be.rejected.then((err) => {
4744
expect(err).to.have.nested.property(
4845
"driverError.code",
4946
"PROTOCOL_SEQUENCE_TIMEOUT",
@@ -52,24 +49,18 @@ describe("mysql driver > enableQueryTimeout connection option", () => {
5249
"driverError.timeout",
5350
timeoutMs,
5451
)
55-
}
56-
expect(errorThrown).to.be.true
52+
})
5753
}),
5854
)
5955
})
6056

6157
it("should not throw a query execution timeout error for the query when it runs within the maxQueryExecutionTime", async () => {
6258
await Promise.all(
6359
dataSources.map(async (dataSource) => {
64-
let errorThrown = false
65-
try {
66-
await dataSource.manager.query(
67-
`SELECT SLEEP(${shortQueryTimeSec})`,
68-
)
69-
} catch (err) {
70-
errorThrown = true
71-
}
72-
expect(errorThrown).to.be.false
60+
await expect(
61+
dataSource.manager
62+
.sql`SELECT SLEEP(${shortQueryTimeSec})`,
63+
).to.be.eventually.fulfilled
7364
}),
7465
)
7566
})
@@ -90,15 +81,10 @@ describe("mysql driver > enableQueryTimeout connection option", () => {
9081
it("should not throw a query execution timeout error", () => {
9182
Promise.all(
9283
datasources.map(async (dataSource) => {
93-
let errorThrown = false
94-
try {
95-
await dataSource.manager.query(
96-
`SELECT SLEEP(${longQueryTimeSec})`,
97-
)
98-
} catch (err) {
99-
errorThrown = true
100-
}
101-
expect(errorThrown).to.be.false
84+
await expect(
85+
dataSource.manager
86+
.sql`SELECT SLEEP(${longQueryTimeSec})`,
87+
).to.eventually.be.fulfilled
10288
}),
10389
)
10490
})

0 commit comments

Comments
 (0)