在node-mysql上,connection.end()会去哪里 [英] node-mysql where does connection.end() go

查看:339
本文介绍了在node-mysql上,connection.end()会去哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的对在node-mysql中使用connection.end()感到困惑.

I am really confused with using connection.end() in node-mysql.

我不完全了解它的位置,目前我将其放在查询之后,但是如果我创建一个新查询,则会收到错误Cannot enqueue Query after invoking quit.

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.

现在我的应用有很多支票,这里是其中之一:

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.

我在jsFiddle中进行了更详尽的解释: http://jsfiddle.net/K2FBk/ ,以更好地解释我遇到的问题. node-mysql的文档并未完全说明放置connection.end()

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()

应该在哪里避免该错误?

Where should it go to avoid this error?

推荐答案

文档:

使用end()结束连接,以确保在将退出数据包发送到mysql服务器之前执行所有剩余的查询.

Closing the connection is done using end() which makes sure all remaining queries are executed before sending a quit packet to the mysql server.

然后,仅当您停止向MySQL发送查询时,即应用程序停止时,才调用

connection.end().您不应该一直创建/终止连接:仅对所有查询使用相同的连接(或使用连接池以提高效率).

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).

这篇关于在node-mysql上,connection.end()会去哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆