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.

How to handle Multiple Connections dynamically?

See original GitHub issue

Hey, I was looking at the link that describes how to handle multiple connections. In the example, there are 2 hard coded connections. I’m building an app that has many users and I’m trying to figure out how to allow them to not get errors if they query the database at the exact same time or before another query is finished.

The following is what I’m using in a nodejs app running on a Linux machine. It works as long as nobody makes a request before another request is finished. Otherwise with the following code I still get Error: Global connection already exists. Call sql.close() first.

const settings  = require('./settings');
const sql = require('mssql');

exports.execSql = async function(sqlquery) {
  try {
    let pool = await new sql.connect(settings.config);
    let result = await pool.request().query(sqlquery);
    return {success: result};
  } catch (err) { 
    return {err: err};
  } finally {
    sql.close(); //closing connection after request is finished.
  }
};

sql.on('error', err => {
  // ... error handler 
  console.log('sql errors', err);
});

The hardcoded example from the docs assumes there are only 2 connections. How do I modify the above to handle a dynamic number of connections?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:11

github_iconTop GitHub Comments

47reactions
patriksimekcommented, May 3, 2017

Try this:

const settings  = require('./settings');
const sql = require('mssql');

exports.execSql = async function(sqlquery) {
  const pool = new sql.ConnectionPool(settings.config);
  pool.on('error', err => {
    // ... error handler 
    console.log('sql errors', err);
  });

  try {
    await pool.connect();
    let result = await pool.request().query(sqlquery);
    return {success: result};
  } catch (err) { 
    return {err: err};
  } finally {
    pool.close(); //closing connection after request is finished.
  }
};
3reactions
patriksimekcommented, Jul 10, 2017

@betweenbrain Calling pool.close() in execSql is not a good idea - once you close a pool, you can no longer acquire a connection from that pool. That means that all subsequent requests will fail.

The good idea is to export a close method and close the pool when you’re absolutely sure there will be no more requests to make (e.g. when application closes).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Managing multiple dynamic database connections
The standard operating procedure is to open the connection, use it, and "close" it (return it to the pool) as quickly as possible....
Read more >
Handle multiple database connection dynamically in ASP.NET
I am doing a research of how to manage the multiple location database server connections dynamically in a ASP.net application.
Read more >
Set sql server connection Dynamically
In the power automate flow, I need to establish multiple connection with the sql database Dynamically(on the Run time).
Read more >
Multiple, Dynamic Database Connections - Slim Framework
I have a microservices app using PHP/DI and CakePHP datasource connection which gives me Cake\Database\Connection and Cake\Database\Query Connection::class ...
Read more >
Creating and Using Dynamic Connections - Logi Analytics
A dynamic connection is a group of connection properties that can be used to override the original data source connections in a catalog....
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