Hammer
Hammer

Reputation: 8888

Node-mysql multiple queries in a connection

I am using node-mysql in my project. I have a scenario where the execution of the second query will be based on the result returned from the first. For example, first query, select * from table where user=x; then based on the existence I will run another query. I am using pool.getConnection(). May I know whether I should create two connections or use one connection?

Option a)

 pool.getConnection(function(err, connection) {
     // Use the connection
     connection.query( 'SELECT something FROM sometable', function(err, rows) {
         //store the result here as a var a; 
         connection.release();
     });
 });
 //check var a and start another connection to run the second query?

Option b)

pool.getConnection(function(err, connection) {
    // Use the connection
    connection.query( 'SELECT something FROM sometable', function(err, rows) {
       //...based on the result, 
       connection.query(second query)
       connection.release();
    });
});

Thanks Hammer

Upvotes: 1

Views: 2263

Answers (1)

mscdex
mscdex

Reputation: 106736

Both options are using a pool, so you're already using multiple connections. So either option doesn't really matter, the only difference might be that the first option may result in the second query not being executed right away if the entire pool is in use after the first query is done.

Upvotes: 1

Related Questions