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.

Error: Global connection already exists. Call sql.close() first.

See original GitHub issue

I’m wrestling with an aws lambda and mssql.

It seems like there’s some kind of error event that exits but doesn’t close the connection, so the next request is met with:

Error: Global connection already exists. Call sql.close() first. From here

It seems important to be able to check for the existence of globalConnection, but there doesn’t seem to be a getter, correct? Something like the following would help:

// Turn off this error and just return the existing globalConnection
sql.connect({ reuseConnection: true })
sql.connect()

// Just a getter for globalConnection
sql.getConnection()

if (sql.connectionStatus() === CONNECTED) { ... }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:9

github_iconTop GitHub Comments

1reaction
WeihanLicommented, Oct 22, 2018

I got connectionStatus is not a function error, I use the code below instead:

let connectionPool;

var getConnection = async function(){
    if(!(connectionPool && connectionPool.connected)) {
        connectionPool = await mssql.connect(connConfig);
    }
    return connectionPool;
}
0reactions
dhensbycommented, Mar 11, 2019

I’m really in two minds about this.

  1. If AWS Lambda process is experiencing an error but not cleaning up the connection, then that sounds like bad AWS behaviour
  2. You really should avoid using the global connection IMO because it’s bad practice, you can maintain your own connection instead which will provide much better control
  3. I really would rather not add a getConnection function for accessing the global connection for fear of making it appear acceptable to use.

However, I can see how it would be useful, maybe connect should just return the existing connection if it exists instead of erroring?

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js Global connection already exists. Call sql.close() first
You have to create a poolConnection try this: new sql.ConnectionPool(config).connect().then(pool => { return pool.request().query("SELECT ...
Read more >
Error: Global connection already exists. Call sql.close() first
close() first. node.js. Hi I am creating node js restful api by using sqlserver database , i prepare get api ...
Read more >
MSSQL not close connection of database - CodeProject
My problem is that in some cases this error occurs: Global connection already exists. Call sql.close() first. How can I resolve this error?...
Read more >
[Solved]-Global connection already exists issue node.js-node.js
I was facing the same issue and solved it with below implementation, For handling global connection already exists. call sql.close() first we need...
Read more >
patriksimek/node-mssql - Gitter
“Connection is closed” errors are only received when the connection is not yet established or already closed (explicitly by calling close ).
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