-
Notifications
You must be signed in to change notification settings - Fork 2.5k
lazy getting connections from pool #762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
It's already possible with pool - just use |
But pool.query() immediately release a connection after query, it makes impossible to use transactions |
I'm not so sure if this is really needed in this library. I think this is more of a question about your server architecture. For example, you do not need to get lazy connections for your example if you implemented your routes like the following: var pool = mysql.createPool({...});
function getMySQLConnection( req, res, next ){
pool.getConnection(function( err, conn ){
if (err) return next(err);
req.mysql = conn;
res.on("finish", function _onfinish(){
res.removeListener("finish", _onfinish);
req.mysql.release();
});
});
}
app.get("/1", getMySQLConnection, function(req, res){
req.mysql.query("SELECT field FROM data", function( err, data ){
res.send("QUERY");
});
});
app.get("/:field", function(req, res){
res.send("OK");
}); As you can see, if you moved your middleware to add the Above, your conditional in |
Thank you for response, I think that lazy pooling make code little bit clean, but maybe it's issue for another library.. If node-mysql lib doesn't need this approach please close this pull request. |
We are still considering it. My main point was that |
in some cases app.get("/", getMysqlConnection, function( req, res ){
if( !req.body.email || !req.body.login || !/\d+/.test(req.body.num) ){
// mysql connection not need here, but it's already instantiated
return res.send("something wrong...");
}
req.mysql.query("...");
}); or when use cache system: app.get("/", getMysqlConnection, function( req, res ){
var cache = Cache.get("...");
if(cache){
// cache used, but mysql connection instantiated
return res.send(cache);
}
req.mysql.query("...");
}); Yes, this issue can be fixed by adding more middleware functions. Another design improvement in my opinion:
I think it's looks good, because uses the same ways to work with connections.
|
17209da
to
76de66e
Compare
Hello, sorry for my English, it's my first pool request.
In some projects there are required to use One mysql connection per HTTP request, but in some cases such requests don't need a mysql connection, for example if response is cached or auth failed, etc.
I write class that proxy calls to mysql connection, and get it from pool only if user really need it.
It works like a Connection object( but for pooled connections ) when it lazily connects on user request.
Simple example:
More complex example for express:
This approach to get connections is similar to JAVA class LazyConnectionDataSourceProxy