how much connection.release() must have in node js code?

时间:2016-08-19 04:51:16

标签: javascript node.js

I am using node js for web service I use pool.getConnection() in my code , after use pool.getConnection() for close this connection i am using connection.release(); but in any query i am not use any function for close connection .please tell me this code is valid or not?.

pool.getConnection(function(err,connection){
connection.release();
if(!err){
    email=req.user.email
    connection.query('select * from users where email=?',[email],
    function(err,user_detail){
        if(!err)
        {
                    connection.query('insert into message(??,time,type,user_id) values(?,?,?,?)',[messagecolumns,dbValues,time,type,user_id],
                                function(err,message_inserted){
                                        if(!err)
                                        {
                                                console.log('Message send',message_inserted.insertId);
                                                connection.query('select u.firstname,u.lastname,u.img_path,m.* from users u, message m where u.id=m.user_id and m.id=?',[message_inserted.insertId],
                                                function(err,message_detail){
                                                        if(!err)
                                                        {
                                                            connection.query('select    count(id) as message_count from message where appointment_request_id=?',[appointment_request_id],
                                                            function(err,total_message){
                                                                if(!err)
                                                                {
                                                                        connection.query('update appointment_request set message_count=? where appointment_request_id=?',[total_message[0].message_count,appointment_request_id],
                                                                        function(err,update_message_count){
                                                                        if(!err)
                                                                        {
        }
    });
 }
else {
   console.log('connection Error',err);
}

I feel this is not valid method for connection because from some web site I realize that after SQL query I must close connection but in this code I never use any close connection and my code are some time give me 500 server error and some time it work successfully so I have to fix this error and I want to permanently solution if anyone can help me please give me solution for this. if you have any confutation for understanding you may ask me.

1 个答案:

答案 0 :(得分:1)

池的连接数有限(Default is 10)。

当您getConnection时,其中一个连接从池中被删除并传递给您,以便您可以使用它。在完成连接后,您将在之后致电connection.release() 。一旦调用connection.release(),连接就会传递回池中,并且可以由代码的其他部分自由使用。因此,在您对其进行调用后,使用此连接是无效的,因为它可能会被请求与getConnection连接的代码的其他部分关闭或使用(在大多数情况下,它可能仍然有效,但它可能意外失败)。

在您的代码中,您在回调开始时调用connection.release();,因此connection之后release的使用无效。

您不得在同一连接上多次拨打connection.release()