How to handle Multiple Connections dynamically?
See original GitHub issueHey, 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:
- Created 6 years ago
- Reactions:1
- Comments:11
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Try this:
@betweenbrain Calling
pool.close()
inexecSql
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).