本文介绍了NodeJS MySQL-如何知道连接是否释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发NodeJS + MySQL Web API.
I am developing NodeJS + MySQL web API.
我正在使用 mysql npm模块.
I am using mysql npm module.
我想知道连接是否已释放或没有任何函数或变量.喜欢
I want know connection is release or not is there any function or variable.Like
if(!connection.isRelease() OR !connection.isClose() OR !connection.end()) {
connection.release();
}
是否有任何特定功能可以知道连接是否释放?
Is there any specific function to know connection is release or not?
我收到诸如连接已释放"之类的错误消息
I am getting error like "connection already release"
推荐答案
您可以检查连接是否在池中以查看其是否已释放.如果未释放连接,则空闲连接中的索引将为-1.这是一个例子.
You can check if the connection is in the pool to see if it was released. The index in the free connections will be -1 if the connection is not released. Here is an example.
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
});
});
这篇关于NodeJS MySQL-如何知道连接是否释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!