Database handling with 2 processes
Asked Answered
S

4

24

I have a an application that has 2 parts.

  • A service which creates content.
  • An application that uses the content

Each of these run as different processes. The problem is that both of them share a database. And I frequently get database locked error, both when the service tries to write something and the UI is reading data. Also vice versa. How do go about this?

  • The class used to access DB is a singleton class. But since both UI & the service are 2 different processes, there are 2 singletons I presume. So that doesn't help.
  • Even synchronise won't help I suppose, since again because of 2 different processes.
  • Content Providers maybe an option, but since I use complex queries to dig info, it would be really hard to use that too.

How do I get the two processes share the database. Any cues would be greatly appreciated.

Slovak answered 9/3, 2011 at 15:51 Comment(2)
can u guide me how to use berkeley db.Formalism
Did you find any solution for this problem?Francklyn
C
8

Using a content provider is one option. Another is to take a look at Berkeley DB. The BDB SQL API is SQLite compatible and the BDB lock manager allows multiple threads and/or processes to read/write to the database concurrently.

Counterstamp answered 10/3, 2011 at 20:31 Comment(2)
Sounds interesting. I have been digging some info on this. There are 2 options BDB and BDB SQL, and I am obviously interested in the latter. But looks like we need to build the source and flash the ROM for the SQL version?Slovak
The OTN forums are a good place to start: bit.ly/eIREhr Yes, I believe that you will need to build the source. I've asked someone to follow up on your question on the OTN forum.Counterstamp
D
3

close the connection after each operation

catch the database locked error and try to reconnect after 50ms

or let the service handle the database and the activity ask the service for data

may be there is isDatabaseInUseMethod ?

Delitescence answered 9/3, 2011 at 21:15 Comment(3)
Well, sure there is no isDatabaseInUse or similar in my knowledge. And since Service and Activity are different processes, Procedure calls between them are not easy.Slovak
yes but you can still catch the exception and repeat the query. Make a while statement around it.It will be executed as soon as the other process closes its connection.Make the same thing in both and they will switch the connectionsDelitescence
-1. There are so many things wrong here. This is essentially trying to ignore the problem which things like mutexes were invented to solve. There's no guarantee the one process will let go at any reasonable rate, exceptions are supposed to be... the exception, not the rule, and polling is something to be avoided in general. Please do some research on an actual solution.Faeroese
V
1

You should use a content provider to funnel your database queries through one source. Inside of the content provider you can use any locking mechanisms you would like to ensure you're not having concurrent access. You may also think about using content observers to coordinate service actions with changes to the database.

Vuong answered 9/3, 2011 at 21:52 Comment(2)
I did dig on those lines. Couldn't get one sample app on ContentProviders. Also, can we use it without ContentObservers ?Slovak
Yes, you can use it without contentobservers. Observers just handles notifying on changes, but you have to manually trigger them, so you can just ignore them if yo uwant.Vuong
S
0

The following is a great article on how locking works with SQLite on Android and what things to be aware of: http://kagii.squarespace.com/journal/2010/9/10/android-sqlite-locking.html

I would think you'll find some answers there :)

Shirtwaist answered 9/3, 2011 at 15:55 Comment(1)
My issue is different. I have 2 processes (equivalent to 2 application) trying to access the same database and it locks out each other.Slovak

© 2022 - 2024 — McMap. All rights reserved.