Capybara uses two threads to run client and server. If you just cargo cult | |
the DatabaseCleaner code that is floating around, you can run into | |
https://github.com/brianmario/mysql2/issues/99 because the threads will | |
occasionally use the same connection at the same time. | |
The fix is to use a single connection but protect access to it by having each | |
thread "check out" the connection when they are using it. This is trivial to | |
do with the `connection_pool` gem. Add it to your Gemfile and use the "after" | |
code above. |
This comment has been minimized.
This comment has been minimized.
In your test helper.rb |
This comment has been minimized.
This comment has been minimized.
Hi, thanks for your reply. I am trying to use the code with Cucumber, so I have added it to the env.rb file (I've tried both in the prefork and each_run blocks), and I've added the connection_pool gem to my Gemfile. |
This comment has been minimized.
This comment has been minimized.
@mperham I am also getting:
|
This comment has been minimized.
This comment has been minimized.
Hi , |
This comment has been minimized.
This comment has been minimized.
@mperham, @ntreadway, did you guys figure out the problem? |
This comment has been minimized.
This comment has been minimized.
Thank you for this!! |
This comment has been minimized.
This comment has been minimized.
same issue as @vitobotta and @ntreadway so for now I just use http://github.com/zdennis/activerecord-mysql2-retry-ext |
This comment has been minimized.
This comment has been minimized.
@vitobotta, @ntreadway, @mperham, @driv3r what about this? class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.increment_open_transactions
connection.increment_open_transactions
end
def self.decrement_open_transactions
connection.decrement_open_transactions
end
def self.connection
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end |
This comment has been minimized.
This comment has been minimized.
Encountering the same |
This comment has been minimized.
This comment has been minimized.
@vitobotta, @ntreadway, @mperham, @driv3r, @daliusg you should update connection_pool. In master all this stuff works well! |
This comment has been minimized.
This comment has been minimized.
cheers @mperham this did the trick for me. |
This comment has been minimized.
This comment has been minimized.
@mperham thanks for publishing this and taking the time to sprinkle comments around. Quick solution to a thorny problem (I'm entirely too dependent on transactional fixtures in a huge legacy app). |
This comment has been minimized.
This comment has been minimized.
Finally, my integration tests running reliably :) Thanks Mike. |
This comment has been minimized.
This comment has been minimized.
I am facing below issue is i replace with after.. /home/ubuntu/redzone/spec/spec_helper.rb:53:in `connection': uninitialized constant ActiveRecord::Base::ConnectionPool (NameError)
from /home/ubuntu/redzone/spec/spec_helper.rb:56:in `block in <top (required)>'
from /home/ubuntu/redzone/vendor/bundle/ruby/1.9.1/gems/spork-1.0.0rc4/lib/spork.rb:24:in `prefork'
from /home/ubuntu/redzone/spec/spec_helper.rb:30:in `<top (required)>'
from /home/ubuntu/redzone/spec/controllers/advertiser_campaign_ads_controller_spec.rb:1:in `require' |
This comment has been minimized.
This comment has been minimized.
This worked great for me with cucumber and capybara 1.x However, with capybara 2.x I get the connection errors. |
This comment has been minimized.
This comment has been minimized.
@mperham, it works great for me. Thanks alot. :) |
This comment has been minimized.
This comment has been minimized.
@siva3395 have you added the connection_pool gem to your Gemfile? |
This comment has been minimized.
This comment has been minimized.
Has anyone taken this approach on an app with multiple database connections? I kept getting
It was only when I commented this out that my tests passed again. |
This comment has been minimized.
This comment has been minimized.
We've been running this flawlessly for a long time and since updating the We connect to two different databases, so we have a slightly altered version: class ActiveRecord::Base
mattr_accessor :shared_connections
self.shared_connections = {}
def self.connection
shared_connections[connection_config[:database]] ||= begin
ConnectionPool::Wrapper.new(size: 1) { retrieve_connection }
end
end
end I wonder if you can think of something new in the gem that would cause trouble? Thanks! |
This comment has been minimized.
This comment has been minimized.
@mrsimo this was really helpful. We also connect to multiple databases. Hopefully people google capybara shared database connection with multiple databases and find this like I did. |
This comment has been minimized.
This comment has been minimized.
@mperham -: Hey, we used the earlier code which resulted in the following. PG::UnableToSend: another command is already in progress Then we found your code, but initially it couldn't find a few fields within the thread, but after running a few times our thread seems stuck now as a result. Its so flaky and hard to get what's happening. Any one has any idea, what I can do to fix it? Faced similar issues? |
This comment has been minimized.
This comment has been minimized.
@pratik60 class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
# hack a mutex in the query execution so that we don't
# get competing queries that can timeout and not get cleaned up
module MutexLockedQuerying
@@semaphore = Mutex.new
def async_exec(*)
@@semaphore.synchronize { super }
end
end
PG::Connection.prepend(MutexLockedQuerying) Check out https://gist.github.com/josevalim/470808 for more history on the issue. |
This comment has been minimized.
This comment has been minimized.
I would say avoiding shared state is far from cargo culting. It actually avoids a bunch of hard-to-debug problems quite nicely. |
This comment has been minimized.
This comment has been minimized.
+1 |
This comment has been minimized.
This comment has been minimized.
Just like siva3395 commented on Dec 5, 2013, I am getting this error: NameError: ./spec/spec_helper.rb:23:in `<top (required)>'I do have the gem connection_pool installed. Any ideas on how to solve this? |
This comment has been minimized.
Hi, could you kindly clarify where exactly I should put this code? Thanks! :)