node-mysql connection.end() 在哪里
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20692989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node-mysql where does connection.end() go
提问by Sir
I am really confused with using connection.end() in node-mysql.
我真的对在 node-mysql 中使用 connection.end() 感到困惑。
I don't fully understand where it goes, at the moment i place it after a query but then if i create a new query i get an error Cannot enqueue Query after invoking quit.
我不完全明白它的去向,目前我将它放在查询之后,但是如果我创建一个新查询,我会收到一个错误 Cannot enqueue Query after invoking quit.
Now my app has a bunch of checks going here is one of them:
现在我的应用程序有一堆检查在这里是其中之一:
socket.on('connect', function(data,callBack){
var session = sanitize(data['session']).escape();
var query = connection.query('SELECT uid FROM sessions WHERE id = ?', [session],
function(err,results){
if(err){
console.log('Oh No! '+err);
}else{
io.sockets.socket(socket.id).emit('connectConfirm',{data : true});
}
connection.end();
});
});
Now after that if i have any other query i get the error occuring.
现在,如果我有任何其他查询,则会出现错误。
I made a more informed explaination in my jsFiddle: http://jsfiddle.net/K2FBk/to better explain the problem I'm getting. The documentation for node-mysql does not totally explain the correct place to put connection.end()
我在我的 jsFiddle: http://jsfiddle.net/K2FBk/ 中做了更明智的解释,以更好地解释我遇到的问题。node-mysql 的文档并没有完全解释放置的正确位置connection.end()
Where should it go to avoid this error?
应该去哪里避免这个错误?
采纳答案by Paul Mougel
Per the documentation:
根据文档:
Closing the connection is done using end() which makes sure all remaining queries are executed before sending a quit packet to the mysql server.
关闭连接是使用 end() 完成的,它确保在向 mysql 服务器发送退出数据包之前执行所有剩余的查询。
connection.end()
is then supposed to be called only when you stop sending queries to MySQL, i.e. when your application is stopping. You shouldn't create/end connections all the time: just use the same connection for all your queries (or use a connection pool to be more efficient).
connection.end()
然后应该只在您停止向 MySQL 发送查询时调用,即当您的应用程序停止时。您不应该一直创建/结束连接:只需对所有查询使用相同的连接(或使用连接池来提高效率)。
回答by VIKAS KOHLI
socket.on('connect', function(data,callBack){
var session = sanitize(data['session']).escape();
var query = connection.query('SELECT uid FROM sessions WHERE id = ?', [session],
function(err,results){
if(err){
console.log('Oh No! '+err);
}else{
io.sockets.socket(socket.id).emit('connectConfirm',{data : true});
}
});
connection.end(); // close connection outside the callback
});
Its giving error because you are closing connection during en-queue
它给出错误,因为您在排队期间关闭连接