Tedious losing connection when running querys in a loop

Viewed 281

The problem is when running queries in a loop the Tedious library executes only the last call, while other calls returning this error

ConnectionError: Connection lost - read ECONNRESET
    at ConnectionError (C:\Users\(myPath)\node_modules\tedious\lib\errors.js:13:12)
    at Connection.socketError (C:\Users\(myPath)\node_modules\tedious\lib\connection.js:1294:54)
    at Socket.<anonymous> (C:\Users\(myPath)\node_modules\tedious\lib\connection.js:1125:14)
    at Socket.emit (events.js:327:22)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'ESOCKET'
}

what I am trying to achieve is doing SQL insert queries in a for loop and returning the results of the queries in an array

I am using mssql lib to connect to a sql-server2019 instance running on localhost

I have tried updating all the packages
I have tried updating nodejs itself

The function I am trying to run

const sql = require('mssql');

async function insertline(){
    let config = {
        user: configFile.database.username,
        password: configFile.database.password,
        server: configFile.database.host,
        database: configFile.database.database,
        port: configFile.database.port
    };
    sql.connect(config)
   .then((conn) => 
      conn.query(`
     INSERT INTO stuff 
     VALUES (values)
`)
         .then((v) => console.log(v))
         .then(() => conn.close()).catch((err) => console.log(err))
   )
    }
//either run from for loop or like this still return the same thing
insertline()
insertline()
insertline()

When run

ConnectionError: Connection lost - read ECONNRESET
    at ConnectionError (C:\Users\(myPath)\node_modules\tedious\lib\errors.js:13:12)
    at Connection.socketError (C:\Users\(myPath)\node_modules\tedious\lib\connection.js:1294:54)
    at Socket.<anonymous> (C:\Users\(myPath)\node_modules\tedious\lib\connection.js:1125:14)
    at Socket.emit (events.js:327:22)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'ESOCKET'
}
ConnectionError: Connection lost - read ECONNRESET
    at ConnectionError (C:\Users\(myPath)\node_modules\tedious\lib\errors.js:13:12)
    at Connection.socketError (C:\Users\(myPath)\node_modules\tedious\lib\connection.js:1294:54)
    at Socket.<anonymous> (C:\Users\(myPath)\node_modules\tedious\lib\connection.js:1125:14)
    at Socket.emit (events.js:327:22)
    at emitErrorNT (internal/streams/destroy.js:106:8)
    at emitErrorCloseNT (internal/streams/destroy.js:74:3)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'ESOCKET'
}
{
  recordsets: [],
  recordset: undefined,
  output: {},
  rowsAffected: [ 1 ]
}

as you can see from the errors, only the last call of the function is executed successfully and when I go check sql management studio the row is inserted as it should

It works normally when running it once

versions

nodejs: v14.15.4
npm : 6.14.10
mssql: "^6.3.1"
sql-server 15.0.2000.5(Microsoft SQL Server Developer (64-bit))

any ideas why this might be happening ? any help would be appreciated :)

EDIT: I haven't yet Found out why This Happens but I have Solved my problem by putting all the SQL queries in a string separated by a ; instead of running the query multiple times

0 Answers
Related