我们知道 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/