Pergunta

With the node-mysql module, there are two connection options - a single connection and a connection pool. What is the best way to set up a connection to a MySQL database, using a single global connection for all requests, or creating a pool of connections and taking one from the pool for each request? Or is there a better way to do this? Will I run in to problems using just a single shared connection for all requests?

Foi útil?

Solução

Maintaining a single connection for the whole app might be a little bit tricky.

Normally, You want to open a connection to your instance, and wait for it to be established.

From this point you can start using the database (maybe start a HTTP(S) server, process the requests and query the database as needed.)

The problem is when the connection gets destroyed (ex. due to a network error). Since you're using one connection for the whole application, you must reconnect to MySQL and somehow queue all queries while the connection is being established. It's relatively hard to implement such functionality properly.

node-mysql has a built-in pooler. A pooler, creates a few connections and keeps them in a pool. Whenever you want to close a connection obtained from the pool, the pooler returns it to the pool instead of actually closing it. Connections on the pool can be reused on next open calls.

IMO using a connection pool, is obviously simpler and shouldn't affect the performance much.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top