Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force to reconnect MySQL in Rails

How to force MySQL reconnect at my will in Rails application? I would like to do this either periodically or on DB exceptions like "MySQL server has gone away".

I found ActiveRecord::Base.remove_connection but as it is written, it should be called for some model, not the whole application.

like image 540
Paul Avatar asked Oct 15 '14 18:10

Paul


2 Answers

It's a huge pain to restart the Rails console when I'm running it via Heroku with a bunch of objects in variables and then lose my database connection.

The following is code I would not consider "good" to put in your actual application but it temporarily gets over the oft encountered Mysql2::Error: closed MySQL connection in a console:

 ActiveRecord::Base.connection.reconnect!
like image 89
Aaron Avatar answered Oct 12 '22 20:10

Aaron


How about using reconnect = true in your database.yml as described here?

like image 40
Roope Hakulinen Avatar answered Oct 12 '22 21:10

Roope Hakulinen