Connections all messed up?
See original GitHub issueThe way connections are handled doesn’t seem to be well defined. Examples seem to open connections and not close them. Copying example code to a simple js file will leave node hanging because the connection doesn’t close.
Also a ‘ConnectionPool’ doesn’t seem to work like expected either. If you create a ConnectionPool, the name seems to imply that there is a pool of connections that will allow you to execute multiple commands at once. In testing, this does not seem to be the case.
From what I can tell, there is only ever a single global connection to sql server. The error I get lends weight to that case, no matter how I am connecting or how many 'ConnectionPool’s I use:
Global connection already exists. Call sql.close() first
Expected behaviour:
-
Documentation and samples need to be improved and to represent best practices for using the package. That includes closing the connection, and especially some kind of warning that there is only one global connection.
-
There should be multiple connections allowed. Having the library be async doesn’t make a lot of sense if you have to write your own code to make sure you never attempt to run two queries or create two connections at the same time or you will get an error.
Actual behaviour:
Creating a new connection, connection pool, request, or anything seems to cause an error:
Global connection already exists. Call sql.close() first.
Configuration:
// paste relevant config here
Software versions
- NodeJS: 8.11.1
- node-mssql: 5.1.0
- SQL Server: 16
Issue Analytics
- State:
- Created 4 years ago
- Comments:10
Yes, there is now a feature that allows multiple calls to
sql.connect()
to be called. It is a bit naive in that if you pass different configs in subsequent calls to connect it will still return the original connection, but it’s really intended to be a low effort/low feature helper for basic usage of the library. If you need to run connections to multiple databases / run multiple pools, then you should be managing this as part of your application state.It will not create a new pool every time any more (it never used to, I don’t think, it would have thrown an error).
Does this still apply, considering the docs say multiple
sql.connect()
are fine?In my case, the server starts with the initial PoolConnection:
And routes in other files want to use that initial connection, or create a new PoolConnection if the database server dropped for some reason:
In this example, does the
routeHandler
connect use the existing pool? Or will callingsql.connect(config)
actually create a new pool every time I need a connection?If it’s the latter, we would still need to manage a
global.pool
across the app, which would seem to contradict the docs.Note, I’m only calling
sql.close()
once, at the end of the application. So each usage of the pool just runssql.connect()