question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connections all messed up?

See original GitHub issue

The 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:

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

  2. 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:closed
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
dhensbycommented, Sep 23, 2020

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.

In this example, does the routeHandler connect use the existing pool? Or will calling sql.connect(config) actually create a new pool every time I need a connection?

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

0reactions
cinnamonKalecommented, Sep 23, 2020

tl;dr only call sql.connect() once in your application.

Does this still apply, considering the docs say multiple sql.connect() are fine?

In my case, the server starts with the initial PoolConnection:

new sql.ConnectionPool(config).connect().then(pool => /** Start app */);

And routes in other files want to use that initial connection, or create a new PoolConnection if the database server dropped for some reason:

const routeHandler = () => { sql.connect(config).then(pool => /** Query */).catch(/** Error handler */); };

In this example, does the routeHandler connect use the existing pool? Or will calling sql.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 runs sql.connect()

Read more comments on GitHub >

github_iconTop Results From Across the Web

6 Common Wire Connection Problems and Their Solutions
Electrical connection problems may be prevalent around your home. Here are some of the most common ones and how to fix them.
Read more >
How To Fix An Intermittent Internet Connection In Windows 10
An intermittent internet connection is often a result of several issues: ... Contact your ISP to see if the problem is on their...
Read more >
13 Tips to Troubleshoot Your Internet Connection | PCMag
1. Try Another Device or Website ... Start with the obvious: is the problem only happening on one device or all your devices?...
Read more >
Fix network connection issues in Windows - Microsoft Support
Try these things to troubleshoot network connection issues in Windows 11. Make sure Wi-Fi is on. Select Start > Settings > Network &...
Read more >
How to fix the four biggest problems with failed VPN connections
Is your VPN connected but not working? Learn four of the biggest trouble areas with VPN connections and how you can fix them...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found