dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

Mysql connections number keep growing even using `db.close()` #795

Open zjhsdtc opened 7 years ago

zjhsdtc commented 7 years ago

Hi, I'm just struggling with this issue for hours and I think I can not figure it out by myself, so I put this question here. I got some database operation functions like below, and the problem is even I use db.close() after the query is finished, every time when queryAllThreads is invoked, the number of connections in mysql database is still keep growing and never decrease its number, so am I missing anything in the document to close the database connection? How can I fix this because I will invoke this query function every minute, and it will reach the maximum number of database connections in a relatively short time. Thanks in advance!

var opts = {
    host: dbhost,
    database: dbname,
    user: dbuser,
    password: dbpass,
    protocol: 'mysql',
    port: dbport,
    query: {pool: true, debug: true}
}

var threadModel = {
    websiteUrl: String,
    threadUrl: { type: 'text', big: true },
    username: String,
    password: String,
    schedule: { type: 'text' },
    content: { type: 'text', big: true },
    remark: String,
    createTime: String,
    createTs: { type: 'integer' },
    updateTime: String,
    updateTs: { type: 'integer' }
}

exports.queryAllThreads = function (callback) {
    orm.connect(opts, function (err, db) {
        var threads = db.define("threads", threadModel);
        db.sync(function (err) {
            if (!err) {
                threads.find({}, function (err, results) {
                    db.close();
                    if (err) {
                        console.log("error when query threads => " + err);
                    } else {
                        callback(results);
                    }
                });
            }
        });
    });
}
screen shot 2017-06-29 at 1 30 32 am