Skip to content

ESOCKET Connection Error from node js web app to MSSql Database connection #799

Closed

Description

@Praveen335

Hi Team,
Currently, we are getting the intermittent connectivity issues from our web app to MsSql frequently when there is more traffic to the service.
Is there any final recommendations on this issue.

Error:
{ ConnectionError: Failed to connect to <>:1433 - Could not connect (sequence)
at Connection.tedious.once.err (D:\home\site\wwwroot\node_modules\mssql\lib\tedious.js:216:17)
at Connection.g (events.js:291:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.socketError (D:\home\site\wwwroot\node_modules\tedious\lib\connection.js:699:14)
at D:\home\site\wwwroot\node_modules\tedious\lib\connection.js:590:25
at SequentialConnectionStrategy.connect (D:\home\site\wwwroot\node_modules\tedious\lib\connector.js:153:9)
at Socket.onError (D:\home\site\wwwroot\node_modules\tedious\lib\connector.js:169:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
code: 'ESOCKET',
originalError:

Version Details:
"tedious": "2.0.1",
"mssql": "^4.0.4",

Thanks,
Praveen.

Activity

  1. arthurschreiber commented on Sep 25, 2018

    @arthurschreiber
    Collaborator

    Hey @Praveen335 👋🏻

    I‘m sorry to hear that you are facing this problem when using tedious. Unfortunately, the information you provided is not enough for us to understand what is happening. Could you provide more information? E.g. it would be helpful for us to know:

    • which version of tedious are you using exactly?
    • when does this error happen exactly? From the stacktrace it looks like this happens when connecting to the database, but can other processes still connect at the same time? I’m thinking this might be a SQLServer issue (maybe due to the high load you mentioned) and not an issue with tedious itself.

    Thanks! 🙇🏻‍♂️

  2. Praveen335 commented on Sep 26, 2018

    @Praveen335
    Author

    Hi @arthurschreiber ,
    Thanks for your quick response. I have already updated the tedious version in my post.

    Version Details:
    "tedious": "2.0.1",
    "mssql": "^4.0.4",

    We had a look at active work thread to SQL server and network connectivity by raising it to Microsoft support. They mentioned all good from SQLServer end. The work thread percentage also less. I have tried to retry with custom logic. But no luck. After getting socket error, it is continuously throwing for all the subsequent requests. Any suggestion or recommendation would really help us. Currently, we are using the connection pool to connect to the Database.

    Thanks,
    Praveen.

  3. regevbr commented on Oct 1, 2018

    @regevbr

    Same here, it suddnely gets to that state and gets stuck...

  4. TimTilroy commented on Dec 18, 2018

    @TimTilroy

    We are also seeing the same connection errors happening on our system.

    We are connecting a connectionPool on service start.
    new App.SQL.ConnectionPool

    At a random moment a request fails with:

    ConnectionError: Failed to connect to databaseserver:1433 - Could not connect (sequence)

    Service restarts automatically but keeps throwing these errors for about 20 minutes. After that service restarts and it can connect again.

    We are seeing this with multiple node processes on this server but they are not going down simultaneously (sometimes they do), which is kinda weird. We believe the cause is in the NodeJS app and not in the mssql DB.

    We are using these versions:

    mssql: 4.3.0 (latest)
    tedious version included in this package is 2.7.1.

  5. MarioPerezEsteso commented on Feb 12, 2019

    @MarioPerezEsteso

    Hi. We are also experiencing this issue.

    These are our versions:
    mssql: 4.3.0
    which uses tedious version: 2.7.1

    ConnectionError: Failed to connect to xxxxxx.database.windows.net:1433 in 15000ms
    at Connection.tedious.once.err (...node_modules\mssql\lib\tedious.js:239:17)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at Connection.connectTimeout (...node_modules\mssql\node_modules\tedious\lib\connection.js:944:12)
    at Timeout._onTimeout (...node_modules\mssql\node_modules\tedious\lib\connection.js:913:16)
    at ontimeout (timers.js:482:11)
    at tryOnTimeout (timers.js:317:5)
    at Timer.listOnTimeout (timers.js:277:5)
    
  6. sergeos commented on Mar 7, 2019

    @sergeos
  7. raygift commented on Apr 10, 2019

    @raygift

    got same error when using code copied from microsoft official sample: https://www.microsoft.com/en-us/sql-server/developer-get-started/node/windows/step/2.html

    Finally, i solved this error by enable TCP/IP protocol in "Sql Server Configuration Manager"->"SQL Server Network Configuration"->" Protocols for "->"TCP/IP"

    C:\Users\zpc\Desktop\SqlServerSample>node connect.js { ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence) at ConnectionError (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\errors.js:13:12) at Connection.socketError (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connection.js:1183:28) at Connector.execute (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connection.js:1022:21) at SequentialConnectionStrategy.connect (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connector.js:69:7) at Socket.onError (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connector.js:84:12) at Socket.emit (events.js:189:13) at emitErrorNT (internal/streams/destroy.js:82:8) at emitErrorAndCloseNT (internal/streams/destroy.js:50:3) at process._tickCallback (internal/process/next_tick.js:63:19) message: 'Failed to connect to localhost:1433 - Could not connect (sequence)', code: 'ESOCKET' }

  8. JoyEnergiser commented on Jul 13, 2019

    @JoyEnergiser

    got same error when using code copied from microsoft official sample: https://www.microsoft.com/en-us/sql-server/developer-get-started/node/windows/step/2.html

    Finally, i solved this error by enable TCP/IP protocol in "Sql Server Configuration Manager"->"SQL Server Network Configuration"->" Protocols for "->"TCP/IP"

    C:\Users\zpc\Desktop\SqlServerSample>node connect.js { ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence) at ConnectionError (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\errors.js:13:12) at Connection.socketError (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connection.js:1183:28) at Connector.execute (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connection.js:1022:21) at SequentialConnectionStrategy.connect (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connector.js:69:7) at Socket.onError (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connector.js:84:12) at Socket.emit (events.js:189:13) at emitErrorNT (internal/streams/destroy.js:82:8) at emitErrorAndCloseNT (internal/streams/destroy.js:50:3) at process._tickCallback (internal/process/next_tick.js:63:19) message: 'Failed to connect to localhost:1433 - Could not connect (sequence)', code: 'ESOCKET' }

    Thank you! I was having this exact issue ;)

  9. TimTilroy commented on Jul 29, 2019

    @TimTilroy

    Since we are still experiencing this issue with almost all of our nodejs services we did some extra tests.
    The comment above with the TCP/IP fix was already enabled on our sql server.

    We created a new nodejs service that is doing absolutely nothing. It is not called by another service, just runs in PM2.
    It will open a new connectionpool when it starts and just sits there.

    We got 3 errors in 5 days, happening randomly:
    "ConnectionError: Failed to connect to sql.tilroy.internal:1433 - Could not connect (sequence)".

    This is the code:

    `
    const config = {
    user: Config.sql.sqluser,
    password: Config.sql.sqlpassword,
    server: Config.sql.sqlhost,
    database: Config.sql.sqldatabase,
    port: Config.sql.sqlport,
    connectionTimeout: 3000,
    requestTimeout: 3000,
    pool: {
    max: 100,
    min: 1, //don't close all the connections.
    idleTimeoutMillis: 1000,
    evictionRunIntervalMillis: 1500000
    }
    };

    const sqlConnection = new SQL.ConnectionPool(config, function (err) {
        Log.debug("got sql connection");
    	if(err){
    		Log.error(err);
    		process.exit(1);
    	}
    });
    
    sqlConnection.on('error', err => {
        Log.error(`Connection err : ${err}`);
        process.exit(1);
    });
    

    `

    We upgraded our SQL server but that didn't help. Updated all our packages but still got these errors.
    Anything else we can try? We'd rather not throw out tedious/mssql as we use it in quite a lot of services.

  10. IanChokS commented on Aug 7, 2019

    @IanChokS
    Member

    @TimTilroy are you using tedious to handle your connection pool or the tedious-connection-pool? If you're using the tedious project, maybe you can try the tedious-connection-pool one and see if that helps.

  11. TimTilroy commented on Aug 8, 2019

    @TimTilroy

    Hi IanChokS,

    We are not using the tedious-connection-pool package, but the default connectionPool of the mssql package (its using tedious I believe).

    However we have updated the mssql package to version 5.1 now and it seems stable, we've got no more connection errors on our test service. One of the changes is the following:

    Moved pool library from node-pool to tarn.js

    So maybe that fixed our problem.

    We also set the min pool connections to 0, don't know if that could have any effect.

  12. IanChokS commented on Aug 8, 2019

    @IanChokS
    Member

    @MarioPerezEsteso if you're having trouble connecting to your server, you can try to make sure that TCP IP is enabled and that You have the correct port number in your configuration to check if the server is reachable.

    @regevbr, @Praveen335 If you're using connection pooling, you can try and use the tarn.js library instead of the node-pool like what TimTilroy did to see if it fixes your connection issues. If not, perhaps try upgrading to the latest mssql (v5.1).

  13. TimTilroy commented on Aug 21, 2019

    @TimTilroy

    Update: we have applied the new version of mssql (5.1.0) to all our sql services, updated the min pool connections to 0 and it looks like the connection errors stopped since our deploy.

  14. MohamedAla commented on Nov 5, 2019

    @MohamedAla

    I think the problem is just because you are using " / " instead of " \ " in the name or smthing like that

  15. IanChokS commented on Dec 13, 2019

    @IanChokS
    Member

    @Praveen335 Have you tried upgrading to the new version of mssql (5.1.0) to see if it resolves your issue? If not, feel free to re-open this issue.

  16. 2 remaining items

  17. Nipont commented on Jul 13, 2020

    @Nipont

    got same error when using code copied from microsoft official sample: https://www.microsoft.com/en-us/sql-server/developer-get-started/node/windows/step/2.html

    Finally, i solved this error by enable TCP/IP protocol in "Sql Server Configuration Manager"->"SQL Server Network Configuration"->" Protocols for "->"TCP/IP"
    SQLServerConfigurationManager

    C:\Users\zpc\Desktop\SqlServerSample>node connect.js { ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence) at ConnectionError (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\errors.js:13:12) at Connection.socketError (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connection.js:1183:28) at Connector.execute (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connection.js:1022:21) at SequentialConnectionStrategy.connect (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connector.js:69:7) at Socket.onError (C:\Users\zpc\Desktop\SqlServerSample\node_modules\tedious\lib\connector.js:84:12) at Socket.emit (events.js:189:13) at emitErrorNT (internal/streams/destroy.js:82:8) at emitErrorAndCloseNT (internal/streams/destroy.js:50:3) at process._tickCallback (internal/process/next_tick.js:63:19) message: 'Failed to connect to localhost:1433 - Could not connect (sequence)', code: 'ESOCKET' }

    This one work for me. Thank you for give me the solution.

  18. fedecaccia commented on Oct 20, 2020

    @fedecaccia

    Im also experimenting this ESOCKET connection errors, and the issue is not related with TCP/IP since the app is connected from AWS EC2 to AS RDS and it is working most of the time.

  19. fedecaccia commented on Dec 17, 2020

    @fedecaccia

    FINALLY I solved the error. The script I was using each time I requested a new connection:

    function getConnection () {
        // console.log("config")
        // console.log(config)
        return new Promise(function (resolve, reject) {
            const pool = new sql.ConnectionPool(getConfig())
            // console.log("Getting connection...");
            pool.connect()
            .then((cn) => {
                // console.log("connection got");
                resolve(cn);
            })
            .catch((e) => {
                // console.log("connection not got");
                reject(e);
            });
        })
    }
    

    The script that solved the issue:

    
    function runCommand(cmdText) {
        return new Promise((resolve, reject) => {
            pool2Connect
            .then((pool) => {
                // pool.request() // or: new sql.Request(pool2)
                // .input('input_parameter', sql.Int, 10)
                // .output('output_parameter', sql.VarChar(50))
                // .execute('procedure_name', (err, result) => {
                //     // ... error checks
                //     console.dir(result)
                // })
                pool.request() // or: new sql.Request(pool2)
                .query(cmdText)
                .then((rows) => {
                    return resolve(rows.recordset);
                })
                .catch((e) => {
                    return reject(e);
                });
            })
            .catch((err) => {
                return reject(err);
            });
        });
    }
    

    where pool2Connect needs to be defined initially, when starting the script:

        pool2 = new sql.ConnectionPool(config);
        pool2Connect = pool2.connect();
        pool2.on('error', err => {
            console.log(err);
        });
    

    Hope it helps

  20. Vasanthi0205 commented on Dec 9, 2021

    @Vasanthi0205

    Hi,
    I am connecting to Azure Data Studio server from Node.js using tedious. (not using pool connection of tedious)
    I am facing this ESOCKET error randomly.
    My Version:
    tedious: 12.3.0.

    Please provide me a solution to resolve this

  21. edwinspire commented on Dec 9, 2021

    @edwinspire

    I find myself with the same novelty, I connect without using pool, I do it to about 1000 different servers and of those 5 they present that message, it is not a TCP issue.
    For the servers that do not connect I have an additional observation, using the same application but from Windows if they connect, but when I upload to the production server, a Centos 7, the connection is not made, but only on these few servers.
    What else could you see?

  22. gretchunkim commented on May 2, 2022

    @gretchunkim

    I was getting the exact same error message and confirmed it wasn't TCP/IP issue. Mine turned out to be account user setting/policy issue on SQL. I've switced to another account assigned to SQL and it started to connect.

  23. ramses429 commented on May 6, 2022

    @ramses429

    image
    image
    image
    image

    Use how host your own public ip direction

  24. nickpazfernande commented on May 9, 2022

    @nickpazfernande

    After several days with this problem, I found a blog that said to use the dynamic port. And it worked!!!
    Captura de pantalla 2022-05-09 090840

  25. MuraliPagadalaColibri commented on Sep 1, 2022

    @MuraliPagadalaColibri

    Ttrying to connect to sql server database, giving following below error, please help on this error.

    ConnectionError: Failed to connect to "AWS SQL Server Database" - getaddrinfo ENOTFOUND
    at Connection.socketError (C:\Scraper\CEShop\node_modules\tedious\lib\connection.js:1399:28)
    at C:\Scraper\CEShop\node_modules\tedious\lib\connection.js:1180:14
    at processTicksAndRejections (node:internal/process/task_queues:78:11) {
    code: 'ESOCKET',
    isTransient: undefined
    }

  26. mShan0 commented on Sep 1, 2022

    @mShan0
    Contributor

    Hi @MuraliPagadalaColibri, can you open a new issue? We can help you from there.

  27. MeghaGhotkar13 commented on Dec 7, 2022

    @MeghaGhotkar13

    Since we are still experiencing this issue with almost all of our nodejs services we did some extra tests. The comment above with the TCP/IP fix was already enabled on our sql server.

    We created a new nodejs service that is doing absolutely nothing. It is not called by another service, just runs in PM2. It will open a new connectionpool when it starts and just sits there.

    We got 3 errors in 5 days, happening randomly: "ConnectionError: Failed to connect to sql.tilroy.internal:1433 - Could not connect (sequence)".

    This is the code:

    ` const config = { user: Config.sql.sqluser, password: Config.sql.sqlpassword, server: Config.sql.sqlhost, database: Config.sql.sqldatabase, port: Config.sql.sqlport, connectionTimeout: 3000, requestTimeout: 3000, pool: { max: 100, min: 1, //don't close all the connections. idleTimeoutMillis: 1000, evictionRunIntervalMillis: 1500000 } };

    const sqlConnection = new SQL.ConnectionPool(config, function (err) {
        Log.debug("got sql connection");
    	if(err){
    		Log.error(err);
    		process.exit(1);
    	}
    });
    
    sqlConnection.on('error', err => {
        Log.error(`Connection err : ${err}`);
        process.exit(1);
    });
    

    `

    We upgraded our SQL server but that didn't help. Updated all our packages but still got these errors. Anything else we can try? We'd rather not throw out tedious/mssql as we use it in quite a lot of services.

  28. MeghaGhotkar13 commented on Dec 7, 2022

    @MeghaGhotkar13

    Hey @TimTilroy
    Thanks for sharing it helped me
    Finally, I solved this error by enabling TCP/IP protocol in "SQL Server Configuration Manager"->"SQL Server Network Configuration"->" Protocols for "->" TCP/IP"

  29. ShahriarKh commented on Jul 2, 2023

    @ShahriarKh

    For debugging, I tried to login with Azure Data Studio and got this mesaage:
    image

    All I had to do was adding encrypt: false. This might not be the best solution, but I hope it helps future Googlers.

  30. protoid2 commented on Oct 25, 2023

    @protoid2

    For debugging, I tried to login with Azure Data Studio and got this mesaage: image

    All I had to do was adding encrypt: false. This might not be the best solution, but I hope it helps future Googlers.

    but for me ,thats solve thanks brother..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions