node js: check mysql connection before a query

user3776738

I use node js with mysql and want to avoid that the app crash on connection errors.At the moment i use this :

function mysql_handleDisconnect() {
  mysql_connection = mysql.createConnection(mysql_config_obj); // Recreate the connection, since
                                                  // the old one cannot be reused.

  mysql_connection.connect(function(err) {              // The server is either down
    if(err) {                                     // or restarting (takes a while sometimes).
      console.log('error when connecting to db:', err);
      mysql_handleDisconnect(); // We introduce a delay before attempting to reconnect,
    }                                     // to avoid a hot loop, and to allow our node script to
  });                                     // process asynchronous requests in the meantime.
                                          // If you're also serving http, display a 503 error.
  mysql_connection.on('error', function(err) {
    console.log('db error', err);
    if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
      mysql_handleDisconnect();                         // lost due to either server restart, or a
    } else {                                      // connnection idle timeout (the wait_timeout
      throw err;                                  // server variable configures this)
    }
  });
}

 mysql_handleDisconnect(mysql_connection);

so this is blocking because it leads to a hot loop if the connection is closed.my problem is, if i add a setTimeout to reestablish connection just every 2 seconds i could get an fatal error when i do a query with "mysql_connection.query('SELECT ...')".in this case the app crashes.

So my question is,if there's a possibility to check the connection before i do a query?

Pranay Saha

Try using below code in every microservice before doing anything:

 if(connection.state === 'disconnected'){
     return respond(null, { status: 'fail', message: 'server down'});
   }

State of connection to DB could fall in 2 states:

  1. disconnected (when due to DB server down or wrong config use for DB connection is wrong)
  2. authenticated (when DB connection is successfully created to DB server).

So either check state == 'disconnected' or state == 'authenticated'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In node.js mysql query, check if no matching found

From Dev

dropzone.js check for condition before query

From Dev

Node.js mysql queries and net connection

From Java

node.js + mysql connection pooling

From Dev

node.js mySQL connection via a singleton

From Dev

Node.JS + MySQL connection pooling required?

From Dev

Close connection MySQL Node.js

From Dev

Node.js mySQL unable to create a connection

From Dev

Node.js - Check if stream has error before piping response

From Dev

node.js - check if file exists before creating temp file

From Dev

node.js + mysql Query from url

From Dev

Node.js MySQL query execution flow

From Dev

Node JS - mysql - querying inside of a callback of a query

From Dev

Nested query in node js using mysql

From Dev

Node JS MySQL query function not returning result

From Dev

node js mysql query where id = ARRAY?

From Dev

Node.js parsing error with mysql query

From Dev

Remove slashes in node js mysql query

From Dev

Node.js: MySQL query with multiple variables

From Dev

mysql query works in phpmyadmin but not in node.js

From Dev

Node.JS MySQL waiting for query results

From Dev

node.js Count MySQL Query with LIMIT

From Dev

Node.js MySQL Query Error

From Dev

Check mysql connection in sequelize

From Dev

node.js and mysql connection pool does not export

From Dev

How to use recursion in node.js mysql without breaking connection?

From Dev

When should one use connection pooling in node.js mysql?

From Dev

When should one use connection pooling in node.js mysql?

From Dev

node js - Is it need to close the connection when mysql task done?