Forum: Ruby on Rails active_record + threads

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F683443fa2a955c021ae50753ab43721?d=identicon&s=25 Javier 12 (jvalencia)
on 2008-12-18 13:00
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...
This topic is locked and can not be replied to.