保持数据库处于连接状态

function handleDisconnect() {
  sqlconnection.connect(function (err) {
    if (err) {
      logger.error('error when connecting to db:', err)
      setTimeout(handleDisconnect, 2000)
    }
  })
  sqlconnection.on('error', function (err) {
    logger.error('db error:', err)
    if (err.code === 'PROTOCOL_CONNECTION_LOST') {
      // Connection to the MySQL server is usually
      // lost due to either server restart, or a
      // connnection idle timeout (the wait_timeout
      // server variable configures this)
      handleDisconnect()
    } else {
      throw err
    }
  })
}
handleDisconnect()

Last updated