question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upgrade Room: attempt to re-open an already-closed object: SQLiteDatabase

See original GitHub issue

I have a database, when I add a migration as below:

abstract class MyDatabase : RoomDatabase() {

    abstract fun dbDao(): DbDao

    companion object {
        @VisibleForTesting
        val MIGRATION_6_7: Migration = object : Migration(6, 7) {
            override fun migrate(database: SupportSQLiteDatabase) {
                MFLogger.d("MyDatabase", "MIGRATION_6_7 start")
                // I create new table and copy data from old table to new table
                ....
                MFLogger.d("MyDatabase", "MIGRATION_6_7 end")
            }
        }
    }
}

In “…”, I create the new table and copy data from old table to new table. It works well, but sometimes I get this crash

PortfolioApp: uncaughtException - ex=java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.misfit.portfolio.debug/databases/589bdfade4b0ba522c4bb4dd_fitness.db
08-24 12:24:31.469 13457-13595/com.misfit.portfolio.debug W/System.err: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.misfit.portfolio.debug/databases/589bdfade4b0ba522c4bb4dd_fitness.db
08-24 12:24:31.469 13457-13457/com.misfit.portfolio.debug D/App-SplashScreenActivity: needToUpdateBLEWhenUpgradeLegacy - isNeedToUpdateBLE=false
08-24 12:24:31.469 13457-13595/com.misfit.portfolio.debug W/System.err:     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
08-24 12:24:31.470 13457-13595/com.misfit.portfolio.debug W/System.err:     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1770)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1716)
        at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.execSQL(FrameworkSQLiteDatabase.java:242)
        at com.portfolio.platform.data.source.local.fitness.FitnessDatabase$Companion$MIGRATION_6_7$1.migrate(FitnessDatabase.kt:45)
        at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:85)
        at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:133)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:259)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        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.query(RoomDatabase.java:233)
        at com.portfolio.platform.data.source.local.fitness.FitnessDao_Impl.getPendingActivitySamples(FitnessDao_Impl.java:358)
        at com.portfolio.platform.data.source.ActivitiesRepository.pushPendingActivities(ActivitiesRepository.kt:181)
        at com.portfolio.platform.usecase.PushPendingDataUtil.start(PushPendingDataUtil.kt:32)
        at com.portfolio.platform.receiver.NetworkChangedReceiver.lambda$pushPendingData$1$NetworkChangedReceiver(NetworkChangedReceiver.java:66)
08-24 12:24:31.470 13457-13595/com.misfit.portfolio.debug W/System.err:     at com.portfolio.platform.receiver.NetworkChangedReceiver$$Lambda$0.run(Unknown Source)

I think this crash happened because the UI is launched and it accesses to database asynchronous with starting migration. Did anyone meet the same problem like this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:24

github_iconTop GitHub Comments

7reactions
svyatoslavpavlovcommented, Oct 9, 2018

I have same problem, use android.arch.persistence.room 1.1.1

3reactions
BartNijland91commented, Nov 1, 2018

@guger Yes it works like that! The problem I’m having is that on app startup I do multiple queries in multiple background threads, so the migration process is ran async with queries. This will fail because room will try to get the database while it’s doing the migration.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Attempt to reopen an already-closed object sqlitedatabase
Attempt to reopen an already -closed object sqlitedatabase · 1. Simply open it by using the line something called myDb.open(). Then do your...
Read more >
attempt to re-open an already-closed object: SQLiteDatabase ...
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: (database path) This issue has happened for ...
Read more >
SQLiteDatabase | Android Developers
The WAL journaling mode is persistent; after being set it stays in effect across multiple database connections and after closing and reopening the...
Read more >
Migrating to Room in the real world — Part 3 - ProAndroidDev
IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: (database path). This error occurred because we were ...
Read more >
Android: Getting 'IllegalStateException: attempt to re-open an ...
Android: Getting 'IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: path/securidDB'.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found