致命异常:java.lang.IllegalStateException:尝试重新打开一个已经关闭的对象:SQLiteDatabase

时间:2018-07-11 10:34:26

标签: android android-room

更新Room数据库时出现错误:

 Fatal Exception: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: *****************
   at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
   at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1312)
   at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1291)
   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:266)
   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 *****************.SettingsDao_Impl$3.call(SettingsDao_Impl.java:105)
   at *****************.SettingsDao_Impl$3.call(SettingsDao_Impl.java:102)
   at android.arch.persistence.room.RxRoom$4.apply(RxRoom.java:111)
   at android.arch.persistence.room.RxRoom$4.apply(RxRoom.java:108)
   at io.reactivex.internal.operators.flowable.FlowableMap$MapConditionalSubscriber.tryOnNext(FlowableMap.java:124)
   at io.reactivex.internal.operators.flowable.FlowableObserveOn$ObserveOnConditionalSubscriber.runAsync(FlowableObserveOn.java:637)
   at io.reactivex.internal.operators.flowable.FlowableObserveOn$BaseObserveOnSubscriber.run(FlowableObserveOn.java:176)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
   at java.lang.Thread.run(Thread.java:818)

我做了什么:

  • 我添加了表格: table_d table_e table_f
  • 将数据库版本更新为4
  • 实施的迁移:从1迁移到2,从2迁移到3,从3迁移到4
  • 通过成功的书面迁移测试
  • 将会议室版本从1.1.0更新为1.1.1
  • 添加了保护规则

当前,我在 table_a table_b table_c 时遇到此类错误,但是还有更多表,所以我没有对于所有表都会出现此错误。

该错误也不会发生于所有用户,而仅针对某些用户...

等级:

implementation "android.arch.persistence.room:runtime:1.1.1"
implementation "android.arch.persistence.room:rxjava2:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"

数据库类:

@Database(
    entities = [
            table_a::class,
            table_b::class,
            table_c::class,
            table_d::class,
            table_e::class,
            table_f::class],
    version = 4)
@TypeConverters(Converters::class)
abstract class DataBase : RoomDatabase() {

abstract fun tableADao(): TableADao
// ... and so on for all table daos

companion object {

    @Volatile
    private var INSTANCE: ILDataBase? = null

    fun getILDataBaseInstance(appContext: Context): ILDataBase =
            INSTANCE ?: synchronized(this) {
                INSTANCE ?:initDatabase(appContext).also { INSTANCE = it }
            }

    private fun initDatabase(appContext: Context): DataBase {
        return Room.databaseBuilder(appContext,
                DataBase::class.java, "databasename_db")
                .addMigrations(
                        Migrations.MIGRATION_1_2,
                        Migrations.MIGRATION_2_3,
                        Migrations.MIGRATION_3_4
                )
                .fallbackToDestructiveMigration()
                .build()
        }
    }
}

0 个答案:

没有答案