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

How is it to more simply using that ActiveRecord::Base.connection.reconnect? #325

Closed
leesanghun6310 opened this issue Jul 19, 2022 · 2 comments

Comments

@leesanghun6310
Copy link

leesanghun6310 commented Jul 19, 2022

I try to improve some api response, by resolve them, I using parallel gem.
And I faced one problem about the way of reconnection.

I used ActiveRecord::Base.connection.reconnect! but It is Not working and it is reason DB is connection away

My db composition is 1 primary and 4 replica

below is sample of my source-code and it is working.
I would like need the way that more simply and flexible portion of reconnect

def search_by_keyword
  resulted_search = Parallel.map(search_keywords, in_processes: 4) do |keyword|
    reconnection
    request_third_party_to_get_search  → heavy logic  
     
  end
  reconnection
end

def reconnection
  ActiveRecord::Base.using(:replica1).connection.reconnect!
  ActiveRecord::Base.using(:replica2).connection.reconnect!
  ActiveRecord::Base.using(:replica3).connection.reconnect!
  ActiveRecord::Base.using(:replica4).connection.reconnect!
  ActiveRecord::Base.using(:master).connection.reconnect!
end
@grosser
Copy link
Owner

grosser commented Jul 19, 2022

not sure how to reconnect all connections if that is what you are asking
please check AR docs or the wiki here and update it if you find a solution

@leesanghun6310
Copy link
Author

Thank you for your answer
I also couldn't find out it.

So I just used the roop

(1..4).each do |no|
  replica_name = "replica{no}".to_sym
  ActiveRecord::Base.using(replica_name).connection.reconnect!
end
ActiveRecord::Base.using(:master).connection.reconnect!

@grosser grosser closed this as completed Jul 19, 2022
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

2 participants