Can you please provide some example code for creating and ending a pool once all connections have finished? Otherwise my script never exits.
var pool = mysql.createPool(poolConfig);
/// Do stuff
pool.end(function onEnd(error) {
if (error) throw error;
// All connections are now closed once they have been returned with connection.release()
// i.e. this waits for all consumers to finish their use of the connections and ends them.
});
Thanks, I tried this.
https://gist.github.com/codecowboy/bc34aac1ffe3651a8f07
The pool now ends prematurely.
pool ended
Post ID: 19383609
[Error: Pool is closed.]
/Users/lukemackenzie/Development/Node/tennistracker/main.js:119
connection.query( "SELECT COUNT(*) AS no_rows FROM mtf_posts W
^
TypeError: Cannot call method 'query' of undefined
at /Users/lukemackenzie/Development/Node/tennistracker/main.js:119:28
at /Users/lukemackenzie/node_modules/mysql/lib/Pool.js:24:14
at process._tickCallback (node.js:415:13)
To be honest, I don't think I actually need a pool but I think this might be important to my understanding of the event loop and non-blocking I/O in general so would be grateful for a reply.
You end the pool in the same tick as its creation. So connection.query does not exist, when you later actually call it.
You need to issue pool.end(), when you have your last result in not before. (Damn you async! ;-))
And for your example, I do not see the need for pool, either. Use one connection for the whole script it is one layer less to maintain.
Thanks! For my understanding, would you be able to explain how I could end the pool at the appropriate time?
When you have gotten back your final result the last time a callback is called.
So in your example, when you have crawled all web pages and stored the data in the database.
You need to find a way to determine, when your last request is actually processed. Search for "node waterfall" on google, there are some blogs and tutorials about this topic and patterns to solve it.
You really do not need the pool for that, since you should not dispatch too many http-requests anyway, or your host will be shut down, because of a Denial-Of-Service attack.
Thanks. In case it helps anyone, I went with https://gist.github.com/codecowboy/d8477d942aa1f75009ca
ok, then I close this issue for now.
@codecowboy Your gist is down :cry: