The mysql module is used to create connection pool, which leads to the problem that the link is not released and blocked.

  node.js, question

Used in the projectExpress, usedexpress-myconnectionMiddleware creates connection pools with a limit of 10 connections. In the process of operation, if the number of links exceeds the limit, it will be blocked directly. No matter waiting for Takuya to make new inquiries, it will not report an error and obtain links again.
The reason for this problem must be that the created link has not been released. However, the source code of the middleware is only a simple encapsulation of mysql module, and the call is also mademysql.createPool.
So I want to know whether this situation is my code problem or mysql module’s own problem, please comment!

Run code

exports.index = function(req, res) {
 console.log('start');
 req.getConnection(function(err,con){
 console.log('got connection');
 if(err){
 res.end('err');
 bracket
 var sql='select id from integrated_db.community'
 con.query(sql,[],function(err,data){
 res.end('data');
 });
 });
 bracket

ConsoleRefresh the page several times and the output will continue:

> start
 > got connection
 > start
 > got connection
 > start got
 > connection
 > start
 > got connection

ConsoleWhen there are more refreshes, only:

> start
 > got connection
 > start
 > start
 > start
 > start
 > start

I’ve been stuck since then, and there won’t be any new links.
Please advise.

I have found the reason why the connection pool is stuck! Let’s say that if any students are using express-myconnection module, they can change it.

Cause of problem: express-myconnectionThe module has a defect in link release.
Resolution:(Original)https://github.com/pwalczyszy …
Specific operations:Just modify the way it releases the link ~ the steps are as follows

//The original connection closing method
 var end = res.end;
 res.end = function (data, encoding) {
 console.log('end2');
 // Ending request connection if available
 if (requestConnection) requestConnection.end();
 
 // Releasing pool connection if available
 if (poolConnection) poolConnection.release();
 
 res.end = end;
 res.end(data, encoding);
 bracket

Revised version:

// Request terminanted unexpectedly.
 res.on("close", function () {
 // Ending request connection if available
 if (requestConnection) requestConnection.end();
 
 // Releasing pool connection if available
 if (poolConnection) poolConnection.release();
 
 console.log('close1');
 });
 // Normal response flow.
 res.on("finish", function () {
 // Ending request connection if available
 if (requestConnection) requestConnection.end();
 
 // Releasing pool connection if available
 if (poolConnection) poolConnection.release();
 
 console.log('finish1');
 });