Search code examples
mysqlruby-on-rails-3resque

Ruby - Exception occured: [Mysql2::Error] closed MySQL connection


I have a Rails application now only runs internally, so there's not so much visits right now. And there're two resque worker running hardly to fetch data from the web and inserts into a mysql database, every insert will followed by sleep 10 second.

We run it on a VPS. After like every 5 hours, I will encounter an Exception Exception occured: [Mysql2::Error] closed MySQL connection".

What could be the reason causing the exception? Now the pool size is 5.

Will it help if I raise the pool size and specify reconnect: true in my database.yml?


Solution

  • This is a common issue when using the mysql2 gem version 0.2.11 or below in combination with multithreading. There is a bug on the issue tracker with a details on the problem but in conclusion the advice is to:

    1. Update the gem version you are using to >= 0.2.12
    2. Add the reconnect: true option your db connection config in the database.yml file

    You probably already solved your problem but this might help others who come across this question.