mikro-orm icon indicating copy to clipboard operation
mikro-orm copied to clipboard

Connection is opened when closing the ORM even if a connection was never established

Open Nickman87 opened this issue 1 month ago • 0 comments

Describe the bug

It is possible in the MikroORM config to not connect on initialisation using connect: false as a property. This works as intended, but when you close the orm, it will still open a connection, just to verify that it can make a connection (using a select 1 query) just to close it again.

I would expect nothing to happen if no connection has ever opened, because now it is impossible to initialise MikroORM "in case you need it" and avoid unnessary connections to your DB being opened.

This issue arrose using the NestJS plugin, where the close method is called when you enableShutdownHooks and can be reproduced by calling the close method on the ORM. For both SQLite and Postgres. (this seems to be a standard feature in Knex that will try to ensure a connection exists when you check if the connection you have is open)

This is an issue in our case since we are running in a serverless environment, and want to avoid unnecessary DB connections at all costs.

Reproduction

https://github.com/Nickman87/mikro-orm-reproductions/tree/issue/connection-opened-on-close

What driver are you using?

@mikro-orm/postgresql

MikroORM version

6.6.2

Node.js version

v22.13.0

Operating system

OSX

Validations

Nickman87 avatar Dec 09 '25 20:12 Nickman87