tediousjs / node-mssql

Microsoft SQL Server client for Node.js
https://tediousjs.github.io/node-mssql
MIT License
2.24k stars 471 forks source link

Would there be memory leaks if we create a new new sql.ConnectionPool with each query and then call .close() to close all connections in the pool after the query? #1686

Closed maxchen33 closed 4 months ago

maxchen33 commented 4 months ago

Hi guys, as mentioned in description, we have a usecase which requires us to connect to a different database, therefore we are creating a new sql.ConnectionPool() with different config settings on each query request in the express route.

After the request is finished, we will execute .close() to all all active connections. I have a few questions:

  1. Will the memory for each of the new ConnectionPools be freed eventually by the garbage collector, since we are calling .close() to close all active connections in each of the new Connection Pools. After the query, the new Connection Pool is no longer needed or referenced.

  2. Will there be any strong references to the config objects used in the new Connection Pool or other objects whose memory cannot be fully released.

  3. Will this approach causes any memory leaks? Thank you

Appreciate your kind responses and help. Thank you!

dhensby commented 4 months ago

I would not recommend that approach regardless of memory leaks or not.

Creating a connection pool is slow and not intended to be done in the way your describe.

see relevant docs: https://github.com/tediousjs/node-mssql?tab=readme-ov-file#advanced-pool-management