0

我正在使用 promise-mysqlNodeJs 库对 MySQL 服务器进行等待操作。

目前我有一个简单的配置:

 let pool = await mysql.createPool(dbConfig);
 let connection = await pool.getConnection();

 /// after the connection is made on the program start, once,
 /// some querying using the connection follows

但是,似乎只有一个连接断开了。问题:如果当前连接断开,如何实现断开连接的自动处理并建立后续的新连接?

4

1 回答 1

0

这对我来说是可行的解决方案

pool.getConnection((err, connection) => {
  if (err) {
    console.log('Error while connecting ', err)
  } else {
    if (connection) connection.release()
    console.log('Database Connected Successfully!')
  }
})

let connection = await pool.getConnection();用上面的代码替换它。并使用池而不是连接执行查询。因此,如果您的单个连接出现故障或正忙,那么您的查询将使用池中的另一个连接执行。

于 2020-05-04T06:50:10.733 回答