android – Database handling with 2 processes-ThrowExceptions

Exception or error:

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.

How to solve:

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.

###

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 ?

###

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.

###

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 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *