1

If Node is single-threaded, what is the advantage of using a pool to connect with MySQL?
If it is, when should I release a connection?

Sharing the same, persistent, connection with the whole application isn't enough?

1 Answer 1

2

Nodejs is single threaded, right. But it is also async, meaning that the single thread fires multiple sql queries without waiting for the result. The result is only processed via callbacks. Therefore it makes sense to use a connection pool with more than one connection. The database is likely multi-threaded, which makes it possible to parallelize the queries, although they were fired consecutively. There is no guarantee however in which order the results are processed if you don't take extra care for that.

Addendum about connection release

If you use a connection pool, than you should aquire/release each connection from the pool for each query. There is no big overhead here, since the pool manages the underlying connections.

  1. Get connection from pool
  2. Query
  3. In the callback release connection back to the pool.
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for putting in a simple way... The only question that remains in this case is when should I release a connection.
if you use a connection pool, than you should aquire/release each connection from the pool for each query. There is no big overhead here, since the pool manages the underlying connections. So get connection from pool -> query -> in the callback release connection back to the pool.
Oh... Is it dead simple like that?! Thank you again... (please, put this in the answer for the future ;D )
What about when you have multiple instances of your Nodejs app running on the same machine. Can these separate processes effectively share the same pool or is the pool managed within the calling process?
AFAIK they will not share the same pool.

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.