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.

Codevalley
  • 4,593
  • 7
  • 42
  • 56

4 Answers4

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.

dsegleau
  • 1,942
  • 9
  • 13
  • 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? – Codevalley Mar 14 '11 at 10:34
  • The OTN forums are a good place to start: http://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. – dsegleau Mar 15 '11 at 16:05
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 ?

sherif
  • 2,282
  • 19
  • 21
  • 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. – Codevalley Mar 10 '11 at 04:53
  • 3
    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 connections – sherif Mar 10 '11 at 09:21
  • -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. – Bondolin Dec 19 '17 at 23:45
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.

Brian Griffey
  • 4,751
  • 1
  • 25
  • 26
  • I did dig on those lines. Couldn't get one sample app on `ContentProviders`. Also, can we use it without `ContentObservers` ? – Codevalley Mar 10 '11 at 04:52
  • 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. – Brian Griffey Mar 10 '11 at 17:01
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 :)

Julian
  • 20,008
  • 17
  • 77
  • 108
  • 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. – Codevalley Mar 09 '11 at 15:59