pg client.on('error' does not detect network disruption












0















My application connects to a database and listens for events, so it can execute the events. If the connection with the database drops, then it's supposed to be able to detect it with consumer.db.on('error'



Relevant documentation:




client.on('error', (err: Error) => void) => void



When the client is in the process of connecting, dispatching a query, or disconnecting it will catch and foward errors from the PostgreSQL server to the respective client.connect client.query or client.end callback/promise; however, the client maintains a long-lived connection to the PostgreSQL back-end and due to network partitions, back-end crashes, fail-overs, etc the client can (and over a long enough time period will) eventually be disconnected while it is idle. To handle this you may want to attach an error listener to a client to catch errors. Here's a contrived example:




const client = new pg.Client()
client.connect()

client.on('error', (err) => {
console.error('something bad has happened!', err.stack)
})

// walk over to server, unplug network cable

// process output: 'something bad has happened!' followed by stacktrace :P


Here is my code, trying to do the same thing:



var pg = require('pg');

// get the connection from the connection pool
pg.connect('pg://some:user@some.server', function(err, db){

// throw error
if(err) {
// handle the error - reconnect and log
}

// set database connection for global use
consumer.db = db;

// when notification received
consumer.db.on('notification', function(event){
console.log(event);
// do something with the event
});

// Detect network disruption and other errors
consumer.db.on('error', function(event){
console.log("Error!")
// handle the error - reconnect and log
});
});


I tested this functionality by turning off the wifi for 5 minutes. Sure enough, the connection dropped, but no error event was created, and nothing got logged to the console. I also tried shutting down the remote database with the same results.



How can I get an error when the connection drops?










share|improve this question



























    0















    My application connects to a database and listens for events, so it can execute the events. If the connection with the database drops, then it's supposed to be able to detect it with consumer.db.on('error'



    Relevant documentation:




    client.on('error', (err: Error) => void) => void



    When the client is in the process of connecting, dispatching a query, or disconnecting it will catch and foward errors from the PostgreSQL server to the respective client.connect client.query or client.end callback/promise; however, the client maintains a long-lived connection to the PostgreSQL back-end and due to network partitions, back-end crashes, fail-overs, etc the client can (and over a long enough time period will) eventually be disconnected while it is idle. To handle this you may want to attach an error listener to a client to catch errors. Here's a contrived example:




    const client = new pg.Client()
    client.connect()

    client.on('error', (err) => {
    console.error('something bad has happened!', err.stack)
    })

    // walk over to server, unplug network cable

    // process output: 'something bad has happened!' followed by stacktrace :P


    Here is my code, trying to do the same thing:



    var pg = require('pg');

    // get the connection from the connection pool
    pg.connect('pg://some:user@some.server', function(err, db){

    // throw error
    if(err) {
    // handle the error - reconnect and log
    }

    // set database connection for global use
    consumer.db = db;

    // when notification received
    consumer.db.on('notification', function(event){
    console.log(event);
    // do something with the event
    });

    // Detect network disruption and other errors
    consumer.db.on('error', function(event){
    console.log("Error!")
    // handle the error - reconnect and log
    });
    });


    I tested this functionality by turning off the wifi for 5 minutes. Sure enough, the connection dropped, but no error event was created, and nothing got logged to the console. I also tried shutting down the remote database with the same results.



    How can I get an error when the connection drops?










    share|improve this question

























      0












      0








      0








      My application connects to a database and listens for events, so it can execute the events. If the connection with the database drops, then it's supposed to be able to detect it with consumer.db.on('error'



      Relevant documentation:




      client.on('error', (err: Error) => void) => void



      When the client is in the process of connecting, dispatching a query, or disconnecting it will catch and foward errors from the PostgreSQL server to the respective client.connect client.query or client.end callback/promise; however, the client maintains a long-lived connection to the PostgreSQL back-end and due to network partitions, back-end crashes, fail-overs, etc the client can (and over a long enough time period will) eventually be disconnected while it is idle. To handle this you may want to attach an error listener to a client to catch errors. Here's a contrived example:




      const client = new pg.Client()
      client.connect()

      client.on('error', (err) => {
      console.error('something bad has happened!', err.stack)
      })

      // walk over to server, unplug network cable

      // process output: 'something bad has happened!' followed by stacktrace :P


      Here is my code, trying to do the same thing:



      var pg = require('pg');

      // get the connection from the connection pool
      pg.connect('pg://some:user@some.server', function(err, db){

      // throw error
      if(err) {
      // handle the error - reconnect and log
      }

      // set database connection for global use
      consumer.db = db;

      // when notification received
      consumer.db.on('notification', function(event){
      console.log(event);
      // do something with the event
      });

      // Detect network disruption and other errors
      consumer.db.on('error', function(event){
      console.log("Error!")
      // handle the error - reconnect and log
      });
      });


      I tested this functionality by turning off the wifi for 5 minutes. Sure enough, the connection dropped, but no error event was created, and nothing got logged to the console. I also tried shutting down the remote database with the same results.



      How can I get an error when the connection drops?










      share|improve this question














      My application connects to a database and listens for events, so it can execute the events. If the connection with the database drops, then it's supposed to be able to detect it with consumer.db.on('error'



      Relevant documentation:




      client.on('error', (err: Error) => void) => void



      When the client is in the process of connecting, dispatching a query, or disconnecting it will catch and foward errors from the PostgreSQL server to the respective client.connect client.query or client.end callback/promise; however, the client maintains a long-lived connection to the PostgreSQL back-end and due to network partitions, back-end crashes, fail-overs, etc the client can (and over a long enough time period will) eventually be disconnected while it is idle. To handle this you may want to attach an error listener to a client to catch errors. Here's a contrived example:




      const client = new pg.Client()
      client.connect()

      client.on('error', (err) => {
      console.error('something bad has happened!', err.stack)
      })

      // walk over to server, unplug network cable

      // process output: 'something bad has happened!' followed by stacktrace :P


      Here is my code, trying to do the same thing:



      var pg = require('pg');

      // get the connection from the connection pool
      pg.connect('pg://some:user@some.server', function(err, db){

      // throw error
      if(err) {
      // handle the error - reconnect and log
      }

      // set database connection for global use
      consumer.db = db;

      // when notification received
      consumer.db.on('notification', function(event){
      console.log(event);
      // do something with the event
      });

      // Detect network disruption and other errors
      consumer.db.on('error', function(event){
      console.log("Error!")
      // handle the error - reconnect and log
      });
      });


      I tested this functionality by turning off the wifi for 5 minutes. Sure enough, the connection dropped, but no error event was created, and nothing got logged to the console. I also tried shutting down the remote database with the same results.



      How can I get an error when the connection drops?







      node.js postgresql node-postgres






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 14:53









      user3207874user3207874

      2516




      2516
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The problem was an old version of pg



          The default pool and pg.connect(...) were removed in v7.0.0: https://github.com/brianc/node-postgres/blob/master/CHANGELOG.md#700



          Also, it looks like client.on('error', ...) didn't support network disruptions at that time.



          Upgrading to ^7.0.0



          My version of pg was 4.5.7.



          After upgrading to the latest version of pg (7.6.1) and replacing



          pg.connect('pg://some:user@some.server', function(err, db){


          with



          const connectionConfig = {
          host: config.database.server,
          port: config.database.port,
          database: config.database.database,
          user: config.database.username,
          password: config.database.password
          }
          // get the connection from the connection pool
          consumer.pool = new pg.Pool(connectionConfig)
          consumer.pool.on('error', function(error) {
          consumer.handleError();
          return;
          });
          consumer.pool.connect(function(err, db){


          The issue was fixed.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322118%2fpg-client-onerror-does-not-detect-network-disruption%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The problem was an old version of pg



            The default pool and pg.connect(...) were removed in v7.0.0: https://github.com/brianc/node-postgres/blob/master/CHANGELOG.md#700



            Also, it looks like client.on('error', ...) didn't support network disruptions at that time.



            Upgrading to ^7.0.0



            My version of pg was 4.5.7.



            After upgrading to the latest version of pg (7.6.1) and replacing



            pg.connect('pg://some:user@some.server', function(err, db){


            with



            const connectionConfig = {
            host: config.database.server,
            port: config.database.port,
            database: config.database.database,
            user: config.database.username,
            password: config.database.password
            }
            // get the connection from the connection pool
            consumer.pool = new pg.Pool(connectionConfig)
            consumer.pool.on('error', function(error) {
            consumer.handleError();
            return;
            });
            consumer.pool.connect(function(err, db){


            The issue was fixed.






            share|improve this answer




























              0














              The problem was an old version of pg



              The default pool and pg.connect(...) were removed in v7.0.0: https://github.com/brianc/node-postgres/blob/master/CHANGELOG.md#700



              Also, it looks like client.on('error', ...) didn't support network disruptions at that time.



              Upgrading to ^7.0.0



              My version of pg was 4.5.7.



              After upgrading to the latest version of pg (7.6.1) and replacing



              pg.connect('pg://some:user@some.server', function(err, db){


              with



              const connectionConfig = {
              host: config.database.server,
              port: config.database.port,
              database: config.database.database,
              user: config.database.username,
              password: config.database.password
              }
              // get the connection from the connection pool
              consumer.pool = new pg.Pool(connectionConfig)
              consumer.pool.on('error', function(error) {
              consumer.handleError();
              return;
              });
              consumer.pool.connect(function(err, db){


              The issue was fixed.






              share|improve this answer


























                0












                0








                0







                The problem was an old version of pg



                The default pool and pg.connect(...) were removed in v7.0.0: https://github.com/brianc/node-postgres/blob/master/CHANGELOG.md#700



                Also, it looks like client.on('error', ...) didn't support network disruptions at that time.



                Upgrading to ^7.0.0



                My version of pg was 4.5.7.



                After upgrading to the latest version of pg (7.6.1) and replacing



                pg.connect('pg://some:user@some.server', function(err, db){


                with



                const connectionConfig = {
                host: config.database.server,
                port: config.database.port,
                database: config.database.database,
                user: config.database.username,
                password: config.database.password
                }
                // get the connection from the connection pool
                consumer.pool = new pg.Pool(connectionConfig)
                consumer.pool.on('error', function(error) {
                consumer.handleError();
                return;
                });
                consumer.pool.connect(function(err, db){


                The issue was fixed.






                share|improve this answer













                The problem was an old version of pg



                The default pool and pg.connect(...) were removed in v7.0.0: https://github.com/brianc/node-postgres/blob/master/CHANGELOG.md#700



                Also, it looks like client.on('error', ...) didn't support network disruptions at that time.



                Upgrading to ^7.0.0



                My version of pg was 4.5.7.



                After upgrading to the latest version of pg (7.6.1) and replacing



                pg.connect('pg://some:user@some.server', function(err, db){


                with



                const connectionConfig = {
                host: config.database.server,
                port: config.database.port,
                database: config.database.database,
                user: config.database.username,
                password: config.database.password
                }
                // get the connection from the connection pool
                consumer.pool = new pg.Pool(connectionConfig)
                consumer.pool.on('error', function(error) {
                consumer.handleError();
                return;
                });
                consumer.pool.connect(function(err, db){


                The issue was fixed.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 10:29









                user3207874user3207874

                2516




                2516
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322118%2fpg-client-onerror-does-not-detect-network-disruption%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python