Advertisement
Guest User

Untitled

a guest
May 5th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.36 KB | None | 0 0
  1. var mysql = require('mysql');
  2.  
  3. var connection;
  4. function handleDisconnect() {
  5.   connection  = mysql.createConnection({
  6.     host : "localhost",
  7.     user : "root",
  8.     password : "superR@@T",
  9.     database : "dotabase"
  10.   });                                              // the old one cannot be reused.
  11.  
  12.   connection.connect(function(err) {              // The server is either down
  13.     if(err) {                                     // or restarting (takes a while sometimes).
  14.       console.log('error when connecting to db:', err);
  15.       setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
  16.     }                                     // to avoid a hot loop, and to allow our node script to
  17.   });                                     // process asynchronous requests in the meantime.
  18.                                           // If you're also serving http, display a 503 error.
  19.  connection.on('error', function(err) {
  20.    console.log('db error', err);
  21.    if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
  22.      handleDisconnect();                         // lost due to either server restart, or a
  23.    } else {                                      // connnection idle timeout (the wait_timeout
  24.      throw err;                                  // server variable configures this)
  25.    }
  26.  });
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement