Skip to content

DB reconnect when connection lost occurred #57

Description

@unikoid

Is it possible to catch and handle 'connection lost' event and try to reconnect to db now?
I use mysql driver and I have wait_timeout set in mysql config, so, if connection is inactive for some time, mysql closes it and node-mysql throws an error.
I tryed to catch it with db.driver.on or db.driver.db.on, but no success.
So, I have two questions:

  1. What should I do in this situation? Should I just ping db every n seconds, or there is another solution?
  2. What is happening when I do db.driver.db.on? As far as I understand, db.driver.on is an instance of node-mysql connection. But, when I do:
var mysql = require('mysql');
var conn = mysql.createConnection(my_params);
conn.connect(my_cb);
conn.on('error', function (e) { ... }); // I'm doing this in node REPL, so error handler is attached after db is connected.

everything works well, and, when I do:

var orm = require('orm');
orm.connect(my_params, function (e, db) { 
  db.driver.db.on('error', function (e) { ... }); //There can be db.driver.on also
});

node-mysql just throws unhandled exception and the whole process ends.
Any help would be appreciated!

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions