Skip to content

Commit

Permalink
handle connection errors @ MssqlDriver.
Browse files Browse the repository at this point in the history
..
  • Loading branch information
igalklebanov committed Jun 17, 2024
1 parent 2473948 commit 24d3f5d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
20 changes: 12 additions & 8 deletions src/dialect/mssql/mssql-dialect-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,26 @@ export interface TediousConnection {
name?: string,
isolationLevel?: number,
): void
cancel(): boolean
close(): void
commitTransaction(
callback: (error?: Error | null) => void,
name?: string,
): void
connect(callback?: (error?: Error) => void): void
execSql(request: TediousRequest): void
off(event: 'error', listener: (error: unknown) => void): this
off(event: string, listener: (...args: any[]) => void): this
on(event: 'error', listener: (error: unknown) => void): this
on(event: string, listener: (...args: any[]) => void): this
once(event: 'end', listener: () => void): this
once(event: string, listener: (...args: any[]) => void): this
reset(callback: (error?: Error | null) => void): void
rollbackTransaction(
callback: (error?: Error | null) => void,
name?: string,
): void
saveTransaction(callback: (error?: Error | null) => void, name: string): void
cancel(): boolean
reset(callback: (error?: Error | null) => void): void
close(): void
once(event: 'end', listener: () => void): this
once(event: string, listener: (...args: any[]) => void): this
connect(callback?: (error?: Error) => void): void
}

export type TediousIsolationLevel = Record<string, number>
Expand All @@ -104,12 +108,12 @@ export declare class TediousRequest {
scale?: number
}> | null,
): void
off(event: 'row', listener: (columns: any) => void): this
off(event: string, listener: (...args: any[]) => void): this
on(event: 'row', listener: (columns: any) => void): this
on(event: string, listener: (...args: any[]) => void): this
once(event: 'requestCompleted', listener: () => void): this
once(event: string, listener: (...args: any[]) => void): this
off(event: 'row', listener: (columns: any) => void): this
off(event: string, listener: (...args: any[]) => void): this
pause(): void
resume(): void
}
Expand Down
34 changes: 25 additions & 9 deletions src/dialect/mssql/mssql-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,12 @@ export class MssqlDriver implements Driver {
this.#pool = new this.#config.tarn.Pool({
...this.#config.tarn.options,
create: async () => {
const connection = await this.#config.tedious.connectionFactory()
const tediousConnection = await this.#config.tedious.connectionFactory()

await new Promise((resolve, reject) =>
connection.connect((error) => {
if (error) reject(error)
else resolve(undefined)
}),
)

return new MssqlConnection(connection, this.#config.tedious)
return await new MssqlConnection(
tediousConnection,
this.#config.tedious,
).connect()
},
destroy: async (connection) => {
await connection[PRIVATE_DESTROY_METHOD]()
Expand Down Expand Up @@ -104,6 +100,11 @@ class MssqlConnection implements DatabaseConnection {
constructor(connection: TediousConnection, tedious: Tedious) {
this.#connection = connection
this.#tedious = tedious

this.#connection.on('error', console.error)
this.#connection.once('end', () => {
this.#connection.off('error', console.error)
})
}

async beginTransaction(settings: TransactionSettings): Promise<void> {
Expand Down Expand Up @@ -132,6 +133,21 @@ class MssqlConnection implements DatabaseConnection {
)
}

async connect(): Promise<this> {
await new Promise((resolve, reject) => {
this.#connection.connect((error) => {
if (error) {
console.error(error)
reject(error)
} else {
resolve(undefined)
}
})
})

return this
}

async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {
try {
const deferred = new Deferred<OnDone<O>>()
Expand Down

0 comments on commit 24d3f5d

Please sign in to comment.