Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting SQLException: database is locked on my JDBC SQLite database?

Tags:

java

sqlite

jdbc

I have a single-threaded application that uses 3 SQLite databases in 3 different files on the local file system.

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.

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`

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 Statements (which will cause the associated ResultSets 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.

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.

This is with Java 1.6, Xerial.org's sqlite-jdbc-3.7.2 driver, on Mac OS 10.6 x64

like image 247
Benoit Duffez Avatar asked Nov 03 '22 21:11

Benoit Duffez


2 Answers

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

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.

like image 67
Benoit Duffez Avatar answered Nov 10 '22 17:11

Benoit Duffez


In my case the problem resulted from trying to update the database from Java, while having it open in SQLite Database Browser (which it seems, held a lock on the db).

like image 39
koljaTM Avatar answered Nov 10 '22 18:11

koljaTM