NodeJS MySQL - 如何知道连接是否发布

Ank*_*iya 8 mysql database-connection node.js

我正在开发NodeJS + MySQL Web API.

我正在使用mysql npm模块.

我想知道连接是否释放是否有任何功能或变量.喜欢

if(!connection.isRelease() OR !connection.isClose() OR !connection.end()) {
    connection.release();
}
Run Code Online (Sandbox Code Playgroud)

有没有具体的功能知道连接是否发布?

我收到的错误就像"连接已经发布"

cur*_*ips 16

您可以检查连接是否在池中以查看它是否已释放.如果未释放连接,则空闲连接中的索引将为-1.这是一个例子.

var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'localhost',
  user            : 'root',
  password        : ''
});

pool.getConnection(function(err, connection) {
    connection.query( 'SELECT something FROM sometable', function(err, rows) {

      console.log(pool._freeConnections.indexOf(connection)); // -1

      connection.release();

      console.log(pool._freeConnections.indexOf(connection)); // 0

   });
});
Run Code Online (Sandbox Code Playgroud)


kiw*_*123 5

以防万一您无权访问池,您也可以这样做:

connection._pool._freeConnections.indexOf(connection)
Run Code Online (Sandbox Code Playgroud)

这将为您提供与接受的答案相同的答案。