Skip to content

Commit 46be4e9

Browse files
committed
fix: use connection pool instead of single connection to prvent "Error: Can't add new command when connection is in closed state
at PromiseConnection.execute (/code/node_modules/mysql2/lib/promise/connection.js:47:22)" during inactivity
1 parent 79c5b65 commit 46be4e9

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

adminforth/dataConnectors/mysql.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
88

99
async setupClient(url): Promise<void> {
1010
try {
11-
this.client = await mysql.createConnection(url);
11+
this.client = mysql.createPool({
12+
uri: url,
13+
waitForConnections: true,
14+
connectionLimit: 10, // Adjust based on your needs
15+
queueLimit: 0
16+
});
1217
} catch (e) {
1318
console.error(`Failed to connect to MySQL: ${e}`);
1419
}
@@ -33,7 +38,7 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
3338
};
3439

3540
async discoverFields(resource) {
36-
const [results] = await this.client.query("SHOW COLUMNS FROM " + resource.table);
41+
const [results] = await this.client.execute("SHOW COLUMNS FROM " + resource.table);
3742
const fieldTypes = {};
3843
results.forEach((row) => {
3944
const field: any = {};
@@ -231,7 +236,7 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
231236
if (process.env.HEAVY_DEBUG_QUERY) {
232237
console.log('🪲📜 MySQL Q:', q, 'values:', filterValues);
233238
}
234-
const [results] = await this.client.query(q, filterValues);
239+
const [results] = await this.client.execute(q, filterValues);
235240
return +results[0]["COUNT(*)"];
236241
}
237242

@@ -243,7 +248,7 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
243248
if (process.env.HEAVY_DEBUG_QUERY) {
244249
console.log('🪲📜 MySQL Q:', q);
245250
}
246-
const [results] = await this.client.query(q);
251+
const [results] = await this.client.execute(q);
247252
const { min, max } = results[0];
248253
result[col.name] = {
249254
min, max,

0 commit comments

Comments
 (0)