1

I am using SOA architecture for my android application. I am running a service continuously in background (after every 1 minute) that fetches the data from my MYSQL database and synchronizes it with my SQLite database (if there is any change in 2 databases).

Now when the service runs in background it opens the SQLite. At the same time if i am handling (using) the application, it also tries to open Sqlite to fetch the data. In that case i get an error called as : Database is locked at android.database.SQLiteOpenHelper.getReadableDatabase.

Can anybody please suggest me what can be done in this kind of situation?

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
Neha
  • 681
  • 2
  • 9
  • 15
  • According to your suggestion, i have created single instance of DatabaseHelper (which is wrapper class) in this way: private static DatabaseHelper instance; public static synchronized DatabaseHelper getHelper(Context context) { if (instance == null) instance = new DatabaseHelper(context); return instance; } Now whenever i need to use methods of DatabaseHelper i call them as follws: DatabaseHelper db = DatabaseHelper.getHelper(this); BaseObject bObj = db.GetObjectFromDB("GlobalObject1"); – Neha Dec 28 '11 at 08:02
  • Now this GetObjectFromDB is a method from DatabaseHelper.This method reads data from Sqlite DB. For that i need to access the DB and i do it in this fashion: SQLiteDatabase db=this.getReadableDatabase(); As told previously, my application runs many threads. When one of my thread accesses DB another thread also tries to access the DB using the same method GetObjectFromDB. My problem is that getReadableDatabase() always opens a new DB connection. How can i overcome this condtion. Thanks, Neha – Neha Dec 28 '11 at 08:02
  • Hi All, I got the answer to my question. I made a static instance of DataBasehelper (which is a wrapper class) and static instance of SQLiteDatabase : private static DatabaseHelper instance; private static SQLiteDatabase sqlitedb=null; public synchronized SQLiteDatabase getHelper() { if (sqlitedb == null) { sqlitedb = this.getWritableDatabase(); } else if (sqlitedb.isOpen() == false) { sqlitedb = this.getWritableDatabase(); } return sqlitedb; } And now i am calling the DB as follows: SQLiteDatabase db = this.getHelper(); – Neha Dec 29 '11 at 05:20

4 Answers4

2

I prefer you to use ContentProvider here.

Even though its mainly aimed to share among applications, It can be used inside our single app.

If we use content provider, there is no worries of closing and locking of db.

Refer Simple Content Provider for db operations

Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
1

You need to share a single database connection between your app and the background service. This will correctly serialize all accesses to the database.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
0

SQlite is a file based database, granting access to only one application at a time.

  1. You have to either synchronise your service with your application, so the service lets some time for the application to fetch new data (there are many solutions in this one; look for semaphores, monitors and round robin).

  2. You use a second service which will get requests for I/O to the database from your service and application, queue them, perform the requested I/O and return any results to the application or service that submitted the request.

0

That is not true, SQLite 3 has support for multiple connections, and you may keep them both open. Just be sure to use a transaction in the updating thread and close it as soon as possible.

Read about this in http://www.sqlite.org/lockingv3.html

Were you using python, you may e.g. set the timeout param when connecting to the db, which will ensure that the reader thread will wait up to timeout seconds before giving up. Therefore it is important to make the updater as compact as possible, so that the reader guy does not time out.

http://docs.python.org/library/sqlite3.html

Also, I deeply reccomend the following book if you want to learn more. The Definitive Guide to SQLite: http://www.apress.com/9781590596739

Joe M.
  • 609
  • 3
  • 11