Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: SQLite3::BusyException: database is locked:

Ran into this error message whilst developing tonight: SQLite3::BusyException: database is locked:

I have two models:

  • Podcasts have many Tracks
  • Tracks belong to Podcasts.
  • Podcast files are hosted on mixcloud.

To create a Podcast:

  • user submits a url for a podcast on mixcloud
  • rails app grabs json feed associated with url
  • json is used to set attributes (title, image etc) on the new Podcast object

I'm trying to get my rails app to take advantage of the fact that the json feed also details the names (and artists) of the Tracks that belong to this Podcast.

I thought the following before_validation method would automatically create all associated Tracks whenever we create a new Podcast.

class Podcast < ActiveRecord::Base   attr_accessible :mixcloud_url, :lots, :of, :other, :attrs   has_many :tracks       before_validation :create_tracks   def create_tracks     json = Hashie::Mash.new HTTParty.get(self.json_url)         json.sections.each do |section|       if section.section_type=="track"           Track.create(:name=>section.track.name, :podcast_id=>self.id)       end     end                end end 

How can I get round this? It looks like rails (or sqlite3) doesn't like me creating new instances of an associated model in this way. How else can I do this? I suspect this is as much a rails problem as an sqlite3 one. I can post more code if it's gonna help.

like image 229
stephenmurdoch Avatar asked Aug 22 '11 23:08

stephenmurdoch


People also ask

Does SQLite lock?

SQLite uses POSIX advisory locks to implement locking on Unix. On Windows it uses the LockFile(), LockFileEx(), and UnlockFile() system calls. SQLite assumes that these system calls all work as advertised. If that is not the case, then database corruption can result.


2 Answers

For anyone else encountering this issue with SQLite locking in development when a Rails console is open, try this:

Just run the following:

ActiveRecord::Base.connection.execute("BEGIN TRANSACTION; END;") 

For me anyway, it appears to clear any transaction that the console was holding onto and frees up the database.

This is especially a problem for me when running delayed_job, which seems to fail at closing the transaction quite often.

like image 58
trisweb Avatar answered Oct 09 '22 14:10

trisweb


SQLite is not really supposed to be used for concurrent access which is the issue you are running into here. You can try increasing the timeout in your database.yml file which may be a workaround for you in this case. However, I would recommend you switch to another database that supports multiple connections like MySQL or PgSQL.

like image 37
Devin M Avatar answered Oct 09 '22 14:10

Devin M