使用池处理node.js mysql中的错误和断开连接
我使用node.js npm模块mysql连接mysql数据库 我认为有可能使用:使用池处理node.js mysql中的错误和断开连接,node.js,connection-pooling,node-mysql,Node.js,Connection Pooling,Node Mysql,我使用node.js npm模块mysql连接mysql数据库 我认为有可能使用: pool.getConnection()、connection.query()和connection.release()中的任意一个 或pool.query 关于第二种情况,我有两个问题 1) 在使用pool.query时,如果出现错误或连接丢失,是否会自动重新连接?(使用池时是否需要“句柄断开”功能 2) 使用第一种方法,我可以设置connection.on('error',function(){…})。在
- pool.getConnection()、connection.query()和connection.release()中的任意一个
- 或pool.query
pool.getConnection((err,con)=>{})
下面是我用来在运行查询之前检查连接的代码。希望能有帮助
connect: function ()
{
return new Promise((resolve, reject) => {
let pool = Mysql.createPool({
connectionLimit: config.mysql.connectionLimit,
host: config.mysql.host,
user: config.mysql.user,
password: config.mysql.password,
database: config.mysql.database
});
pool.getConnection((err, con) =>
{
try
{
if (con)
{
con.release();
resolve({"status":"success", "data":"MySQL connected.", "con":pool});
}
}
catch (err)
{
reject({"status":"failed", "error":`MySQL error. ${err}`});
}
resolve({"status":"failed", "error":"Error connecting to MySQL."});
});
});
}