1

We want to have multiple concurrent requests without using node cluster i.e. if we requests 3 times then we should have the execution ending almost at the same time.

This is sample code:

const execute_select = async () => {
    const jdbcres = new Driver(Conn.MySQL, {host, port, database})
    const d = await jdbcres.sql(‘SELECT * from db.maindb’); // takes 10 seconds to complete

}

app.post(‘/select, (request, response) => {
 execute_select();
 response.send(‘200’);
});

If we execute http://localhost/select 3 times (at the same time), what happens is the following:

  1. We immediately get a response back.
  2. Each of the requests are blocked at the following line const d = await jdbcres.sql(‘SELECT * from db.maindb’)
  3. First one ends within 10 seconds followed by the next one which ends in 10s followed by the next one for a total of about 30seconds.

Is there a way to have calls to /select not waiting for the previous request to end i.e. the total time should be around 10seconds?

My other attempt was to have this in place of the following line: const d = await jdbcres.sql(‘SELECT * from db.maindb’); // takes 10 seconds to complete

/* const result = await new Promise((resolve, reject) => {
   setTimeout(() => {
        const d = jdbcres.sql('SELECT * from db.maindb')
        resolve(d);
    }, 20000);
    });
*/

Please find a list of related pages that I have read:

  1. NodeJS Express Async Not Handling More Requests
  2. Will using async/await in an express server block all the routes while one is being used?
  3. NodeJS Express Async Not Handling More Requests
  4. Concurrency in node js express app for get request with setTimeout
poyango
  • 11
  • 1
  • What library do you use for mysql? – Konrad Jul 16 '23 at 19:22
  • You should await for ` execute_select();` like `await execute_select();` – Konrad Jul 16 '23 at 19:22
  • node-jdbc, if we await on execute_select then any requests to /select will be blocked in app.post request immediately i.e. we won't get any response – poyango Jul 16 '23 at 19:23
  • If you mean [this](https://www.npmjs.com/package/jdbc) then this library hasn't been updated for two years. It doesn't support promises so using `await` does nothing. The reason it's blocking the thread is that it's poorly designed. I would suggest using `mysql2` – Konrad Jul 16 '23 at 19:31
  • Yes, we have our code wrapped around it. The issue is that we have lots of dbs and we wanted a generic solution. – poyango Jul 16 '23 at 19:37
  • Suppose we have a code which hangs e.g. converting values returned from the select query to an array using toObjArray(). How, do we make that asynchronous? I suppose the same idea can be applied to this question. – poyango Jul 16 '23 at 19:39
  • You can use `child_process` to run task in another process or worker thread – Konrad Jul 16 '23 at 21:33

0 Answers0