title: When to close MySQL connection using node-mysql?
body: <p>Currently using: <a href="https://github.com/felixge/node-mysql" rel="noreferrer">https://github.com/felixge/node-mysql</a></p>
  
  <p>I have the following code:</p>
  
  <pre><code>var connection = mysql.createConnection({
      host     : 'localhost',
      user     : 'me',
      password : 'secret',
      database : 'Database1'
  });
  app.put('/api/upload', function(req, res, next)
  {
      connection.connect();
      doMultipleQueries(function(err)
      {
          connection.end();
      });          
  };
  </code></pre>
  
  <p>The put request works perfectly fine, but calling it the second time, I get the following error</p>
  
  <pre><code>events.js:68
          throw arguments[1]; // Unhandled 'error' event
                         ^
  Error: Cannot enqueue Handshake after invoking quit.
      at Protocol._validateEnqueue (/Users/anon/Desktop/project Web/node_modules/mysql/lib/protocol/Protocol.js:110:16)
  </code></pre>
  
  <p>Am I supposed to leave the connection open until the server dies?</p>
  
  <p>UPDATE:
  When I move the <code>mysql.createConnection</code> into the put request function like so:</p>
  
  <pre><code>var connection = null; 
  app.put('/api/upload', function(req, res, next)
  {
      connection = mysql.createConnection({
          host     : 'localhost',
          user     : 'me',
          password : 'secret',
          database : 'Database1'
      });
      connection.connect();
      doMultipleQueries(function(err)
      {
          connection.end();
      });          
  };
  </code></pre>
  
  <p>It works fine. Does this mean <code>connection.end()</code> closes what <code>mysql.createConnection</code> created and cannot be reconnected?</p>