7

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?

2
  • 2
    Hmm. Really good question! Got me thinking. Handle the release in each error callback? Commented Feb 12, 2014 at 23:37
  • @Zlatko that is what i thought, since that connection never returned to the pool when error strike Commented Oct 26, 2017 at 3:26

3 Answers 3

3

One way this could be handled is promises. Since you're building a pool, you can construct your requests with something like q (or native promises, coming soon):

// assuming you have your local getConnection above or imported
exports.getConnection = function(queryParams) {
    var d = q.defer();
    local.getConnection(function(err, conn) {
        if(err) d.reject(err);
        d.resolve(conn);
    });
});

So, wrap few of your other calls into promises like that and then just compose your query:

db.getConnection()
.then(function(conn){
    makeRequest()
    .then(...)
    ...
.catch(function(err){
    // at the end you release the conn
});

Does it seem like something you're asking for?

Sign up to request clarification or add additional context in comments.

3 Comments

that is what I ended up doing - but with promises and generators. thank you for pointing me in the right direction.
Wait, wouldn't this only release the connection if an error was thrown since you are releasing in the catch block? Wouldn't you want to do .finally(function(){ conn.release(); } ??
Oh, you can do that, I thought you can do would release the connection in the makeRequest, but finally would be way better.
1

When you perform a MySQL query, for that time it locks the database until query completes. After the successfull completion of query, it releases that database lock.

Same case here: connection.release(); simply releases the DB connection, nothing else.

Comments

0

You should use separate connections for that situation. That's what the connection pool is for. That way one doesn't have to wait for the other one to finish before it can start. I only use the same connection if one query cannot start until the other query has finished.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.