Repository navigation
Description
Hi Patrik.
I have some code which should have simultaneous queries running. I am using a global connection pool by instantiating a new let sqlConn = new sql.Connection(dbconfig) with a pool of max 15 connections.
Using that single connection pool called sqlConn, I am not able to run multiple sqlConn.request() queries concurrently. It always queues requests up and runs them sequentially, instead of executing all called query() functions. Is there a way to stop the queuing?
What is the reason requests are queued if the purpose of NodeJS is to run code concurrently? Problem is that this design stalls every other query in the program. From my understanding, atleast queries which have stream = true should allow other streamed queries to run concurrently, so that node-mssql is between those two functions concurrently instead of using all power to fininsh one and then executing the other. Sequentially running queries should explicitly be written in code by nesting callbacks or using thenable Promises and not by the library doing it automatically for me.
Here is an example:
The connection pool:
let dbConfig = {
user: 'root',
password: 'obviouslyfake',
server: 'localhost',
database: 'test',
requestTimeout: 60000,
connectionTimeout: 3000,
options: {
tdsVersion: '7_3_B'
},
pool: {
max: 15,
min: 10,
idleTimeoutMillis: 3000
}
};
// instantiate new connection pool
let sqlConn = new sql.Connection(dbConfig);Now using that connection pool, we run 3 Promises concurrently, in this example, we load data from the sql server into a cache, all at the same time:
return Promise.all([
CustomerRetriever.preloadCache(report),
ContractRetriever.preloadCache(report),
CustomerDeviceRetriever.preloadCache(report)
]);Each of this preloadCache functions does the same, but when the program is executed, only one of them is returning query results back at a time. The 3 preloadCache functions are called immediately, they don't wait for the one before to be finished. (The word entities is replaced with the actual entity which is cached in the original code)
static preloadCache(report){
return new Promise((resolve, reject) => {
// check count
let amountOfEntities = 0;
let onePercent = 0;
let psCount = sqlConn.request();
let cacheKeyPrefix = 'entity:';
logger.debug(`get amount of entites for preloading`);
psCount.stream = true;
psCount.query('SELECT COUNT(*) AS count FROM entities');
psCount.on('row', (row) => {
// set count
amountOfEntities = row.count;
onePercent = amountOfEntities / 100;
});
psCount.on('error', (err) => reject(err));
psCount.on('done', () => {
mainLogger.info(`preloading ${amountOfEntities} entities`);
// query entities
let done = 0;
let req = sqlConn.request();
req.stream = true;
req.query(`
SELECT * FROM entities
`);
req.on('row', row => {
let entityNumber = row.entityNumber;
let entity = this.build(row);
redis.set(`${cacheKeyPrefix}${entityNumber}`, JSON.stringify(entity));
done++;
report(onePercent, done, 'entity');
});
req.on('done', () => {
resolve();
});
req.on('error', err => reject(err));
});
});
}What really happens is, that in the console you might or might no see two mainLogger.info('preloading ${amountOfEntities} entities'); messages and then you can see how only one of the streamed queries is returning data at a time because the report(onePercent, done, 'entity'); function never gets called from the other queries as only one is ran at a time.
Is this a bug? If I have 15 connections in a pool, in my opinion, I should be able to run 15 streamed queries simlultaneously. Here we see, that only one is executed.
Changing sqlConn.request() to new sql.Request(sqlConn) has no effect.
The documentation does not comply with what is actually happening:
"Internally, each Connection instance is a separate pool of TDS connections. Once you create a new Request/Transaction/Prepared Statement, a new TDS connection is acquired from the pool and reserved for desired action (So one executing query handled by one connection from the pool?). Once the action is complete, connection is released back to the pool. Connection health check is built-in so once the dead connection is discovered, it is immediately replaced with a new one."
Activity
patriksimek commentedon Oct 2, 2015
There is no queue for requests outside transaction. You can try it yourself:
Result:
Result after changing
pool.maxto1:luca-moser commentedon Oct 3, 2015
@patriksimek
Your example works, but it is missing nested queries.
Here is your example extended to correspond with my original code. What I want to happen is to get
console.log('got row on inner request X')from all 3 requests concurrently. We have nested streamed queries in each of the first COUNT(*) queries. But I only seeconsole.log('got row on inner request 1')frominnerRequest1.request3is actually stalled untilinnerReqest1andinnerReqest2are finished. The streamed inner requests are queued/run sequentially. If I move the inner requests to thedoneevent of the outer request, the result is the same.output 1:
If I use your example with the
waitfor delayit works:output 2:
Now without any inner requests but directly querying 3 different tables with stream enabled:
Gives the same result as
output1, the queries are running sequentially.using 3 pools for each query also runs the queries sequentially:
Does the query parser somehow block subsequent requests if there are rows returned? Yes, it does work with
waitfor delaybut not when actually processing rows.patriksimek commentedon Dec 11, 2015
Discussion moved to tediousjs/tedious#319
audiolion commentedon Mar 28, 2016
This is unfortunate because it is the main driver node-mssql relies upon, I am not sure if the other drives fair any better. Maybe the windows native msnodesql driver doesn't have this issue.