&引用;ER“CON”COUNT“错误:连接太多”;从node.js到mysql的池连接出错
我有大约20个node.js文件,它们使用以下配置来访问我的数据库:&引用;ER“CON”COUNT“错误:连接太多”;从node.js到mysql的池连接出错,mysql,sql,node.js,connection-pooling,Mysql,Sql,Node.js,Connection Pooling,我有大约20个node.js文件,它们使用以下配置来访问我的数据库: var pool = mysql.createPool({ host: databaseHost, user: databaseUser, password: databasePassword, database: databaseName, multipleStatements: true }); 这些函数都使用以下模式: pool.getConnection(function (e
var pool = mysql.createPool({
host: databaseHost,
user: databaseUser,
password: databasePassword,
database: databaseName,
multipleStatements: true
});
这些函数都使用以下模式:
pool.getConnection(function (err, connection) {
if (err) {
callback(err);
} else {
// Use the connection
var sql = "...sql statement...";
var inserts = [...inserts...];
connection.query(sql, inserts, function (error, results, fields) {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) {
callback(error);
} else {
callback(null, results);
}
});
}
});
我最近开始出现错误:
"ER_CON_COUNT_ERROR: Too many connections"
在调用我的任何函数时。我对游泳池的概念不是很了解。如果每个函数都在创建一个池,那么每次调用该函数时是否会创建一个单独的池
我理解获取连接和释放连接。只是没有真正得到createPool
我尝试记录以下内容:
console.log(pool.config.connectionLimit); // passed in max size of the pool
console.log(pool._freeConnections.length); // number of free connections awaiting use
console.log(pool._allConnections.length); // number of connections currently created, including ones in use
console.log(pool._acquiringConnections.length); // number of connections in the process of being acquired
结果是:
10
0
0
0
我可以增加连接的数量,但希望更好地了解问题存在的原因。如果每次必须有查询时都在函数内部调用createPool,那么是的,这是严重的!相反,只为mysql连接使用不同的文件。编写一个类,在其中在函数中创建一个池,然后在构造函数中只需从池返回连接即可。这样,如果您只需要在项目中的任何位置使用该文件,并创建该类的对象,那么您就可以简单地使用它来查询和发布 你能举个例子吗?另外,如果调用创建并返回连接的函数,为什么会有什么不同?该函数是否会被多次调用,从而导致创建多个池?