javascript - How do I switch connections using Node JS and mssql -
i have situation need query multiple sql servers bunch of data. so, iterating on sql servers first , iterating on queries have run. in caller function, have below code
config.servers.foreach((element) => { sqlserver = element.server; element.queries.foreach(function(configquery) { jsonkey = configquery.key; sqlquery = configquery.query sqldatabase = configquery.database; var out; if (configquery.parameter) { sqlquery = sqlquery + " " + configquery.parameter + " = '" + partnumber + "'"; } sqlhelper.sqlfun(sqlquery, sqlserver, sqldatabase, jsonkey, callback) })
and sqlfun referes below function
let connection ; function poolit (sqlquery,sqlserver,databasename) { if(!connection) { //sqlserver_temp = sqlserver //instead of checking if connection defined //we have check if connection open var dbconfiguration = { driver: 'msnodesqlv8', server: sqlserver, database: databasename, options: { trustedconnection: true } } connection = sql.connect(dbconfiguration); } } function runquery(sqlquery,sqlserver,databasename,sqlkey, callback) { poolit(sqlquery,sqlserver,databasename) connection.then(pool =>{ return pool.request().query(sqlquery) }).then(result=>{ if(callback) return callback(null,result.recordset, sqlkey ) }).catch(err=>{ if(callback) { console.log(err) return callback(err,null, sqlkey) } }) } module.exports.sqlfun = runquery;
the problem facing is, sql server connections trying establish, of connecting same sql server. understand has due function poolit checking there if connection exists or not , if exists, coming out of loop. i'd understand how close connection once done executing queries in iteration first sql server? tried sql.close() , connection.close() not working. program hangs long time if use them.
Comments
Post a Comment