Thread safety in Rails and ActiveRecord

written about about 1 year ago |
2 comments

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

said about 11 months ago
Posted by author

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!

pbforster
said about 11 months ago
Posted by author

Thanks, that was exactly what I was looking for.

Leave a comment...

formatting help...