From 4253099f07b4e47b8bf29afdfa418ff855804c08 Mon Sep 17 00:00:00 2001
From: Rosanny <rosanny.sihombing@hft-stuttgart.de>
Date: Thu, 16 Sep 2021 13:35:13 +0200
Subject: [PATCH] using connection pools

---
 config/dbconn.ts | 49 ++++++++++++------------------------------------
 1 file changed, 12 insertions(+), 37 deletions(-)

diff --git a/config/dbconn.ts b/config/dbconn.ts
index c13c40f1..845697e5 100644
--- a/config/dbconn.ts
+++ b/config/dbconn.ts
@@ -4,59 +4,34 @@ var env = process.env.NODE_ENV || 'testing'
 const config = require('./config')[env]
 
 // ==== USER ACOOUNT DB CONNECTION ====
-var userConnection = mysql.createConnection({
+const userConnection = mysql.createPool({
     host: config.database.host,
     user: config.database.user,
     password: config.database.password,
     port: config.database.port,
     database: config.database.dbUser,
-    multipleStatements: true
-})
+    waitForConnections: true,
+    connectionLimit: 10,
+    queueLimit: 0
+});
 
-userConnection.connect(function(err) {
-    if (err) throw err
-})
 userConnection.query('USE '+config.database.dbUser)
 
-// ALTERNATIVE approach: close db connection manually after every query
-/*
-var dbconn = function dbconn(query, values, next) {
-    var connection = mysql.createConnection({
-        host: config.database.host,
-        user: config.database.user,
-        password: config.database.password,
-        port: config.database.port,
-        database: config.database.db
-    })
-    connection.connect(function(err) {
-        if (err) throw err;
-    })
-    connection.query(query, values, function(err) {
-        connection.end(); // close the connection
-        if (err) {
-            throw err;
-        }
-        // Execute the callback
-        next.apply(this, arguments);
-    });
-}
-*/
-
 // ==== PROJECT DB CONNECTION ====
-var projectConnection = mysql.createConnection({
+const projectConnection = mysql.createPool({
     host: config.database.host_project,
     user: config.database.user,
     password: config.database.password,
     port: config.database.port,
-    database: config.database.dbProject
-})
+    database: config.database.dbProject,
+    waitForConnections: true,
+    connectionLimit: 10,
+    queueLimit: 0
+});
 
-projectConnection.connect(function(err) {
-    if (err) throw err
-})
 projectConnection.query('USE '+config.database.dbProject)
 
-var connection = {
+const connection = {
     user: userConnection,
     project: projectConnection
 }
-- 
GitLab