jwg*_*g2s 13 mysql rspec ruby-on-rails-3.2
我们在带有数据库清理程序的测试套件中随机出现以下错误.我们正在使用数据库清理器以及我认为相关的以下两个代码片段:
错误:
Mysql2::Error: This connection is in use by: #<Thread:0x00000017bbf2f8 sleep>: TRUNCATE TABLE `cr_contacts`;
Run Code Online (Sandbox Code Playgroud)
共享数据库连接(可能的原因)
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
Run Code Online (Sandbox Code Playgroud)