Search code examples
androidmultithreadingsqlitetransactionsgreendao

SQLite transaction behaviour using greendao


I am using greendao as ORM library in my Android project and have problems understanding the behaviour of transactions.

greendao offers an API runInTx(Runnable r) . My question is, how will two calls of this method from different Threads will be handled?

Thread 1

public void thread1() {
    DaoManager.INSTANCE.getDaoSession().runInTx(new Runnable()
    {
        doQueries();
        doInsertsAndUpdates();
    }
}

Thread 2

public void thread2() {
      DaoManager.INSTANCE.getDaoSession().runInTx(new Runnable()
    {
        doQueries();
        doInsertsAndUpdates();
    }
} 

I have seen, that runInTx is calling SQLiteDatabase.beginTransaction in exclusive mode, so no other thread can read/write to database while this transaction is open. But what i cannot figure out is, when thread1 is doing his job (i.e. doQueries) will the other thread (i.e thread2) blocked, so doQueries and doInsertsAndUpdates of thread2 will not be performed until thread1 has finished the transaction?

Thanks for any help.


Solution

  • SQLiteDatabase.beginTransaction is a blocking call.