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:20

actually for me, I found killing rails help to fix this problem.

use "ps aux | grep rails" to find out ongoing rails process id. then use

"kill -9 [rails-pid]"

to kill processes.

Then it will work

查看更多
Viruses.
3楼-- · 2019-01-13 02:21

try restarting the server or closing any running rails console, worked for me

查看更多
【Aperson】
4楼-- · 2019-01-13 02:21

Make sure you don't have 2 guards or several consoles running. If you want make sure desperately see the "No Name's" answer above.

You can also try increasing pool:

for example: change test section in your config/database.yml as below

test:
    adapter: sqlite3
    database: db/test.sqlite3
    pool: 50
    timeout: 5000
查看更多
登录 后发表回答