Sannis / node-mysql-libmysqlclient

Asynchronous MySQL binding for Node.js
http://sannis.github.com/node-mysql-libmysqlclient
Other
229 stars 47 forks source link

Properly handle "MySQL server has gone away" #179

Closed conrad10781 closed 9 years ago

conrad10781 commented 10 years ago

I have a process that is intended to run for a long time. It's often processing several requests a second, but in some cases it can be > 1 day in between a request, which is resulting in a timeout of the connection.

Originally, when the longest time between requests was less than a day, I changed the MySQL wait_timeout value to 86400. I want to avoid making this number any bigger and am looking for a better way to handle this error.

I apologize if this is a duplicate of #163 , but I honestly couldn't understand the response in that issue, or at least how to modify it to resolve my problem.

In summary, is it possible to provide a snippet on how to deal with this issue ( once the problem happens ) or a way maybe to check before doing any queries if we have a valid connection ( before the problem )?

jaxbot commented 10 years ago

Here's a (not very good) solution I've been using.

connection.query(query, function(err,res) {
            if (err) {
                if (err.message.indexOf("connect") !== -1 || err.message.indexOf("gone away") !== -1) {
                    exports.init(global.database);
                    exports.query(query, callback);
conrad10781 commented 10 years ago

@jaxbot , thanks for the response. I'd be curious to see if someone else has another approach. I've implemented a variation of yours in the meantime, that's just doing a SELECT NOW() every X period of time ( rather than on all the individual queries in the application ).

Has anyone explored the possibility of maybe having a disconnected event coming off the main object that could be used to quickly reconnect in one central location?

Sannis commented 9 years ago

Connection is cheap, so you can open and close connection to TD every X requests or using timer.