Reputation: 1480
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
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