My Node.js Code is like below
CODE1: below
var http=require('http');
var MySQL = require('mysql');
mysql = MySQL.createConnection(...)
http.createServer(function(req, res){
// the query will take several seconds
mysql.query("SELECT SLEEP(1)", function....)
});
http.listen(...);
The problem is the server will be crash when I refresh the page too fast. I think is the node-mysql module's problem, it process the query in a queue.So I try to create a connection pool.
CODE2: below
....
var pool = require('generic-pool');
var mp = pool.Pool({
...
create: function(cb){
client = MySQL.createConnection(...);
cb(null, client)
},
max: 10, // up to 10 connection
min: 2,
...
});
....
mp.acquire(function(err, mysql){
// the query will take several seconds
mysql.query("SELECT SLEEP(1)", function....)
mp.release(mysql);
});
....
But the problem still here, Why? How can I fix this.
EDIT: I launch 100 requests with 100 concurrency, 10 seconds expected. But it take 20 seconds. Why? Is The pool only support up to 5 connection?