简体   繁体   English

Node.js应用程序的node-mysql连接池“无可用连接”

[英]node-mysql Connection Pooling “No connections available” for Node.js Application

I've got a website I've written that gets data from a Restful web service. 我已经编写了一个网站,该网站从Restful Web服务获取数据。 That Restful web service is run off node.js and MySQL using node-mysql. Restful Web服务是使用node-mysql在node.js和MySQL上运行的。 The problem I am running into is with connections running out. 我遇到的问题是连接用完了。 For example, my default page does a bunch of lookups to get data. 例如,我的默认页面进行了大量查找以获取数据。 On the 9th refresh of that page, one of my lookups to the Restful API throws this error "No connections available." 在该页面的第9次刷新中,我对Restful API的查找之一抛出此错误“无可用连接”。

Here is kind of how things are architected on the Restful API side: 这是在Restful API方面构建事物的方式:

In server.js: 在server.js中:

var db = require('mysql');
var db_pool = db.createPool(
{
    host : 'localhost',
    connectionLimit: 100,
    supportBigNumbers: true,
    waitForConnections: false
});

setRoutes(server, db_pool, passport, LocalStrategy, jwt, tokenSecret, bcrypt);

In router.js: 在router.js中:

setRoutes = function(server, connectionPool, passport, LocalStrategy, jwt, tokenSecret, bcrypt)
{
    require('./lib/places.js');
    place.connectionPool = connectionPool;
}

server.get('api/place/:id', function(request, response)
{
    response.header("Access-Control-Allow-Origin", "*");
    response.header("Access-Control-Allow-Headers", "X-Requested-With");

    place.get(request.params.id, function(err, data)
    {
        response.send(data);
        next();
    });
});

Finally, in places.js: 最后,在places.js中:

place.connectionPool = null;

place.get = function(id, callback)
{
    place.connectionPool.getConnection(function(error, connection) 
    {
        var query = "select myColumn from myTable";

        connection.query
        (
            query,
            function (queryError, rows, fields)
            {
                try {connection.release();} catch(e){};

                if (queryError)
                {
                    console.log(JSON.stringify(queryError));
                    return (callback(null, queryError));
                }

                return (callback(null, rows));
            }
        );
    });
}

What is the best practice for implementing node-mysql and connection pooling for websites? 为网站实现节点MySQL和连接池的最佳实践是什么?

Thanks! 谢谢!

解决了连接问题-我并未在进行回调的所有位置释放连接。

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

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