I am currently experiencing concurrency issues after moving from MySQL
to SQLite.
My original program worked fined using MySQL but is now returning
“SQLite3::BusyException” errors. The same result happens whether or
not I enable the allow_concurrency flag.
If I do manually acquire a lock on the SQLite DB the problem would
disapear, but I thought that rails was supposed to handle this
internally (optimistic locking?)
For example the following code (shortened for clarity purposes) caused
no exception with MySQL but would not run with SQLite. Is this a rails
issue or is it simply normal behavior?
----- lib/person_updator.rb -----
class PersonUpdator
def self.start
ActiveRecord::Base.allow_concurrency = true
p1 = Person.find_by_id(1)
p2 = Person.find_by_id(2)
t = Thread.start do
5.times do
p1.name = "Michael"
p1.save
end
end
5.times do
p2.name = "Joe"
p2.save
end
t.join
end
end
C:\test>ruby script\runner ‘PersonUpdator.start’
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/runner.rb:47:
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
connection_adapters/abstract_adapter.rb:150:in log': SQLite3::BusyException: database is locked: UPDATE people (ActiveRecord::StatementInvalid) SET "name" = 'Michael', "lock_version" = 16 WHERE id = 1 AND "lock_version" = 15 from C:/test/lib/person_updator.rb:18:in
join’
from C:/test/lib/person_updator.rb:18:in start' from (eval):1 from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb: 27:in
eval’
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/
runner.rb:47
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in gem_original_require' from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb: 27:in
require’
from script/runner:3
Using the following gems/environment:
rails 2.0.2
activerecord 2.0.2
sqlite3-ruby 1.2.1
sqlite 3.5.1
Windows XP