【发布时间】:2015-11-01 15:40:33
【问题描述】:
想知道在带有连接池的nodejs中编写mysql查询的最佳实践。找到了一些相关的线程。但他们都没有回答确切的问题。所以开始一个新的。
var mysql = require('mysql');
var pool = mysql.createPool(...);
pool.getConnection(function(err, connection) {
// insert into first table
connection.query( 'Insert into table values(....)', function(err, rows) {
//get the auto incremented value from first insert and use it in the second insert
connection.query("insert into table2 values(rows.insertID,..)",function(err,rows){
//release the connection to pool after performing all the inserts
connection.release();
});
});
}); `
疑问:
- 这是编写这些查询的正确方法吗
- 使用 pool 时需要使用 end() 。我的理解是,当我们使用 release() 函数时,连接将回到池中,我们可以重用它。那么是否需要在任何地方使用 end()。
【问题讨论】: