NodeJS,不关闭mysql连接

时间:2016-02-16 16:39:22

标签: javascript mysql node.js

我实际使用此MYSQL Client,当我关闭连接时实际上永远不会关闭,所以我可以看到状态。

router.get('/test', function (req, res, next) {

    var conn = mysql.createConnection(config);

    conn.connect();

    conn.query('select  * from invoices ', function (err, result) {

        if (err) {
            throw err;
        }

        res.status(200).json({result: result});
        conn.end();// || conn.destroy();
    });

});

Connections

2 个答案:

答案 0 :(得分:3)

function reworkedInBetweenDays(year, month, day) { var today = new Date(); var fromdate = new Date(year, month - 1, day); var yearsDiff = today.getFullYear() - fromdate.getFullYear(); var monthsDiff = today.getMonth() - fromdate.getMonth(); var daysDiff = today.getDate() - fromdate.getDate(); if (monthsDiff < 0 || (monthsDiff === 0 && daysDiff < 0)) yearsDiff--; if (monthsDiff < 0) monthsDiff += 12; if (daysDiff < 0) { var fromDateAux = fromdate.getDate(); fromdate.setMonth(fromdate.getMonth() + 1, 0); daysDiff = fromdate.getDate() - fromDateAux + today.getDate(); monthsDiff--; } var result = []; if (yearsDiff > 0) result.push(yearsDiff + (yearsDiff > 1 ? " years" : " year")); if (monthsDiff > 0) result.push(monthsDiff + (monthsDiff > 1 ? " months" : " month")); if (daysDiff > 0) result.push(daysDiff + (daysDiff > 1 ? " days" : " day")); return result.join(', '); } console.log(reworkedInBetweenDays(2013, 4, 8)); console.log(reworkedInBetweenDays(2014, 1, 16)); console.log(reworkedInBetweenDays(2016, 1, 31)); console.log(reworkedInBetweenDays(2017, 2, 16));移出查询回调 - 如node-mysql's documentation

中所述
  

您在连接上调用的每个方法都会按顺序排队并执行。

     

使用end()关闭连接,确保在将退出数据包发送到mysql服务器之前执行所有剩余的查询。

zadd myset 0 "action_1"
zadd myset 1 "action_2"
zadd myset 1 "action_3"

答案 1 :(得分:0)

你也可以使用游泳池。

检查this link

  

可以合并连接以简化单个连接的共享,或者   管理多个连接。

     

完成连接后,只需调用connection.release()   并且连接将返回池,准备再次使用   别人。

pool.end(function (err) {
  // all connections in the pool have ended
});