title: Node.js MySQL Needing Persistent Connection
body: <p>I need a persistent MySQL connection for my Node web app. The problem is that this happens about a few times a day:</p>
  
  <pre><code>Error: Connection lost: The server closed the connection.
  at Protocol.end (/var/www/n/node_modules/mysql/lib/protocol/Protocol.js:73:13)
  at Socket.onend (stream.js:79:10)
  at Socket.EventEmitter.emit (events.js:117:20)
  at _stream_readable.js:895:16
  at process._tickCallback (node.js:415:13)
  error: Forever detected script exited with code: 8
  error: Forever restarting script for 2 time
  info: socket.io started
  </code></pre>
  
  <p>Here is my connection code:</p>
  
  <pre><code>// Yes I know multipleStatements can be dangerous in the wrong hands.
  var sql = mysql.createConnection({
      host: 'localhost',
      user: 'my_username',
      password: 'my_password',
      database: 'my_database',
      multipleStatements: true
  });
  
  sql.connect();
  
  function handleDisconnect(connection) {
      connection.on('error', function(err) {
          if (!err.fatal) {
              return;
          }
          if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
              throw err;
          }
          console.log('Re-connecting lost connection: ' + err.stack);
          sql = mysql.createConnection(connection.config);
          handleDisconnect(sql);
          sql.connect();
      });
  }
  
  handleDisconnect(sql);
  </code></pre>
  
  <p>As you can see, the handleDisconnect code does not work..</p>