当我输入这个查询: 删除邮件中id = 71的所有邮件

SQLite返回以下错误:

SQL error: database is locked

我如何解锁数据库,以便这个查询将工作?


当前回答

删除-journal文件听起来是个糟糕的主意。它允许sqlite在崩溃后将数据库回滚到一致的状态。如果在数据库处于不一致状态时删除它,则会留下一个损坏的数据库。引用sqlite站点的一个页面:

If a crash or power loss does occur and a hot journal is left on the disk, it is essential that the original database file and the hot journal remain on disk with their original names until the database file is opened by another SQLite process and rolled back. [...] We suspect that a common failure mode for SQLite recovery happens like this: A power failure occurs. After power is restored, a well-meaning user or system administrator begins looking around on the disk for damage. They see their database file named "important.data". This file is perhaps familiar to them. But after the crash, there is also a hot journal named "important.data-journal". The user then deletes the hot journal, thinking that they are helping to cleanup the system. We know of no way to prevent this other than user education.

The rollback is supposed to happen automatically the next time the database is opened, but it will fail if the process can't lock the database. As others have said, one possible reason for this is that another process currently has it open. Another possibility is a stale NFS lock, if the database is on an NFS volume. In that case, a workaround is to replace the database file with a fresh copy that isn't locked on the NFS server (mv database.db original.db; cp original.db database.db). Note that the sqlite FAQ recommends caution regarding concurrent access to databases on NFS volumes, because of buggy implementations of NFS file locking.

我无法解释为什么删除一个-journal文件会让你锁定一个数据库,而你以前不能。这是可复制的吗?

顺便说一下,-journal文件的存在并不一定意味着发生了崩溃或有要回滚的更改。Sqlite有几种不同的日志模式,在PERSIST或TRUNCATE模式下,它始终保留-journal文件,并更改内容以指示是否有要回滚的部分事务。

其他回答

一个老问题,有很多答案,这里是我最近遵循的步骤,阅读上面的答案,但在我的情况下,问题是由于cifs资源共享。这个病例以前没有报道过,希望能帮助到一些人。

检查java代码中没有任何连接是打开的。 使用lsof检查是否有其他进程正在使用您的SQLite db文件。 检查正在运行的jvm进程的用户所有者对该文件具有r/w权限。 尝试在连接开口上强制锁定模式 final SQLiteConfig config = new SQLiteConfig(); config.setReadOnly(假); config.setLockingMode (LockingMode.NORMAL); connection = DriverManager。getConnection (url, config.toProperties ());

如果您在NFS共享文件夹上使用SQLite db文件,请检查SQLite faq的这一点,并检查您的挂载配置选项,以确保避免锁定,如下所述:

//myserver /mymount cifs username=*****,password=*****,iocharset=utf8,sec=ntlm,file,nolock,file_mode=0700,dir_mode=0700,uid=0500,gid=0500 0 0

SQLite db文件只是文件,所以第一步是确保它不是只读的。另一件要做的事情是确保在DB打开时没有某种GUI SQLite DB查看器。可以在另一个shell中打开DB,也可以在代码中打开DB。通常情况下,如果不同的线程或应用程序(如SQLite Database Browser)打开了数据库以便写入,则会看到这种情况。

SQLite wiki DatabaseIsLocked页面提供了此错误消息的解释。在某种程度上,它指出争用的来源是内部的(对发出错误的进程而言)。本页没有解释的是SQLite如何决定进程中的某些东西持有锁,以及哪些条件会导致误报。

当您试图从同一个数据库连接同时对数据库执行两项不兼容的操作时,就会出现此错误代码。


在v3中引入的有关文件锁定的变化可能对未来的读者有用,可以在这里找到:SQLite版本3中的文件锁定和并发性

如果文件位于远程文件夹(如共享文件夹)中,则可能引发此错误。我将数据库更改为本地目录,它工作得很好。

我刚才在远程服务器上使用存储在NFS挂载上的SQLite数据库时遇到了这个问题。当我使用的远程shell会话在数据库打开时崩溃后,SQLite无法获得锁。

上面建议的恢复方法对我不起作用(包括先移动然后再复制数据库的想法)。但是在将其复制到非nfs系统后,数据库变得可用,数据似乎没有丢失。