Ruby: SQLite3::BusyException: database is locked:

后端 未结 18 1000
感动是毒
感动是毒 2020-11-28 23:29

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

I have two models:

  • Podcasts have many
相关标签:
18条回答
  • 2020-11-29 00:20

    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.

    0 讨论(0)
  • 2020-11-29 00:25

    For me...the issue I had was that it seemed that the Rails Console I had open for a while was locking up a connection with SQLite.

    So once I exited that console, and restarted my webserver (Thin) it worked perfectly.

    I tried @trisweb's suggestion but it didn't work for me.

    0 讨论(0)
  • 2020-11-29 00:27

    Yes this is an old question and there are many answers on here already. But none of them worked for me, meaning it took me a long time to finally figure out the problem. I found what worked and will share it in case it is what might be causing the problem for you too.

    I was using the SQLITE Browser (its a GUI database browser). I will refer to it as "GUI" here (to prevent confusion with the word browser being your localhost::8000 chrome browser or whatever.
    http://sqlitebrowser.org/

    I was monitoring what was being written to the database and had the GUI open while my rails app ran in my chrome browser. I would refresh the GUI to see if it was adding the data as I expected it to.

    As a matter of debugging I had decided to delete a row from the SQLite GUI so I could see if my app would react appropriately to the row being missing now.

    As it turns out, the SQLite Browser does not actually delete the row (causing confusion on my end as to why my app was acting like the row was still there, even though it was visually missing on the GUI). Anyway after 30 minutes of frustration I closed the SQLite GUI and then got a notice that asked if i wanted to save any changes to the database that I made. I naively clicked "No" and closed the app.

    Apparently what happens is that the GUI then locked the database because there were rows in my database that had been sort of "soft-deleted" without committing to the delete. So the GUI was (for lack of a better term) holding the database in Limbo.

    This explains why a) my app wasnt acting like the row was missing, because it hadn't actually been deleted yet, and B) explains why the database locked up. It was still waiting for me to commit the deletes.

    So to solve the problem, I simply opened up the GUI once again and deleted the same row and then closed the GUI and this time I clicked "Yes" when asking to save changes to the database. It saved the delete and unlocked the database and now my app works!


    I hope this helps someone else that might be having the same issue but was using the SQLite Browser GUI interface. This might be what is locking your database.

    0 讨论(0)
  • 2020-11-29 00:27

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

    0 讨论(0)
  • 2020-11-29 00:28

    Your .sqlite3 file must be saved. Go to DB Browser for SQlite and make ctrl+s or File->Write Changes.

    0 讨论(0)
  • 2020-11-29 00:29

    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.

    0 讨论(0)
提交回复
热议问题