Search code examples
javaandroidandroid-roomillegalstateexception

Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase


I have this problem with Room, which detected by Yandex Metrica on devices with first install from Market, so it can't be a migration issue. I also tested it on different real and AVD devices and couldn't reproduce it. According to Yandex Metrica logs this issue can be reproduced on android versions from 5.0.1 to 9.

Room version: 1.1.1

I think my issue is similar to this one, but there is no right answer there.

java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:354)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/one.sendy.messenger/databases/Socialmessanger_database
        at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1861)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1840)
        at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.query(FrameworkSQLiteDatabase.java:161)
        at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.query(FrameworkSQLiteDatabase.java:150)
        at android.arch.persistence.room.RoomOpenHelper.hasRoomMasterTable(RoomOpenHelper.java:151)
        at android.arch.persistence.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:123)
        at android.arch.persistence.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:115)
        at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.java:151)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:411)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
        at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
        at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
        at android.arch.persistence.room.RoomDatabase.compileStatement(RoomDatabase.java:244)
        at android.arch.persistence.room.SharedSQLiteStatement.createNewStatement(SharedSQLiteStatement.java:65)
        at android.arch.persistence.room.SharedSQLiteStatement.getStmt(SharedSQLiteStatement.java:72)
        at android.arch.persistence.room.SharedSQLiteStatement.acquire(SharedSQLiteStatement.java:87)
        at com.social.soc.repository.repositorycore.daos.ChatMessageDao_Impl.deleteAll(ChatMessageDao_Impl.java:697)
        at com.social.soc.repository.repositorycore.Repository.clearDatabaseSync(Repository.java:680)
        at com.social.soc.repository.repositorycore.Repository.lambda$deleteAllData$37$Repository(Repository.java:669)
        at com.social.soc.repository.repositorycore.-$$Lambda$Repository$Wk_rZJ-sdwid6vlvxvXj1SzYXr8.run(Unknown Source)
        at beta.framework.android.util.async.AsyncWorker$AsyncTaskExec.doInBackground(AsyncWorker.java:51)
        at beta.framework.android.util.async.AsyncWorker$AsyncTaskExec.doInBackground(AsyncWorker.java:39)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        ... 3 more

Here is what I am doing with db:

chatMessageDao.deleteAll();

And in Dao:

 @Query("DELETE FROM chatmessage")
    void deleteAll();

It is not a migration issue, because: 1) it is first install; 2) it reproduced on 5% devices; 3) and if it was problem with migrations it usually says what it "expected" and what "found".

Also I found this issue on GitHub, that leads me to this one. Looks like this issue actually existed, but they fixed it in Room 2.1.0. But my gradle says that latest version is 1.1.1. And also I'm using support libs and not an androidx.

Update:

I migrated project on androidx, fixed a few conflicts, and everything seem to work fine. But because I can't reproduce this issue, I will report on that question later, if it helps.

Thanks to everyone for the help.


Solution

  • This issue has been solved in a later version of Room. There's no use in trying to solve it on the version you're currently using. Please update because the version you're using is almost 2 years old

    Checkout this link to see which version is the latest: https://developer.android.com/jetpack/androidx/releases/room