简体   繁体   English

什么时候应该在node.js mysql中使用连接池?

[英]When should one use connection pooling in node.js mysql?

This is the typical way I create MySQL connection in node.js 这是我在node.js中创建MySQL连接的典型方式

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '',
  database : 'address_book'
});
var app = express();

connection.connect(function(err){
if(!err) {
    console.log("Database is connected ... nn");    
} else {
    console.log("Error connecting database ... nn");    
}
});

Is it good enough for production use? 它对生产使用是否足够好? When should one use connection pooling? 什么时候应该使用连接池? What are the advantages and disadvantages of using connection pooling? 使用连接池有哪些优缺点?

This will work but creating mysql connection for each and every request is not recommended. 这将有效,但不建议为每个请求创建mysql连接。

So it would be better to have a pool of mysql connections, as cost of getting a connection will be nil compared to prior case. 因此,拥有一个mysql连接池会更好,因为与先前的情况相比,获得连接的成本将是零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM