我有一个套接字,可通过UDP监听事件(字符串)。每次获取字符串时,都会生成一个SQL请求。我的问题是使它异步运行,以便请求排队,而在另一个查询运行时不尝试访问数据库。
伪:
socket.on('message', function (msg) {
switch (msg) {
case "case1":
storeRows();
break;
//there's a new message every 1-2 secs
...
}
var rows = []; //push results of all queries here
function storeRows() {
rows.push(getFromDB());
}
function getFromDB() {
var sqlString = "SELECT * ..."
var req = new Req(sqlString, function(err, rowCount) {
...
}
var resultsArray = [];
req.on('row', function (cols) {
//add results to resultsArray
}
return resultsArray;
}
基本上,我需要getFromDB()才能异步运行,在运行下一个查询之前,等待上一个查询完成。我不知道该怎么办。我正在使用tedious.js访问SQL Server数据库。
编辑:
var config = {
userName: "admin",
password: "pw",
server: "test",
domain: "test",
options: {
port: '10322'
}
}
connection = new Connection(config);
connection.on('connect') {
isConnected = true;
}
getCoordinates(vehicleID, fromTime, toTime) {
var SQLString = "Select * FROM coordinates WHERE TimeStamp BETWEEN '" + fromTime + "' AND '" + toTime + "' AND vehicleID = " + vehicleID;
var rowsToSend = [];
var req = new Req(SQLString, function(err, rowCount) {
if (err) console.log(err)
else console.log(rowCount);
}
req.on('row', function (columns) {
var rowArray = [];
columns.forEach(function (column) {
var colValue = column.value;
switch (column.metadata.colName) {
case "ID":
if (rowArray.length > 0)
rowsToSend.push(rowArray);
rowArray = new Array();
break;
default:
rowArray.push(colValue);
break;
}
});
rowsToSend.push(rowArray);
});
connection.execSql(req);
req.on('doneProc', function () {
return(rowsToSend);
}
}
//called every few seconds
function runQueries() {
someArray.push(getCoordinates ());
}
答案 0 :(得分:1)
您可以构建类似于Processor之类的东西,它构建基于数组的队列,并且一次只能在数据库上执行一个查询。这是一个使用带有诺言的async / await函数的示例:
class Processor {
constructor() {
this.queue = [];
this.queryRunning = false;
}
// Main query function.
// Add the query to a queue, and try to process the queue.
query(query) {
return new Promise((resolver) => {
this.queue.push({
query,
resolver
});
this.tryProcessNext();
});
}
// Try to process the next element of the queue
tryProcessNext() {
let self = this;
// If it's running, or queue is empty, simply do nothing
if (this.queue.length === 0 || this.queryRunning)
return;
this.queryRunning = true;
// Get the first added element to the queue (And shorten the array at the same time)
let qry = this.queue.shift();
// Run query, and use `qry.resolver()` to resolve the original promise.
setTimeout(function() {
qry.resolver(`Result: ${qry.query}`);
self.queryRunning = false;
// If there's anything next in the queue, try to process that next
self.tryProcessNext();
}, 1500);
}
}
const proccessor = new Processor();
let i = 0;
// A simple function to demonstrate adding queries via a websocket.
setInterval(async function() {
console.log(`Adding query: QUERY ${++i}`);
var res = await proccessor.query(`QUERY ${i}`);
console.log(res);
}, 1000);