timeout in node mysql

as we knows node has some mysql modules, some are pure js implemented(like node-mysql), some are based on c libmysql.

i quite prefer node-mysql because it doesn't need extra mysql library which seems more 'clean'. But i also notice that it does not support timeout feature in connect & query which may cause problem at some envrioment.

so my question is : does anyone have solve this timeout problem cleanly?

What we did to fix this was to check the error message, and re-connect if necessary

This is how they suggest it at https://github.com/felixge/node-mysql#server-disconnects

Here is the sample code in case the documentation ever changes

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);

    connection = mysql.createConnection(connection.config);
    handleDisconnect(connection);
    connection.connect();
  });
}

handleDisconnect(connection);

I have had the same issue, using the method mysql.createPool instead of the method createConnection have work for me.