Node-mysql连接中的多个查询

时间:2014-05-25 16:48:50

标签: mysql node.js

我在项目中使用node-mysql。我有一个场景,第二个查询的执行将基于从第一个返回的结果。例如,首先查询,从表中选择*,其中user = x;然后基于存在我将运行另一个查询。我正在使用pool.getConnection()。我可以知道是否应该创建两个连接或使用一个连接吗?

选项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?

选项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();
    });
});

由于 锤

1 个答案:

答案 0 :(得分:1)

两个选项都使用池,因此您已经在使用多个连接。因此,任何一个选项都不重要,唯一的区别可能是第一个选项可能导致第二个查询没有立即执行,如果在第一个查询完成后整个池正在使用中。