Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is it necessary to reconnect each iteration for parallelizing ActiveRecord? #36

Closed
phene opened this issue Oct 6, 2012 · 10 comments

Comments

@phene
Copy link

phene commented Oct 6, 2012

Why is it necessary to run ActiveRecord::Base.connection.reconnect! in order to parallelize the processing of ActiveRecord::Base objects? ActiveRecord::Base is thread safe, so what is this actually fixing? Reconnecting every iteration is extremely wasteful.

@grosser
Copy link
Owner

grosser commented Oct 6, 2012

Try and see if it works, I had lots of issues without this, maybe it's better in AR3,
for forged processes it definetly has to happen.

You can reconnect once per fork, but thats also tricky :)

@phene
Copy link
Author

phene commented Oct 8, 2012

I am also having a lot of issues using the Parallel gem and ActiveRecord, and without a decent explanation from you as to why reconnection for every iteration is necessary, I'm forced to conclude that this gem is unsafe with ActiveRecord.

@grosser
Copy link
Owner

grosser commented Oct 8, 2012

conclude what you want, I don't have any sla's :D

afaik you have to reconnect affter forking, not sure about threading, but I saw a lot of weird issues so far, so I always use forking + reconnect

@grosser grosser closed this as completed Oct 8, 2012
@grosser grosser reopened this Oct 8, 2012
@grosser
Copy link
Owner

grosser commented Oct 8, 2012

whoops wrong button

@grosser
Copy link
Owner

grosser commented Oct 8, 2012

if you can digg down and find any issue let me know, or add it to the readme
even having some reproducable failing test-case would be nice

@phene
Copy link
Author

phene commented Oct 8, 2012

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html seems to indicate that though ActiveRecord is thread safe, you have to manage the connection pool manually. This seems like something that this gem could/should support.

@grosser
Copy link
Owner

grosser commented Oct 8, 2012

This gem should ideally not have to worry about issues of other gems,
if you can fix it without hacking AR and making it opt-in/or a pasteable snippet that would be great :)

@phene
Copy link
Author

phene commented Oct 8, 2012

I found one possible solution using ActiveRecord::Base.connection_pool.with_connection, and I suggest you add it (or a better example) to the readme:

Parallel.each(Message.where(some_condition).all, :in_threads => 8) do |message|
  ActiveRecord::Base.connection_pool.with_connection do
    message.update_attribute(:some_attribute, some_value)
  end
end

@grosser
Copy link
Owner

grosser commented Oct 9, 2012

Added it to the readme, let's see if it works :)

@grosser grosser closed this as completed Oct 9, 2012
@chamnap
Copy link

chamnap commented Feb 22, 2013

It works well for me with mongoid.
Mongoid v2: Mongoid.reconnect!
Mongoid v3: Mongoid.default_session.disconnect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants