[英]How does mysql connection pooling works with Node microservices?
I have two node microservices talking to one common mysql database. 我有两个节点微服务与一个常见的mysql数据库通信。 Both microservices have this below code to create a connection pool with a connectionlimit of 10 as follows:
这两个微服务都具有以下代码来创建连接限制为10的连接池,如下所示:
// Initializing pool //初始化池
var pool = mysql.createPool({
connectionLimit: 10,
host: 'localhost',
port: '3306',
user: 'root',
password: 'root'
});
function addConnection(req, res) {
pool.getConnection(function (err, connection) {
if (err) {
connection.release();
res.json({ "code": 500, "status": "Error" });
return;
}
connection.query("select * from user", function (err, rows) {
connection.release();
if (!err) {
res.json(rows);
}
});
connection.on('error', function (err) {
res.json({ "code": 500, "status": "Error" });
return;
});
});
}
For mysql database I have the max_connections set to 200(SHOW VARIABLES LIKE 'max_connections'; returns 200). 对于mysql数据库,我将max_connections设置为200(类似于SHOW VARIABLES LIKE'max_connections';返回200)。
Ideally it would not; 理想情况下不会; but it can go above 10;
但可以超过10; if suppose some connections become stale ie they are closed from client end but are still open on Server end.
如果某些连接失效,即它们从客户端关闭,但在服务器端仍然打开。
If you have multiple instances of same micro-service deployed in multiple VM or docker containers; 如果您在多个VM或Docker容器中部署了同一微服务的多个实例; they are like independent services.. and they have no connection among each other.. Hence, each of them will create its own 10 connection.
它们就像独立的服务。彼此之间没有连接。因此,它们每个都将创建自己的10个连接。
Firstly if u set connection pool limit as 10; 首先,如果您将连接池限制设置为10; that does NOT mean that during first moment 10 connections would be created.. While creating a pool;
这并不意味着在第一时间将创建10个连接。 you also specify initial connection parameter suppose 5.. so, when service starts only 5 connections would be created.. and more created only when needed.. with UPPER Limit set as defined by parameter max_connections.
您还要指定初始连接参数,假设为5 ..因此,服务启动时将仅创建5个连接,并且仅在需要时才创建更多连接,且UPPER Limit由参数max_connections定义。 Coming back to your question;
回到您的问题; well if you have NOT implemented synchronization etc. properly then yes it is possible that both pools will initialize their INITIAL_CONNECTIONS..
如果您尚未正确实现同步等,那么可以,两个池都可能会初始化其INITIAL_CONNECTIONS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.