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 5 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
5 changes: 5 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export {
configLogger as configMySQLLogger,
Connection as MySQLConnection,
} from "https://deno.land/x/[email protected]/mod.ts";

export {
ResponseTimeoutError as MySQLReponseTimeoutError
} from "https://deno.land/x/[email protected]/src/constant/errors.ts";

export type { LoggerConfig } from "https://deno.land/x/[email protected]/mod.ts";

export { Client as PostgresClient } from "https://deno.land/x/[email protected]/mod.ts";
Expand Down
43 changes: 40 additions & 3 deletions lib/connectors/mysql-connector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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 +14,8 @@ export interface MySQLOptions extends ConnectorOptions {
port?: number;
charset?: string;
logger?: LoggerConfig;
idleTimeout?: number;
reconnectOnTimeout?: boolean
bleugateau marked this conversation as resolved.
Show resolved Hide resolved
}

export class MySQLConnector implements Connector {
Expand Down Expand Up @@ -46,6 +49,7 @@ export class MySQLConnector implements Connector {
password: this._options.password,
port: this._options.port ?? 3306,
charset: this._options.charset ?? "utf8",
idleTimeout: this._options.idleTimeout
bleugateau marked this conversation as resolved.
Show resolved Hide resolved
});

this._connected = true;
Expand All @@ -62,9 +66,15 @@ export class MySQLConnector implements Connector {
}
}

/**
* Executing query
* @param queryDescription {QueryDescription}
* @param client {MySQLClient | MySQLConnection}
* @param reconnectAttempt {boolean} - Used for reconnect client when ResponseTimeoutError happened
*/
bleugateau marked this conversation as resolved.
Show resolved Hide resolved
async query(
queryDescription: QueryDescription,
client?: MySQLClient | MySQLConnection,
client?: MySQLClient | MySQLConnection
): Promise<any | any[]> {
await this._makeConnection();

Expand All @@ -76,7 +86,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) {
//reconnect client, at this moment we can't subscribe to connectionState of mysql driver, we need to do this
await this.reconnect();

return this.query(queryDescription, client);
}

throw error;
}
bleugateau marked this conversation as resolved.
Show resolved Hide resolved

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


/**
* Reconnect client connection
*/
bleugateau marked this conversation as resolved.
Show resolved Hide resolved
async reconnect() {
warning("Client reconnection has been triggered.");

bleugateau marked this conversation as resolved.
Show resolved Hide resolved
await this.close();
return this._makeConnection();
}

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