rails has_many through and Multiple databases causing SQLite::BusyException
I have a rails application that is using multiple sqlite3 databases (That part is non negotiable), and I have the following classes
class User < ActiveRecord::Base
establish_connection "users_#{Rails.env}" # use alternate DB
has_many :memberships
has_many :groups, through => :memberships
end
class Group < ActiveRecord::Base
establish_connection "users_#{Rails.env}" # use alternate DB
has_many :memberships
has_many :users, :through => :memberships
end
class Membership < ActiveRecord::Base
belongs_to :group
belongs_to :user
# validations stuff
establish_connection "users_#{Rails.env}" # use alternate DB
end
When I create user with a group I get a the following:
ActiveRecord::StatementInvalid (SQLite3::BusyException: database is locked: INSERT INTO "memberships" ...
However when I remove the "开发者_如何转开发establish_connection" method call, and use the single default database, everything works fine and as expected.
I have tried increasing the timeout in database.yml to be 15 seconds, but the same exception just takes longer to appear.
I've seen this happen too. A few things to try:
- Up the timeout in database.yml to 10-15 seconds. You already did this, but it may help someone else with this problem. If you have a slow hard drive and a lot of test data, maybe 20+
- Either delete and re-create the database (if this is in development that probably shouldn't be a problem), or restore a backup of it. There might be a problem with it as it is currently. Incorrectly shutting down the development server, maybe?
- May be a stretch, but make sure the permissions on the file are correct.
精彩评论