dbrrt
dbrrt

Reputation: 2542

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

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

Upvotes: 4

Views: 14025

Answers (2)

vilaboa
vilaboa

Reputation: 21

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

Upvotes: 1

tadman
tadman

Reputation: 211560

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.

Upvotes: 8

Related Questions