Is it a good practice to close my connection after Sequelize operations?

Viewed 5890

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

2 Answers

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.

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..

Related