3

How to achieve concurrency in SQLite database? As per documents it is possible using WAL(Write-Ahead Logging).But, I dont know how to implement it.

In my app I want to read data from db on main thread and at the same time a background thread is writing/inserting some data in the same table. On executing read and write query at same time app stops responding until insertion isn't done. Is concurrency possible in SQLite, and how?

5
  • when you openDatabase and get instance of SQLite Db you need to enable WLA i guess developer.android.com/reference/android/database/sqlite/… method: enableWriteAheadLogging Commented Dec 22, 2015 at 6:59
  • "I want to read data from db on main thread":-> Please never do it. Read data from database in background thread and update your UI from main thread. Commented Dec 22, 2015 at 7:10
  • @virendrao I have reffered this. I am using SQLChipher library and there is no method like enableWriteAheadLogging. Thnaks. Commented Dec 22, 2015 at 7:10
  • @PorasBhardwaj above one was for Android sqlite db Commented Dec 22, 2015 at 7:12
  • @virendrao i have tried it. Doesn't Work :( Commented Dec 22, 2015 at 7:14

5 Answers 5

3

To enable write-ahead logging, just call enableWriteAheadLogging() in the onConfigure() callback of your SQLiteOpenHelper-derived class.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to use a singleton object of DataBase object. For this purpose you can use ContentProvider. Which is a better option to achieve concurrency.

Here is link for tutorial on ContentProvider http://www.vogella.com/articles/AndroidSQLite/article.html

You can refer the documentation also http://developer.android.com/guide/topics/providers/content-providers.html

2 Comments

Yes, it can be achieved by ContentProvider. But, As i mentioned how it can be achieved using WAL?
@PorasBhardwaj, I also have no idea about implementing WAL as of now.
1

I completely accept the answer by Nigam Patro, With reference to that I recommend use Greenrobot's GreenDAO with content provider, Singleton approach!!

Why GreenDao ?

  • Maximum performance (probably the fastest ORM for Android).
  • Easy to use APIs.
  • Highly optimized for Android.
  • Minimal memory consumption.
  • Small library size, focus on the essentials, code generator .

More info

Realm also trending ORM with replacement for Sqlite, but I don't use it until they provide support for Content Provider!

Other than that there are many RxJava ported ORM libs available have a look into that once you master some RxJava basics ( toughest thing, time consuming, but it minimizes the coding effort)

I think content provider + with minimized SQLite ORM framowrk + RxJava support will solve the majority of the ORM problems in Android.

Comments

0

"I want to read data from db on main thread": -> Please never do this. It will hangs your UI until database read call executes. Always do it on background thread.

As per my understanding SQLite handles concurrency itself, so we not need to worry about it.

You can use Content Provider to update database by using it's insert() method. Once insertion completes you can notify all register Content Observer about change in database by using following code in insert() method:

getContext().getContentResolver().notifyChange(uri, null);

For creating, registering and unregistering ContentObserver. Please click here.

Please call all Content Provider related operations from background thread or AsyncQueryHandler. You can know more about AsyncQueryHandler here.

Let me know if more details needed or if it helps :)

Comments

-1

Try SQLite triggers to write to log before insert, update or delete

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.