Thread safety in Rails and ActiveRecord
This is just a quick post to make this point more accessible to the great oracle that is Google.
If you are using multiple threads (or forking a daemon process) which interact with ActiveRecord, add this line to your code before using the DB connection:
ActiveRecord::Base.allow_concurrency = true
If you don’t, you’ll get intermittent errors of the form:
Mysql::Error: Lost connection to MySQL server during query:
There. I’ve done my part.
Fork and edit this post on Github.
Co-Founder of