kylefarris / node-querybuilder

Node QueryBuilder Adapter for Node.js (tags: nodejs, node, mysql, active record, activerecord, querybuilder, query builder)
49 stars 19 forks source link

mysql connection does not end #21

Closed AbhijetPokhrel closed 6 years ago

AbhijetPokhrel commented 6 years ago

I think that the mysql connection is not terminated and eventually gives too many connection error for busy traffic.I solved it for my project by adding conn.end() before callback in query_exec.js.Are there any alternatives?

kylefarris commented 6 years ago

I feel like I've witnessed this from time to time too. If you can figure out a way to write a test for this scenario and to prove your conn.end() hypothesis, that would be incredibly helpful!

AbhijetPokhrel commented 6 years ago

Well lets say i just want to select users like below

qb.select('name')
    .get('users', (err,response) => {
      console.log(response);
    }
); 

Now when I see the process lists in phpmyadmin i see that its process is live even if the query has succesfully executed

untitled

So lets say If I have more DB requests then this will cause too many connection err as pervious connections are not closed even if their task is done. My point is that these mysql connection should be closed to avoid such situation. I added conn.end() in query_exec.js before callback(err,result) at line 30. Now when i run the same code, as soon as the query is executed the connection is closed and there are no more stacks of unused connections.

kylefarris commented 6 years ago

Are you using the connection pool feature or just using single one-off connections for each query?

kylefarris commented 6 years ago

I just realized that I did not document this feature even though it's been there since the beginning...

If you're not using a connection pool, you should be able to do this in your code to close the connection after completing your query:

qb.select('name')
    .get('users', (err, response) => {
        qb.disconnect(); // DISCONNECT
        console.log(response);
    }
); 

If you are are using a connection pool, you would "release" the connection back to the pool (not disconnect):

qb.select('name')
    .get('users', (err, response) => {
        qb.release(); // RETURN CONNECTION TO POOL
        console.log(response);
    }
); 

I'll address this by adding some new documentation.

Does this resolve your issue?

AbhijetPokhrel commented 6 years ago

Yes definately. Thankyou for your response

kylefarris commented 6 years ago

You're welcome!