Ruby: SQLite3::BusyException: database is locked:

2019-01-13 01:56发布

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.

15条回答
欢心
2楼-- · 2019-01-13 02:12

I had the same "ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: INSERT INTO "users" ("created_at", "email", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?)" issue. I tried every way around found in Google and I failed. The problem was solved for me when I closed my SQLite Database Browser.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-13 02:14

It is most likely not related to rails code. Using at the same time the console with the sandbox option (rails console --sandbox), makes the problem systematic with SQLite, since the console is basically waiting to quit to rollback everything.

The solution above from @trisweb will not work in this case, but quitting the console will.

查看更多
对你真心纯属浪费
4楼-- · 2019-01-13 02:15

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.

查看更多
来,给爷笑一个
5楼-- · 2019-01-13 02:16

This happens when you make any changes manually directly into the SQlite DB Browser (like delete a row or change the value of any column) and forget to save those changes. Any changes made need to be saved (ctrl + s). If not saved, SQLite locks the Database until u save those changes.

I did the same and my issue got resolved!

查看更多
对你真心纯属浪费
6楼-- · 2019-01-13 02:16

I was using DB Browser for SQLite and rails console simultaneously. Closing the DB Browser for SQLite fixed the issue for me.

查看更多
欢心
7楼-- · 2019-01-13 02:17

Probably you have a Rails console open on another bash, if so you have to close it (ctrl+D).

查看更多
登录 后发表回答