[英]Nodejs Mysql connection pooling using mysql module
We are using mysql module for node and i was just wondering if this approach is good or does it have any bad effects on our application, consider this situation 我们正在为节点使用mysql模块,我只是想知道这种方法是否好,或者对我们的应用程序有不好的影响,请考虑这种情况
dbPool.getConnection(function(err, db) {
if(err) return err;
db.query()
Here i am calling the dbPool object and requesting a connection from the pool then using it. 在这里,我正在调用dbPool对象,并从池中请求连接,然后使用它。 However i found another implementation (which is the one i am asking about) which uses the dbPool object directly like: 但是我发现了另一个实现(这是我要问的问题),它直接使用dbPool对象,例如:
dbPool.query('select * from test where id = 1' function(err, rows) {})
so i was wondering what does the second implementation does exactly, does it automatically return a free connection and use it ? 所以我想知道第二个实现到底是做什么的,它会自动返回一个免费连接并使用它吗? can explain what is happening exactly in the second case and if it has any effect + or - on my application ? 可以解释第二种情况到底发生了什么,以及它对我的应用程序有何影响? Thank you. 谢谢。
So this is so what called callback chaining. 因此,这就是所谓的回调链。 In NodeJS you have a lot of asynchronous calls going around. 在NodeJS中,有很多异步调用。 But sometimes you want to do something when the connection is done with MySQL. 但是有时候,当使用MySQL完成连接时,您想做些什么。 That's why the getConnection
functionality has a callBack
feature. 这就是为什么getConnection
功能具有callBack
功能。
dbPool.getConnection(function(err, db) {
if(err) return err;
db.query()
Is equal to this: 等于这个:
dbPool.query('select * from test where id = 1' function(err, rows) {})
dbPool.query()
will wait for the connection to be open, you don't have to put all your queries inside the getConnection to make it work. dbPool.query()
将等待连接打开,您不必将所有查询放入getConnection即可使其正常工作。 This is why it also has a callBack feature. 这就是为什么它还具有回调功能。
Tell me if I'm wrong. 告诉我我是否错。 I hope this solves your question. 我希望这能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.