tediousjs / node-mssql

Microsoft SQL Server client for Node.js
https://tediousjs.github.io/node-mssql
MIT License
2.23k stars 467 forks source link

ConnectionError: Connection lost - read ECONNRESET #1467

Closed kin-for-test closed 1 year ago

kin-for-test commented 1 year ago

when using connectionpool to connect the db , it will randomly exit program

Expected behaviour:

expected using connection pool connect to the db

Actual behaviour:

start from connect() it will throw err but not close the program , after some req come in , program will randomly exit program.

ConnectionError: Connection lost - read ECONNRESET\n    
at C:\\------------------\\node_modules\\mssql\\lib\\tedious\\connection-pool.js:70:17\n    
at Connection.onError (C:\\------------------\\node_modules\\tedious\\lib\\connection.js:1017:9)\n    
at Object.onceWrapper (node:events:627:26)\n    at Connection.emit (node:events:524:35)\n    
at Connection.emit (C:\\------------------\\node_modules\\tedious\\lib\\connection.js:1040:18)\n    
at Connection.socketError (C:\\------------------\\node_modules\\tedious\\lib\\connection.js:1399:12)\n    
at Socket.<anonymous> (C:\\------------------\\node_modules\\tedious\\lib\\connection.js:1149:16)\n    
at Socket.emit (node:events:524:35)\n    at emitErrorNT (node:internal/streams/destroy:151:8)\n    
at emitErrorCloseNT (node:internal/streams/destroy:116:3)\n    
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {code: 'ESOCKET', originalError: Error: Co…ktop\\Bus…, …}

1 2 1

Configuration:

index.js

import * as sql from 'mssql';
import * as Koa from "koa";
import {Log,bindDB} from "./DB";

var dbConfig=
{
    user:'user',
    password:'pw',
    server:'localhost',
    port:'1434',
    database:'test',
    options:
    {
        trustServerCertificate: true,
        trustedConnection:true,
        useUTC:false,
    }
 };
const app = new Koa();
 let appPool = new sql.ConnectionPool(dbConfig);
 appPool.connect().then(function(pool) {
    bindDB(pool);
    app.listen(80);
    now Log("server start").insert();
  }).catch(e=> {
    console.log("Error creating connection pool :"+e);
  });

DB.js

 let dbpool;

export function bindDB(p)
{
    dbpool=p;
}

class Log
{
    punblic msg;
    constructor(msg)
    {
        this.msg=msg;
    }
    public async insert()
    {
        dbpool.request().query(`insert into log values ('${this.msg}')`);
    }

}

Software versions

dhensby commented 1 year ago

I believe it is expected behaviour in node 19 for uncaught promise rejections to exit node.

You need to make sure you're catching errors.

As I don't know about your environment, I can't give help on why the connections are being reset.

dhensby commented 1 year ago

closing due to lack of response

Harshpanday commented 1 year ago

Were you able to solve this issue @kin-for-test @dhensby ? I have tried multiple fixes but none of them seem to work.

I have opened a new issue as well https://github.com/tediousjs/tedious/issues/1545

Software Versions: Node : v18.16.0 mssql : v9.1.1 Sequelize : v6.31.1 Tedious: v16.1.0