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

verifying connections before first use #545

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions lib/octopus/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,25 @@ def select_value(*args, &block)
# reconnect, but in Rails 3.1 the flag prevents this.
def safe_connection(connection_pool)
connection_pool.automatic_reconnect ||= true
if !connection_pool.connected? && shards[Octopus.master_shard].connection.query_cache_enabled
connection_pool.connection.enable_query_cache!
con = nil
if !connection_pool.connected?
master_con = shards[Octopus.master_shard].connection
if master_con.query_cache_enabled
con ||= connection_pool.connection
con.enable_query_cache!
end
end
con ||= connection_pool.connection
if defined?(RequestStore)
# we need to verify! the connection before use. Rails does this by default but octopus bypasses this mechanism.
# the primary connection works fine but if you use using(x) it doesn't checkout the connection from the pool correctly
# now we verify each pool once per request
if RequestStore.store["octopus.verify_pool_#{connection_pool.object_id}"].blank?
RequestStore.store["octopus.verify_pool_#{connection_pool.object_id}"] = 1
con.verify!
end
end
connection_pool.connection
con
end

def select_connection
Expand Down Expand Up @@ -192,15 +207,15 @@ def send_queries_to_slave_group(method, *args, &block)
def current_model_replicated?
replicated && (current_model.try(:replicated) || fully_replicated?)
end

def initialize_schema_migrations_table
if Octopus.atleast_rails52?
select_connection.transaction { ActiveRecord::SchemaMigration.create_table }
else
else
select_connection.initialize_schema_migrations_table
end
end

def initialize_metadata_table
select_connection.transaction { ActiveRecord::InternalMetadata.create_table }
end
Expand Down