This is actually pretty easy--if you don't need connection in the outer
scope (ie you don't ever plan on reusing it), you can define it locally:
function poolIt(q, url, dbName) {
const conn = sql.connect(mssqlConfig(url, dbName));
Or, if you need to reuse it, you can store it on an object, maybe something
like:
const conn = {};
function poolIt(q, url, dbName) {
const k = hashSomehow(url, dbName);
if (!conns[k]) {
conns[k] = sql.connect(mssqlConfig(url, dbName));
}
Keep in mind you'll need some kind of cleanup for the conn:
conns[k].close(); // This will close the connection
conns[k] = null; // This will enable GC
I'd consider changing the above to just use a connection name parameter
rather than generating a hash, though.
--Josh
On Wed, Sep 13, 2017 at 6:52 PM, Karun Reddy <[email protected]>
wrote:
> In my application, I am querying multiple SQL Servers to return some data.
> In my main function, I am iterating through the SQL Servers and on each of
> them, I am iterating through the queries that are supposed to run to fetch
> the data. When querying the SQL Server for data, I am using a connection
> promise returned by the following function.
>
>
> function poolIt (sqlQuery,sqlServer,databaseName) {
> if(!connection)
> {
> //sqlServer_temp = sqlServer
> //instead of checking if connection is defined
> //we have to check if connection is open
> var dbConfiguration = {
> driver: 'msnodesqlv8',
> server: sqlServer,
> database: databaseName,
> options: {
> trustedConnection: true
> }
> }
> connection = sql.connect(dbConfiguration);
> }
> }
>
> Now when the loop is done for one SQL Server, it should connect to another
> SQL Server. But when the debug mode is on, I see that all the queries are
> running against only one server. I understand that this is because of the
> above function as it is checking if a connection object is available or not
> before creating a new connection. So, my question is on how to kill the
> connection object? connection.close() is not working and nor does
> sql.close()
>
> Any help, please ?
>
> --
> Job board: http://jobs.nodejs.org/
> New group rules: https://gist.github.com/othiym23/9886289#file-
> moderation-policy-md
> Old group rules: https://github.com/joyent/node/wiki/Mailing-List-
> Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/nodejs/44521f9c-993a-4e4f-a533-efaba1d7054c%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/44521f9c-993a-4e4f-a533-efaba1d7054c%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/CAHL2Ymo%2Bo%3DbQ5MHdZgg_kzhUkRUPMXK%2BsD4kM-aop2xzAjWxLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.