Last Updated: February 25, 2016
·
5.765K
· rafaelcgo

Ruby: SQLite3::BusyException: database is locked ?

I have a simple local script that needed threads, one to push things into a queue and one to pull them out and perform the actions (worker), and a Sinatra app.

Sometimes one of the threads, or the Sinatra would hang with a Ruby: SQLite3::BusyException: database is locked, because SQLite3 does not support concurrency.

I'm using ActiveRecord. A simple trick is to add a Timeout option on the establish_connection block of ActiveRecord, now SQLite3 won't trow the exception right away.

ActiveRecord::Base.establish_connection(
  :adapter  => 'sqlite3',
  :database => 'database.sqlite3',
  :host     => 'localhost',
  :timeout  => 1000
)