tediousjs / tedious

Node TDS module for connecting to SQL Server databases.

Home Page:http://tediousjs.github.io/tedious/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Socket Hang-up in tedious

chaitanya1094 opened this issue · comments

Hi Team,
We are using tedious "6.6.5" and Node "10.15.1" to connect to Azure SQL. Frequently we are facing below error and app crashes,

/opt/app/node_modules/tedious/lib/errors.js:13
return new ConnectionError(message, code);
       ^
ConnectionError: Connection lost - socket hang up
at connectionError (/opt/app/node_modules/tedious/lib/errors.js:13:12)
at connection.socketError (/opt/app/node_modules/tedious/lib/connection.js:1289:54)
at Connection.socketEnd (/opt/app/node_modules/tedious/lib/connection.js:1307:12)
at Socket.socket.on (/opt/app/node_modules/tedious/lib/connection.js:1126:14)
at socket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process. tickcallback (internal/process/next_tick.js:63:19)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app@1.4.18 start: "node index`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the app@1.4.18 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!

Below is the code we are using to connect to DB.

const {  Connection,  Request} = require('tedious');
var config = {
    server: '<server>',
    authentication: {
      type: 'azure-active-directory-password',
      options: {
        userName: 'test',
        password: 'test'
      }
    },
    options: {
      encrypt: true,
      conenctionTimeout:30000
    }
  };
const queryDB= (query,  done) => {

    let result = "";
    let ERROR_HANDLER = null;
    const connection = new Connection(config);

    connection.on("connect", err => {

        if (err) {
            ERROR_HANDLER = err
            ERROR_HANDLER.status = 500;
            done(ERROR_HANDLER);
            return;
        }
        const request = new Request(query, (err, rowCount) => {
            if (err) {
                ERROR_HANDLER = err
                ERROR_HANDLER.status = 500;
            } else {
                console.log(`${rowCount} rows returned`);
            }
        })

        request.on("row", columns => {
            result += columns[0].value;
        });

        request.on('requestCompleted', () => {

            connection.close();
            if (ERROR_HANDLER) {

                done(ERROR_HANDLER);

            } else {
                done(null, result);
            }
        })

        request.on('error', (err) => {

            connection.close();
            ERROR_HANDLER = err;

            ERROR_HANDLER.status = 500;
            done(err);
        })
        connection.execSql(request);
    })
}

module.exports={queryDB}

I don't see anything specific about your use case that would cause these hangups. Have you tried upgrading to more recent versions of tedious and checked if the same error still happens there as well? 🤔

Thanks for the response, we are in production currently.. so little hesitant to upgrade...this error is occurring if a huge load on the app is present. Is this error because we are creating a connection for each DB call? If we want to upgrade what version you suggest?

Thanks for the response, we are in production currently.. so little hesitant to upgrade...

I'd suggest doing the updates in small steps, so you can resolve any deprecation warnings. That might help you to be more confident about the upgrade.

this error is occurring if a huge load on the app is present. Is this error because we are creating a connection for each DB call?

That's hard to say, really. 😞 Maybe you're hitting some sort of timeout when a lot of connections / requests are being processed? The error is very generic and basically just means that the connection was closed by the server, but it's not clear why. You could try to see if you can also access the server's logs and if this helps you to pinpoint where the issue is coming from.

I'm seeing this same issue with Azure SQL. Several times per day this error occurs resetting the app. 80% of the time everything works as expected, then these errors pop up causing the app to restart.