问题描述
我有一个单线程应用程序,该应用程序在本地文件系统上的3个不同文件中使用3个SQLite数据库.
I have a single-threaded application that uses 3 SQLite databases in 3 different files on the local file system.
我创建了一个DbAdapter
帮手类,该类打开了与SQLite数据库文件的连接.在此类中,我有一个创建连接的open
方法和一个释放所有内容的close
方法.
I have created a DbAdapter
helper class that opens the connection to the SQLite database file. In this class I have an open
method that creates the connection, and a close
method that releases everything.
从派生DbAdapter
的类访问这3个数据库.
The 3 databases are accessed from a class that derives DbAdapter
.
在我的程序中,每个数据库访问都是这样的:
In my program every database access look like this:
MyDbAdapter DB = new MyDBAdapter();
int stuff = DB.getStuff(); // queries the database
DB.close();
// now do something with `stuff`
我已将所有对DbAdapter.open
和DbAdapter.close
的呼叫记录到stdout
.每次出现open()
时,附近都会出现close()
.
我还要小心关闭所有Statement
(这也会导致关联的ResultSet
也被关闭).
I have logged to stdout
all calls to DbAdapter.open
and DbAdapter.close
. Everytime there's an open()
, a close()
follows close by.
I also take the care to close all my Statement
s (which will cause the associated ResultSet
s to be closed aswell).
所以我想我的数据库访问是干净的,因为我试图使它们尽可能短,并且在不再需要它们时会立即释放所有资源.
So I guess that my database accesses are clean because I'm trying to get them as short as possible, and I release all the resources as soon as I no longer need them.
但是,我仍然得到一个java.sql.SQLException: database is locked
.
Yet, I'm still getting a java.sql.SQLException: database is locked
.
有什么我做不正确的事情吗?我知道我没有显示任何代码,但是我必须发布很多代码,并且这将无关紧要.我只是问我是否在这里使用了最佳做法,因为我认为我已经这样做了,而且仍然有例外.
Is there anything that I'm not doing properly? I know I haven't shown any code, but I'd have to post a LOT of code and it won't be relevant. I'm just asking if I'm using the best practises here, because I think I do and I'm still getting exceptions.
这是Xerial.org的sqlite-jdbc-3.7.2驱动程序Java 1.6,在Mac OS 10.6 x64上实现的
This is with Java 1.6, Xerial.org's sqlite-jdbc-3.7.2 driver, on Mac OS 10.6 x64
推荐答案
我注意到我有无法杀死的奇怪Java进程.我跑了:
I noticed that I had strange Java processes that I couldn't kill. I ran:
ps aux | grep java | grep -v grep | awk '{print$2}' | xargs sudo kill -9
但是那些进程仍然在这里(具有相同的PID).
But those processes were still here (with same PID).
我重新启动,问题不再出现.这没有任何意义,因为我能够进行很多数据库调用而不会发生任何崩溃,只有一个调用导致了异常.我完全不了解发生了什么,但是我不再遇到这个问题.
I rebooted, and the problem doesn't appear anymore. This doesn't make sense because I was able to make a lot of DB calls without any crash, there was just one single call that caused the exception. I don't understand at ALL what happened, but I'm no longer having this issue.
任何 真实 回答欢迎.
Any real answer welcome.
这篇关于为什么会出现SQLException:数据库已锁定在JDBC SQLite数据库上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!