简体   繁体   English

nodeJS mysql库如何顺序执行查询

[英]nodeJS mysql library how to serially execute query

Say for example if I have code like below would it execute serially or parallely? 例如,如果我有如下代码,它将以串行还是并行方式执行?

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
  database : 'my_db'
});

connection.connect();

let transaction = 'START TRANSACTION; //DO SOMETHING COMMIT;'

connection.query(transaction, function (error, results, fields) {
  if (error) throw error;
  console.log(results);
});

connection.query(transaction, function (error, results, fields) {
  if (error) throw error;
  console.log(results);
});

connection.end();

It does say 它确实说

The MySQL protocol is sequential, this means that you need multiple connections to execute queries in parallel. MySQL协议是顺序的,这意味着您需要多个连接才能并行执行查询。 You can use a Pool to manage connections, one simple approach is to create one connection per incoming http request. 您可以使用池来管理连接,一种简单的方法是为每个传入的HTTP请求创建一个连接。

On the website but I am wondering if 'sequential' means the queries are sent to the database sequentially but executed 'parallely' or if it means whether the next query is never sent until the first query is fully executed (which is what I mean by serial). 在网站上,但我想知道“顺序”是指将查询顺序发送到数据库但是“并行”执行的,还是表示是否在完全执行第一个查询之前是否不发送下一个查询(这就是我的意思?序列号)。

If they are being 'executed' parallely in the database, how can I make sure that each query is served as an individual transaction ? 如果并行地在数据库中“执行”它们,如何确保每个查询作为一个单独的transaction I am asking because I have to treat multiple queries (potentially simultaneously requested to server) to be executed serially in the database. 我问是因为我必须处理要在数据库中串行执行的多个查询(可能同时向服务器请求)。 Of course this is possible under multiple connections (multiple connection to mysql server with different terminal instance) but how can I simulate that environment using this mysql nodejs library? 当然,这可以在多个连接(具有不同终端实例的mysql服务器的多个连接)下进行,但是如何使用此mysql nodejs库模拟该环境?

I can't use promises or callbacks because I do not know how many or when the queries will be executed. 我不能使用Promise或callbacks,因为我不知道查询将执行多少次或何时执行。 I will only be able to call connection.query every time query is made from the client to the server. 每次从客户端到服务器进行查询时,我只能调用connection.query

You can leverage javascript promise to solve your problem. 您可以利用javascript promise解决您的问题。 You can check https://github.com/tuhinpaul/syp-model/wiki/Programmatic-(Dynamic)-Chaining-and-Recursive-Functions-with-JavaScript-or-Node.js-Promises to understand how to serially execute tasks. 您可以检查https://github.com/tuhinpaul/syp-model/wiki/Programmatic- (动态)-Chaining-and-Recursive-Functions-with-JavaScript-or-Node.js-Promises以了解如何顺序执行任务。

The syp-model package provides the ability to execute queries sequentially although documentation needs more work at this time; syp-model软件包提供了按顺序执行查询的功能,尽管此时文档需要做更多工作。 and it does not have integrated transaction support yet. 并且它还没有集成的交易支持。

Was it helpful? 有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM