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.
Comments
You certainly have. That’s exactly what I needed to know, and I’m sure you’ve saved me several hours of head-scratching there… thanks very much. You rule!
Thanks, that was exactly what I was looking for.
Leave a comment...