4

currently I'm designing an app on top of Sequelize using NodeJS/TypeScript, and I'm wondering if it can cause performance issues not closing a connection.

For instance, in a micro service, I need data from 1 entity.

const resolver = async (,,{db}) => {
  const entity1 = await db.models.Entity1.findOne()
  return entity1
}

Is it required to close the connection after having called findOne?

My understanding is that the following config defines a number of concurrent connections and idle is a parameter making the connection manager closing the connection of idle ones:

module.exports = {
  development: {
    host: 'db.sqlite',
    dialect: 'sqlite',
    pool: {
        max:5,
        min:0,
        idle:10000
    }
  },
  test: {
    host: 'test.sqlite',
    dialect: 'sqlite',
    pool: {
        max:5,
        min:0,
        idle:10000
    }
  }
}

Any advice is welcome

dbrrt
  • 2,275
  • 4
  • 18
  • 35

2 Answers2

7

Sequelize maintains an internal database connection pool, that's what the pool parameters are for, so this isn't necessary. Each call actually borrows a connection temporarily and then returns it to the pool when done.

Closing that connection manually may poison the pool and cause performance issues.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    is the same applicable for lambda function as well ? https://stackoverflow.com/questions/74264495/nestjs-aws-lambda-sequelize-connection-pool – Ujjual Oct 31 '22 at 15:33
1

If you don't close the Sequelize connection, the micro-service will still run until the connection got timed out (idle time pool parameter).. I suggest to close Sequelize connection, at least in micro-services..

vilaboa
  • 21
  • 3
  • 1
    and that will incur more costs (eg: lambda executing time).. and may cause a timeout of lambda service.. – vilaboa Nov 25 '21 at 20:29
  • but I'm wondering if the connection isn't automatically closed by sequelize after the operation is done, I didn't notice timeouts for that specific use case – dbrrt Dec 04 '21 at 11:06