title: Server connection timed out when using mysql pooling
body: <p>So I have been getting this error a lot recently.</p>
  
  <pre><code>Error: Connection lost: The server closed the connection.
  at Protocol.end (/home/node_modules/mysql/lib/protocol/Protocol.js:$
  at Socket.&lt;anonymous&gt; (/home/node_modules/mysql/lib/Connection.js:1$
  at emitNone (events.js:111:20)
  at Socket.emit (events.js:208:7)
  at endReadableNT (_stream_readable.js:1064:12)
  at _combinedTickCallback (internal/process/next_tick.js:138:11)
  at process._tickDomainCallback (internal/process/next_tick.js:218:9)
  </code></pre>
  
  <p>I have been searching a lot for a solution. I read a lot that mysql pooling would solve this, which I have been using for weeks now. The error still pops up though. Does anyone know why this could be?</p>
  
  <p>I'm using this base function which I found in an answer on Stackoverflow. It handles all my queries</p>
  
  <pre><code>var mysql   = require("mysql");
  var config = require('./db');
  var db = config.database;
  
  var pool = mysql.createPool({
      connectionLimit : 20,
      host: db.host,
      user: db.user,
      password: db.password,
      database: db.database
  });
  
  
  var DB = (function () {
  
      function _query(query, params, callback) {
          pool.getConnection(function (err, connection) {
              if (err) {
                  connection.release();
                  callback(null, err);
                  throw err;
              }
  
              connection.query(query, params, function (err, rows) {
                  connection.release();
                  if (!err) {
                      callback(rows);
                  }
                  else {
                      callback(null, err);
                  }
  
              });
  
              connection.on('error', function (err) {
                  connection.release();
                  callback(null, err);
                  throw err;
              });
          });
      };
  
      return {
          query: _query
      };
  })();
  
  module.exports = DB;
  </code></pre>
  
  <p>I'm executing queries like this:</p>
  
  <pre><code>    DB.query("SELECT * FROM lists WHERE list_id = ?", [listId], function (result, err) {
  console.log(result);
  
  }
  </code></pre>