Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

node.js - Tedious losing connection when running querys in a loop

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)
ode_modulesediousliberrors.js:13:12)
    at Connection.socketError (C:Users(myPath)
ode_modulesediouslibconnection.js:1294:54)
    at Socket.<anonymous> (C:Users(myPath)
ode_modulesediouslibconnection.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)
ode_modulesediousliberrors.js:13:12)
    at Connection.socketError (C:Users(myPath)
ode_modulesediouslibconnection.js:1294:54)
    at Socket.<anonymous> (C:Users(myPath)
ode_modulesediouslibconnection.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)
ode_modulesediousliberrors.js:13:12)
    at Connection.socketError (C:Users(myPath)
ode_modulesediouslibconnection.js:1294:54)
    at Socket.<anonymous> (C:Users(myPath)
ode_modulesediouslibconnection.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

question from:https://stackoverflow.com/questions/65896384/tedious-losing-connection-when-running-querys-in-a-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...