Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Auto reconnection to the database server is not triggered #269

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ export {
configLogger as configMySQLLogger,
Connection as MySQLConnection,
} from "https://deno.land/x/mysql@v2.10.0/mod.ts";

export {
ResponseTimeoutError as MySQLReponseTimeoutError
} from "https://deno.land/x/mysql@v2.10.0/src/constant/errors.ts";

export type { LoggerConfig } from "https://deno.land/x/mysql@v2.10.0/mod.ts";


export { Client as PostgresClient } from "https://deno.land/x/postgres@v0.11.2/mod.ts";

export { DB as SQLiteClient } from "https://deno.land/x/sqlite@v2.3.1/mod.ts";
Expand Down
43 changes: 41 additions & 2 deletions lib/connectors/mysql-connector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { configMySQLLogger, MySQLClient, MySQLConnection } from "../../deps.ts";
import {
configMySQLLogger,
MySQLClient,
MySQLConnection,
MySQLReponseTimeoutError,
} from "../../deps.ts";
import type { LoggerConfig } from "../../deps.ts";
import type { Connector, ConnectorOptions } from "./connector.ts";
import { SQLTranslator } from "../translators/sql-translator.ts";
import type { SupportedSQLDatabaseDialect } from "../translators/sql-translator.ts";
import type { QueryDescription } from "../query-builder.ts";
import { warning } from "../helpers/log.ts";

export interface MySQLOptions extends ConnectorOptions {
database: string;
Expand All @@ -13,6 +19,8 @@ export interface MySQLOptions extends ConnectorOptions {
port?: number;
charset?: string;
logger?: LoggerConfig;
idleTimeout?: number;
reconnectOnTimeout?: boolean;
}

export class MySQLConnector implements Connector {
Expand Down Expand Up @@ -46,6 +54,7 @@ export class MySQLConnector implements Connector {
password: this._options.password,
port: this._options.port ?? 3306,
charset: this._options.charset ?? "utf8",
idleTimeout: this._options.idleTimeout,
});

this._connected = true;
Expand All @@ -62,6 +71,11 @@ export class MySQLConnector implements Connector {
}
}

/**
* Executing query
* @param queryDescription {QueryDescription}
* @param client {MySQLClient | MySQLConnection}
*/
async query(
queryDescription: QueryDescription,
client?: MySQLClient | MySQLConnection,
Expand All @@ -76,7 +90,23 @@ export class MySQLConnector implements Connector {
: "execute";

for (let i = 0; i < subqueries.length; i++) {
const result = await queryClient[queryMethod](subqueries[i]);
let result = null;

try {
result = await queryClient[queryMethod](subqueries[i]);
} catch (error) {
//reconnect client on timeout error
if (
this._options.reconnectOnTimeout &&
error instanceof MySQLReponseTimeoutError
) {
await this.reconnect();

return this.query(queryDescription, client);
}

throw error;
}

if (i === subqueries.length - 1) {
return result;
Expand All @@ -88,6 +118,15 @@ export class MySQLConnector implements Connector {
return this._client.transaction(queries);
}

/**
* Reconnect current client connection
*/
async reconnect() {
warning("Client reconnection has been triggered.");
await this.close();
return this._makeConnection();
}

async close() {
if (!this._connected) {
return;
Expand Down