Skip to content

Instantly share code, notes, and snippets.

@mperham
Created July 4, 2012 19:30

Revisions

  1. mperham revised this gist Jul 4, 2012. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    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.
    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.
    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.
  2. mperham revised this gist Jul 4, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    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.
  3. mperham revised this gist Jul 4, 2012. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions before.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    class ActiveRecord::Base
    mattr_accessor :shared_connection
    @@shared_connection = nil

    def self.connection
    @@shared_connection || retrieve_connection
    end
    end
    ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
  4. mperham created this gist Jul 4, 2012.
    9 changes: 9 additions & 0 deletions after.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    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