2016-02-16 147 views
0

我实际上使用这个MYSQL Client,当我关闭一个连接实际上从来没有关闭,所以我可以看到在状态。NodeJS,不关闭mysql连接

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

回答

3

conn.end()移动出来的查询回调 - 如在node-mysql's documentation描述:

你的连接上调用的每个方法进行排队,并依次执行。

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

connection.connect(); 

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) { 
    if (err) throw err; 

    console.log('The solution is: ', rows[0].solution); 
}); 

connection.end(); 
+0

您好,我尝试,并没有工作,我也尝试用该方法'conn.destroy()',并没有工作。 –

0

您也可以使用游泳池。

检查this link

可以集中连接以简化共享单个连接,或管理多个连接的 。

当您完成连接时,只需调用connection.release() 并且连接将返回到池中,准备再次被其他人使用 。

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