I have an application which has times of heavy database activity. From time to time I used to get SQLite 'database is locked' errors leading to SQLiteExceptions. Whie I have tried to use:
if((!db.isDbLockedByCurrentThread())&&(!db.isDbLockedByOtherThreads())) {
before using save or insert but this hasn't been successful. So, I have set up the following in a class eg. MyApplication which extends Application like so:
private AtomicBoolean writingToDataBaseFlag = new AtomicBoolean(false);
with an accessor:
public AtomicBoolean getWritingToDataBaseFlag() {
return writingToDataBaseFlag;
}
and any time I want to perform an insert or save I do something like this:
boolean succeeded = false;
while(!succeeded) {
if(application.getWritingToDataBaseFlag().compareAndSet(false, true)) {
if((!db.isDbLockedByCurrentThread())&&(!db.isDbLockedByOtherThreads())) {
if(!activity.isFinishing()) {
set_id(db.insert(SDIGENRE, SDIID, cv));
}
}
application.getWritingToDataBaseFlag().compareAndSet(true, false);
succeeded = true;
} else {
// Wait 100 millis
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
This seems to have worked well for quite a while.
- Is this the right way to go about getting around this issue?
- I have started to see these errors again; is there any advice on how to debug this issue?
Thanks for any help, Julius.