-
Notifications
You must be signed in to change notification settings - Fork 25
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: define connection holder more thread-safe #32
base: master
Are you sure you want to change the base?
Conversation
AR's establish_connection is not thread-safe. If standby connection is not established beforehand concurrent threads will raise something like: ActiveRecord::ConnectionNotEstablished: No connection pool with id SlaverySlaveConnectionHolder found.
Without the change don't know exactly why, but with sqlite3 the test passes, with "real" db (postgresql in the example) test fails. Couldn't get to the fail scenario with sqlite3. The actual issue is not connected to any specific DB adapter, the issues comes from the fact that |
@@ -20,5 +20,6 @@ Gem::Specification.new do |gem| | |||
gem.add_runtime_dependency 'activerecord', '>= 3.0.0' | |||
|
|||
gem.add_development_dependency 'rspec' | |||
gem.add_development_dependency 'sqlite3' | |||
gem.add_development_dependency 'sqlite3', '~> 1.3', '< 1.4' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
had to update: test environment doesn't see sqlite adapter with sqlite 1.4.
Hi @jughead - I think introducing Mutex for this is overkill, wouldn't it be simpler to add Standby.on_standby { ActiveRecord::Base.connection.execute('SELECT 1') } In |
Hi @kenn! Thank you for quick reply! It happens in Sidekiq actually, so I need to add it there before thread spawning. It helps, but generally everyone need to remember to In your suggested approach, you still need to be sure that the connection is returned back in the main thread. If you insist on the calling Standby beforehand, I think it's better to put this in readme then. |
Just to be clear, mutex is used only once per standby during the initilization. In all other cases, it's not used, as the hash is filled. |
AR's establish_connection is not thread-safe. If standby connection is not established
beforehand concurrent threads will raise something like: