Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Created February 19, 2020 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamSaffron/6f9ffec0162c475152a5176ab9401ee9 to your computer and use it in GitHub Desktop.
Save SamSaffron/6f9ffec0162c475152a5176ab9401ee9 to your computer and use it in GitHub Desktop.
require 'active_record'
config = <<~CONF
development:
adapter: sqlite3
database: ":memory:"
host_names:
- test.localhost
CONF
ActiveRecord::Base.configurations = ActiveRecord::DatabaseConfigurations.new(YAML::load(config))
ActiveRecord::Base.establish_connection(:development)
(0..4).map do |thread_number|
Thread.new do
# Obtain a reference to the schema cache, and log which connection it's using
my_schema_cache = ActiveRecord::Base.connection.schema_cache
initial_conn_id = my_schema_cache.connection.object_id
# Do something else
sleep 1
# Now check the schema cache connection again
final_conn_id = my_schema_cache.connection.object_id
if initial_conn_id != final_conn_id
puts "Thread #{thread_number} switched connection from #{initial_conn_id} to #{final_conn_id}"
end
end
end.each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment