2

I'm using nodejs and SequelizeJS to my sql database.

After I verify the connection,

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });
  1. Does Sequelize open a new connection every time it executes a SQL-command or it keeps an open connection and reuses it?

  2. How can I verify that?

Tsvetan Ganev
  • 8,246
  • 4
  • 26
  • 43
raxinaga
  • 411
  • 1
  • 6
  • 18
  • Yes it does to me, because I want to understand his behavior. How can you approve your answer? based on what I see, using sp_who I can't see the login-user from the nodejs is exist... – raxinaga Jun 23 '18 at 12:51
  • i didn't answer, i wrote a comment.. yeah .. the logged in user in web applications is stored usually in sessions some identifier of somekind .. the authenticate tests the if it can establish an connection using the configuration provided to it.. there is the sequelize repository if you want to look into internals .. i strongly suggest to read basic tutorials how to use with express for example. – Gntem Jun 23 '18 at 13:33

1 Answers1

2

Does Sequelize open a new connection every time it executes a SQL-command or it keeps an open connection and reuses it?

Sequqlize will try to reuse the existing connection it has in its pool. Default config looks like this. You can look at the source code here

const defaultPoolingConfig = {
  max: 5,
  min: 0,
  idle: 10000,
  acquire: 10000,
  evict: 10000,
  handleDisconnects: true
};

If the app needs more connections and the current number of connections is less than max, sequelize will attempt to create a new connection.

How can I verify that?

You can add logging at pool.create method in sequelize to see if a new connection is made. It should also be possible to it at the db log but I am not sure.

AbhinavD
  • 6,892
  • 5
  • 30
  • 40