Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB reconnect when connection lost occurred #57

Closed
unikoid opened this issue Mar 3, 2013 · 2 comments
Closed

DB reconnect when connection lost occurred #57

unikoid opened this issue Mar 3, 2013 · 2 comments

Comments

@unikoid
Copy link

unikoid commented Mar 3, 2013

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!

@dresende
Copy link
Owner

dresende commented Mar 4, 2013

db.driver is an ORM Driver abstraction, that defines a couple of common methods. Every native driver (mysql, postgres) has one ORM Driver to make the methods behave the same way.

Pinging the database is in my opinion the best solution, I have to export it so people don't need to go deep in the ORM object to do it. Dropping the connection and reconnecting takes time and resources (client goes offline, free mem, client comes online again, alloc mem, ..). I think pinging and having the connection idle improves startup time (on requests) and has almost no impact in the database (assuming you have only 1 connection per app and not 1 per request).

dresende added a commit that referenced this issue Mar 4, 2013
@unikoid
Copy link
Author

unikoid commented Mar 5, 2013

Thank you for your reply. I have understood your point, so I'll continue to use db.ping().
Also, there's a typo in my first post, I really meant 'As far as I understand, db.driver.db is an instance of node-mysql connection.' So, isn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants