From 4b8dc1fa1cbff05ad2dabe045e0a06d710e8f4a5 Mon Sep 17 00:00:00 2001 From: Axel Cocat Date: Mon, 28 Feb 2022 23:27:46 +0100 Subject: [PATCH] feat: use mysql connection pool --- src/armory/Armory.ts | 12 ++++++------ src/armory/controllers/IndexController.ts | 5 +++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/armory/Armory.ts b/src/armory/Armory.ts index d9c0ee1..807c61f 100644 --- a/src/armory/Armory.ts +++ b/src/armory/Armory.ts @@ -4,7 +4,7 @@ import { Express } from "express"; import * as express from "express"; import * as winston from "winston"; import * as morgan from "morgan"; -import { Connection, createConnection } from "mysql2/promise"; +import { Pool, createPool } from "mysql2/promise"; import { engine as handlebarsEngine } from "express-handlebars"; import { Config } from "./Config"; @@ -17,10 +17,10 @@ export class Armory { public characterCustomization: CharacterCustomization; public dbc: DbcManager; public config: Config; - public worldDb: Connection; + public worldDb: Pool; public logger: winston.Logger; - private charsDbs: { [key: string]: Connection }; + private charsDbs: { [key: string]: Pool }; private errorNames: { [key: number]: string }; private errorDescriptions: { [key: number]: string }; @@ -68,9 +68,9 @@ export class Armory { await this.characterCustomization.loadData(); this.logger.info("Connecting to databases..."); - this.worldDb = await createConnection(this.config.worldDatabase); + this.worldDb = createPool(this.config.worldDatabase); for (const realm of this.config.realms) { - this.charsDbs[realm.name.toLowerCase()] = await createConnection(realm.charactersDatabase); + this.charsDbs[realm.name.toLowerCase()] = createPool(realm.charactersDatabase); } this.logger.info("Starting server..."); @@ -168,7 +168,7 @@ export class Armory { }); } - public getCharactersDb(realm: string): Connection { + public getCharactersDb(realm: string): Pool { return this.charsDbs[realm.toLowerCase()]; } diff --git a/src/armory/controllers/IndexController.ts b/src/armory/controllers/IndexController.ts index 48e679f..1599a88 100644 --- a/src/armory/controllers/IndexController.ts +++ b/src/armory/controllers/IndexController.ts @@ -54,20 +54,21 @@ export class IndexController { } const db = this.armory.getCharactersDb(realm.name); + const conn = await db.getConnection(); if (!(realm.name in this.charsetCache)) { let [rows, fields] = await db.query(` SELECT CCSA.character_set_name FROM information_schema.\`TABLES\` T, information_schema.\`COLLATION_CHARACTER_SET_APPLICABILITY\` CCSA WHERE CCSA.collation_name = T.table_collation - AND T.table_schema = "${db.config.database}" + AND T.table_schema = "${conn.config.database}" AND T.table_name = "characters" `); this.charsetCache[realm.name] = rows[0].character_set_name; } const charSet = this.charsetCache[realm.name]; - let ssp = new DataTablesSsp(req.query, db, "characters", "guid", [ + let ssp = new DataTablesSsp(req.query, conn, "characters", "guid", [ { name: "name", collation: `${charSet}_general_ci` }, { table: "guild", name: "name" }, { name: "level" },