# Active\_record + threads

**URL:** <https://www.ruby-forum.com/t/active-record-threads/155951>\
**Category:** Rails\
**Created:** [December 18, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/active-record-threads/155951 "2008-12-18T13:00:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvalencia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/958977/32.png) [@jvalencia](https://www.ruby-forum.com/u/jvalencia)\
**Post date:** [December 18, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/active-record-threads/155951/1 "2008-12-18T13:00:22Z")

</div>

I’m having problems with active\_record within threads

I have some code that works alone, but if I insert it into a thread  
with:

threads = []  
for x in 1…5 do  
threads \<\< Thread.new do  
team = Team.find(2)  
end  
end

threads.each do |t|  
t.join  
end

That code fails with:

/usr/local/lib/site\_ruby/1.8/active\_record/connection\_adapters/abstract\_adapter.rb:147:in  
`log': Mysql::Error: Lost connection to MySQL server during query: SELECT * FROM`teams`WHERE (`teams`.`id`= 2) (ActiveRecord::StatementInvalid) from simm.rb:311:in`join’  
from simm.rb:311  
from simm.rb:310:in `each’  
from simm.rb:310

Seems the connection goes away inside threads, isn’t it?  
It works without threads…
