tutiplain
tutiplain

Reputation: 1480

Is it a good practice to leave a database connection open on a node.js application?

I have seen examples from different Node.js frameworks in which a database connection is opened at the beginning of the app, and that same connection is reused all over the application. I have seen, for example, Mongoose being used this way, and it seems to be the case for sequelize.js.

Is this a good practice? Can a constantly open connection by hijacked? If not, what would be the correct way to do it?

Upvotes: 1

Views: 670

Answers (1)

Gillis Haasnoot
Gillis Haasnoot

Reputation: 2278

Yes it is good. You can still use pooling if you need to. But generally having a single connection is better than opening a new connection everytime. Make sure though that you check 'error' events on the DB instance. So you can reconnect if the connection to DB is dropped. You can always have a hickup in your mysql server. For example when diskspace runs low or memory full issues.

Upvotes: 1

Related Questions