Im writing a scheduler/calendar type application.

The user enters some information and that gets stored in a database,
then I have a service which polls the database every sixty seconds
looking for events and spits them out via a notification.

It used to be working pretty well until I started trying to put
progress spinners in which needed to go in a thread to display. (I
still can't get them to spin). At this point I started to get loads of
errors with database locks.

I've done a lot of tidying up, now Im finding a problem between the
service and the insert commands.

The service utilises "getReadableDatabase" and "rawQuery", while the
separate updater function (user controlled) utilises
"getWritableDatabase" and "execSQL" to perform inserts.

I get errors in the log such as:

I/CMT     (  405): Running Scheduler...
D/AndroidRuntime(  405): Shutting down VM
W/dalvikvm(  405): threadid=1: thread exiting with uncaught exception
(group=0x4001d800)
E/AndroidRuntime(  405): FATAL EXCEPTION: main
E/AndroidRuntime(  405): android.database.sqlite.SQLiteException:
error code 5: database is locked
E/AndroidRuntime(  405):        at
android.database.sqlite.SQLiteStatement.native_execute(Native Method)
E/AndroidRuntime(  405):        at
android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:
55)
E/AndroidRuntime(  405):        at
android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:
1598)
E/AndroidRuntime(  405):        at xxx.xxx.xxx.ProgramLocation:999)

I've asked elsewhere and I've looked for solutions such as using
synchronized (Im still trying to wrap my head around that at the
moment), but ideally what I want to know is why is this error being
generated. If I can find why things work instead of what the solutions
are, I'll be a better Android programmer.

Immediately before the insert, I check to see if the database
isLockedByOtherthreads(), but the answer is false. However it seems
when I open/read the database in the Service, Android then fails to be
able to do the Insert in the Activity and errors that the database is
locked.

Why does a read lock the database?
What is generating the lock?
Why does isLockedByOtherThreads() return false and then the Insert
fail?

Perhaps "database is locked" is an erroneous error and refers to it
not being able to generate an exclusive lock for the insert?

Is there a way to find out programmatically what locks there are on a
database?

And finally what are some solutions for this?

I presume I can do reads all over the app as I wish, but the problem
comes when I try to do a write at the same time a read is being
performed.

Perhaps I should disable the service while the update is in progress.

Or I've heard about synchronized but I really don't have any
experience with this and Im looking for some good (android related)
theory about what it is, how it works, why you do it, and how you do
it?

Thanks
Simon

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to