Simon,

In your original message you wrote:

>  >  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.

You definitely do not need a thread to display a progress dialog or a progress bar / wheel. I think using threads for this is the cause of your database issues, and actually you confirmed this too.

This describes progress dialogs:

http://developer.android.com/guide/topics/ui/dialogs.html#ProgressDialog

I'd recommend implementing progress feedback as described there, by not using threads, to see if it makes a difference.

If you use threads to handle a lengthly insert / update operation, take a look at AsyncTask.

Now, with this being said, opening the database from two points in the code (application / service) and holding it for a length of time is not a good idea, as you already discovered.

Consider implementing a ContentProvider for your own use (you can mark it as non-exported, i.e. private to your application).

-- Kostya

20.01.2011 1:47, Simon Dean пишет:
Thanks for this. That explains why there's a lock from a read when I
try to do a write. But why does isLockedByOtherThreads() appear to
fail to work (bearing in mind Im calling it after I've read from the
database in the Service and just before I've ran the "INSERT" command
on the Activity)

Is "synchronized" a good way to get over this problem? And do you have
any links to real world examples? I've had a quick Google but Im
coming up with blanks.

Thanks
Simon

On Jan 19, 9:52 pm, DanH<danhi...@ieee.org>  wrote:
http://www.sqlite.org/lockingv3.html

On Jan 19, 8:00 am, Simon  Dean<sjd...@gmail.com>  wrote:

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


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

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

Reply via email to