Concurrent insertion of dao with greendao; Database is locked when executing BEGIN EXCLUSIVE

StackOverflow https://stackoverflow.com/questions/15815380

  •  01-04-2022
  •  | 
  •  

Question

We are testing greendao for concurrency issues when there are two different threads within the same process trying to persist at the same time.

We spawn two threads in the same process and are having them insert 100, randomly populated, dao's at the same time.

GreenDao version 1.3.0

Please let me know if we are doing something wrong or if you need more information, thanks!


We keep getting this error:

04-04 14:29:48.528: E/Database(17736): Failure 5 (database is locked) on 0x2d4c98 when executing 'BEGIN EXCLUSIVE;'
04-04 14:29:48.528: W/dalvikvm(17736): threadid=10: thread exiting with uncaught exception (group=0x40018560)
04-04 14:29:48.538: E/AndroidRuntime(17736): FATAL EXCEPTION: Thread-11
04-04 14:29:48.538: E/AndroidRuntime(17736): android.database.sqlite.SQLiteException: database is locked: BEGIN EXCLUSIVE;
04-04 14:29:48.538: E/AndroidRuntime(17736):    at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
04-04 14:29:48.538: E/AndroidRuntime(17736):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1772)
04-04 14:29:48.538: E/AndroidRuntime(17736):    at android.database.sqlite.SQLiteDatabase.beginTransactionWithListener(SQLiteDatabase.java:536)
04-04 14:29:48.538: E/AndroidRuntime(17736):    at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:490)
04-04 14:29:48.538: E/AndroidRuntime(17736):    at de.greenrobot.dao.AbstractDao.executeInsert(AbstractDao.java:344)
04-04 14:29:48.538: E/AndroidRuntime(17736):    at de.greenrobot.dao.AbstractDao.insert(AbstractDao.java:293)

Here is my code that is used for inserting a dao:

getGreenDaoSession().getMyDao().insert(this.myDao);
Was it helpful?

Solution

Finally figured this out.

Each thread was spawning a separate DaoSession. Each thread, since they are in the same process, needs to be using the same DaoSession.

This does concern me though, i am wondering how this will work when we attempt to do this same test but having the threads on different processes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top