title: Too many mysql connections / optimizing mysql query
body: <p>After using my node applications for a couple of time I always get problems with too many MySQL queries. Currently I am using a MySQL Connection Pool which already increased the stability but isn't a final solution.</p>
  
  <p>Is there a better way to connect to the MySQL database and close the connection directly after the query?</p>
  
  <p>General "MySQL Connection":</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const mysql = require('mysql');
  const config = require('config');
  const dbConfig = config.get('Azure.dbConfig');
  
  console.log(`used mysql information: ${dbConfig}`);
  const con = mysql.createPool(dbConfig
  );
  
  con.on('enqueue', () =&gt; {
      console.log('connection enqued')
  });
  con.on('connection', function (connection) {
      connection.query('SET SESSION auto_increment_increment=1')
  });
  
  con.on('release', function (connection) {
      console.log('Connection %d released', connection.threadId);
  });
  
  module.exports = con;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Example for a MySQL query:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const con = require('../db.js');
  const bodyParser = require('body-parser');
  
  
  
  module.exports = function (app) {
      
      app.use(bodyParser.json());
      app.use(bodyParser.urlencoded({
          extended: true
      }));
  
      app.get('/dbCompareName', function (req, res) {
  
          const ingredients = [];
  
          con.query('SELECT wm_wine.name, wm_wine.avocandoIngredient, wm_wine.jahrgang, wm_wine.sorte, wm_cart.anzahl, wm_wine.typ from wm_wine INNER JOIN wm_cart on wm_cart.id = wm_wine.id', function (err, result, info) {
              if (err) {
                  console.log('while querying the mysql database, an error occurred');
                  console.log(err);
                  if (err.fatal === true) {
                      con.connect(err =&gt; {
                          if (err) throw err;
                          logger.info('reconnected to mysql database');
                      });               
                  };
              }
              else {
                  const transfer = result;
                  
              };
  
  };</code></pre>
  </div>
  </div>
  </p>