I'm using the node-mysql
driver with connection pooling.
Releasing the connection back into the pool when there's only one query, is easy:
pool.getConnection(function(err, connection) {
if (err) {
throw err;
}
query = "SELECT * FROM user WHERE id = ?";
connection.query(query, [id], function(err, users) {
connection.release();
if (err) {
throw err;
}
// ...
});
});
What if I need to use the connection a second time? I'd have to move the release()
down a few lines. But what happens if the error is thrown? Is the connection never returned to the pool?
Do I have to use some control flow lib to have a "finally" moment in which I could release it?
Any better ideas?