简体   繁体   English

Node.js,ORM2和Mysql。 连接未关闭

[英]Node.js, ORM2 and Mysql. Connection are not closing

I am using node.js, express, ORM2 and Mysql. 我正在使用node.js,express,ORM2和Mysql。 Everytime a page loads a new mysql connection is opened. 每次页面加载时,都会打开一个新的mysql连接。 The issue here is that the connection doesn't close, it stays open. 这里的问题是连接没有关闭,而是保持打开状态。 So, each requests result a new "sleep" status connection in my mysql "show processlist" command. 因此,每个请求都会在我的mysql“ show processlist”命令中产生一个新的“ sleep”状态连接。

Thanks, Radu 谢谢Radu

Actually because i am new at node i did not realize that my application never ends the execution and i have to use singleton method for the mysql connection. 实际上,因为我是节点上的新手,所以我没有意识到我的应用程序永远不会结束执行,因此我必须对mysql连接使用单例方法。

Using something like: 使用类似:

if (connection) return cb(null, connection);
orm.connect(settings.database, function (err, db) {
    if (err) return cb(err);
    connection = db;
    db.settings.set('instance.returnAllErrors', true);
    setup(db, cb);  
});

This will keep just one mysql connection open. 这将仅打开一个mysql连接。

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

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