Connection is opened when closing the ORM even if a connection was never established
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
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord.
- [x] The provided reproduction is a minimal reproducible example of the bug.