LoginSignup
5
3

More than 5 years have passed since last update.

mysql2+capybara+poltergeistでrspecを動作させるとThis connection is in use by Threadというエラーが出る時の対策

Posted at

備忘録としてメモします。

問題

表題の通り、mysql2、capybara、poltergeistを組み合わせたrapecのテストで、下記のエラーが発生しました。

This connection is in use by: #<Thread:0x007fd32136efd8 sleep>

対策

github gist :josevalim / shared_connection.rbでmrsimoさんが示した内容です。

spec/support/active_record.rb
class ActiveRecord::Base
  mattr_accessor :shared_connections
  self.shared_connections = {}

  def self.connection
    shared_connections[connection_config[:database]] ||= begin
      retrieve_connection
    end
  end
end

module MutexLockedQuerying
  @@semaphore = Mutex.new

  def query(*)
    @@semaphore.synchronize { super }
  end
end

Mysql2::Client.prepend(MutexLockedQuerying)

自分の環境では、spec/supportに上記のファイルを追加すると、テストが通るようになりました。
以上です。

参考

5
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3