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

Fix connection #29

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

curi1119
Copy link

establish_connection store the connection to ConnectionPool with name of called class name.
So, original code was creating connections for each sharded model.

I fixed that use same ConnectionPool for same DB.
This fix decrease connection count to the DB.

spec/active_record/sharding/model_spec.rb

p ActiveRecord::Base.connection_handler.connection_pool_list.map{|c| c.spec.name }

before fix
["User::SequencerForTestUserSequencer", "Item::ShardForTestUser001", "Item::ShardForTestUser002", "Item::ShardForTestUser003", "primary", "User::ShardForTestUser001", "User::ShardForTestUser002", "User::ShardForTestUser003"]

after fix
["primary", "ShardForTestUser001", "ShardForTestUser002", "ShardForTestUser003", "SequencerForTestUserSequencer"]

…tead of create a connection per sharded model
let(:connection_names) { ["primary", "ShardForTestUser001", "ShardForTestUser002", "ShardForTestUser003", "SequencerForTestUserSequencer"] }

it "Create 1 connection for each DB" do
expect(ActiveRecord::Base.connection_handler.connection_pool_list.map{|c| c.spec.name }).to match_array(connection_names)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
Layout/SpaceInsideBlockBraces: Space between { and | missing.

@@ -35,6 +37,14 @@ def find_from_all_by_name(name)

let(:alice) { model.put! name: "Alice" }

context "ActiveRecord::ConnectionAdapters::ConnectionPool" do
let(:connection_names) { ["primary", "ShardForTestUser001", "ShardForTestUser002", "ShardForTestUser003", "SequencerForTestUserSequencer"] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/WordArray: Use %w or %W for an array of words.

end
a_class.class_eval { abstract_class = true }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint/UselessAssignment: Useless assignment to variable - abstract_class.

@@ -1,3 +1,14 @@
module EstablishConnectionForShard
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.

@hirocaster
Copy link
Owner

Thank you for feedback.
I was able to understand various problems.

I want to think for redesign modules.
Please wait, few days.

@curi1119
Copy link
Author

curi1119 commented Oct 24, 2018

Thank you for review.
I did not have a idea to deal with this issue on AR4.
Now, Rails6 is up comming (early next year?).
I suggest that drop supporting AR4, and support newer Ruby(2.4, 2.5, maybe 2.6), if you don't have any concerns.

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

Successfully merging this pull request may close these issues.

3 participants