节点 Mysql 在调用退出后无法入队查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14333272/
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 Cannot Enqueue a query after calling quit
提问by Kinjal Dixit
where do i close the mysql connection?
我在哪里关闭mysql连接?
I need to run queries in sequence. I am writing code that looks like this at present:
我需要按顺序运行查询。我目前正在编写如下所示的代码:
var sqlFindMobile = "select * from user_mobiles where mobile=?";
var sqlNewUser = "insert into users (password) values (?)";
//var sqlUserId = "select last_insert_id() as user_id";
var sqlNewMobile = "insert into user_mobiles (user_id, mobile) values (?,?)";
connection.connect(function(err){});
var query = connection.query(sqlFindMobile, [req.body.mobile], function(err, results) {
if(err) throw err;
console.log("mobile query");
if(results.length==0) {
var query = connection.query(sqlNewUser, [req.body.password], function(err, results) {
if(err) throw err;
console.log("added user");
var user_id = results.insertId;
var query = connection.query(sqlNewMobile, [user_id, req.body.mobile], function(err, results) {
if(err) throw err;
console.log("added mobile");
//connection.end();
});
});
}
});
//connection.end();
(I am a beginner with node, npm-express and npm-mysql. I have tried searching SO for "express mysql cannot enqueue" to find related questions and have not found them.)
(我是 node、npm-express 和 npm-mysql 的初学者。我尝试搜索 SO 中的“express mysql cannot enqueue”以找到相关问题,但没有找到。)
回答by unlimited
回答by matthewtole
If you're using the node-mysqlmodule by felixgethen you can call connection.end() at any point after you've made all of the connection.query() calls, since it will wait for all of the queries to finish before it terminates the connection.
如果您正在使用felixge的node-mysql模块,那么您可以在完成所有 connection.query() 调用后随时调用 connection.end(),因为它将等待所有查询完成在它终止连接之前。
See the example herefor more information.
If you're wanting to run lots of queries in series, you should look into the asyncmodule, it's great for dealing with a series of asynchronous functions (i.e. those that have a callback).
如果您想连续运行大量查询,您应该查看async模块,它非常适合处理一系列异步函数(即具有回调的函数)。
回答by user3413723
Maybe the problem is that the mySQL query is executed after the connection is already closed, due to the asynchronous nature of Node. Try using this code to call connection.end() right before the thread exits:
也许问题是由于 Node.js 的异步特性,mySQL 查询是在连接已经关闭后执行的。尝试使用此代码在线程退出前立即调用 connection.end() :
function exitHandler(options, err) {
connection.end();
if (options.cleanup)
console.log('clean');
if (err)
console.log(err.stack);
if (options.exit)
process.exit();
}
//do something when app is closing
process.on('exit', exitHandler.bind(null, {cleanup: true}));
Code adapted from @Emil Condrea, doing a cleanup action just before node.js exits
改编自 @Emil Condrea 的代码,在 node.js 退出之前执行清理操作
回答by Kyle Roux
In my case connection.end was being called in a spot that was hard to notice, so an errant call to connection.end could be the problem with this error
在我的例子中,connection.end 在一个很难注意到的地方被调用,所以对 connection.end 的错误调用可能是这个错误的问题