我们在带有数据库清理器的测试套件中随机收到以下错误。我们将数据库清理器与以下两个我认为相关的代码片段结合使用:
错误:
Mysql2::Error: This connection is in use by: #<Thread:0x00000017bbf2f8 sleep>: TRUNCATE TABLE `cr_contacts`;
共享数据库连接(可能的原因)
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection