mysql - Node mysql超时

标签 mysql node.js timeout

我们知道 node 有一些 mysql 模块,有些是纯 js 实现的(如 node-mysql ),有些是基于 c libmysql。

我更喜欢 node-mysql,因为它不需要额外的 mysql 库,看起来更“干净”。但我也注意到它不支持连接和查询中的超时功能,这在某些环境下可能会导致问题。

所以我的问题是:有没有人彻底解决这个超时问题?

最佳答案

我们为解决此问题所做的是检查错误消息,并在必要时重新连接

这就是他们在 https://github.com/felixge/node-mysql#server-disconnects 上的建议

这里是示例代码,以防文档发生变化

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

关于mysql - Node mysql超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14180858/

相关文章:

node.js - Node.JS 的默认文件夹功能

mysql工作台 "Lost connection to mysql server"

MySQL TIMEDIFF 负值

带有加密文件系统的 MySQL 数据库用于存储图像

node.js - Bot 框架中的上下文管理 - Node JS

javascript - 如何在 Nodejs REPL 中隐藏 process.stdout.write() 的返回值?

java - 如何在 Seam 应用程序中阻止 HTTP session 超时?

c++ - 停止 Intellisense 显示超时

php - 如何在php mysql中使用unix时间获取今天记录

mysql - 来自 Django select_for_update 的锁定记录是否受更新值的影响