I am developing an express based application that makes some queries different (different by user!) SQL Server 2008 and 2014 databases. This is different because each user belongs to a different company, and each company has its own SQL Server. My application uses its own SQL Server to manage companies and their SQL Server connection string (my application has access to its database servers). I am using the mssql module.
I did not find best practice regarding "use one SQL Server connection for each user session or one connection for each user query."
From the .NET world, we had a rule: "one request / function - one connection" .
First, the application must query its own application database to get the SQL Server connection string for the user company database. The user can then retrieve some data from his SQL Server company (in my application) - for example, getAccounts() . Each of these functions (each function is not every request in this function!) Opens a new connection and closes it after the request is completed:
let connection = new mssql.Connection(conStr, (err) => { request.query(queryString, (err, result) => { if (err) throw new Error('...'); resolve(result) connection.close(); }); })
As I understand it, it should not have any (negative) difference if 100 users open and close connections for each request (provided that there is one request per user at a time) or 100 users have 100 open connections (one per user) for everything session. At first glance, it seems that my approach is less resource intensive, since connections are opened only when they are needed (i.e., a few seconds per request).
Am I missing something? What if 200 users access my application at the same time - will I somehow get in trouble?
Thanks in advance!
[EDIT]
As far as I understand,
let connection = new mssql.Connection(...)
will create a new connection pool that will open a new connection when I use something like
connection.connect()
and close all active connections with
connection.close()
So, I assume that the best practice in my scenario would be to create one connection pool ( new mssql.Connection(..) ) for the active user, save it in some session repository, and then reuse it throughout the session .
Is this a good approach?
I just want to avoid one thing: the user receives an error message because the connection could not be created.