迁移前房间数据库已关闭

时间:2017-12-23 11:35:03

标签: android sqlite android-sqlite android-room android-architecture-components

我将应用程序中的旧sqlite数据库迁移到了Rooms,它适用于90%的用户。问题是它不是100%。 根据崩溃报告,设备有很多可用空间和RAM,其中大部分是Android 4.4上的Samsung Note 2。此外,我还没有在应用程序的任何位置关闭数据库。

崩溃:

Fatal Exception: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.szyk.myheart/databases/database.db
       at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
       at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1648)
       at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
       at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.execSQL(SourceFile:240)
       at com.szyk.myheart.data.room.Migrations$1.migrate(SourceFile:16)
       at android.arch.persistence.room.RoomOpenHelper.onUpgrade(SourceFile:73)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(SourceFile:118)
       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
       at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(SourceFile:93)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(SourceFile:54)
       at android.arch.persistence.room.RoomDatabase.inTransaction(SourceFile:305)
       at android.arch.persistence.room.InvalidationTracker$1.run(SourceFile:281)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
       at java.lang.Thread.run(Thread.java:838)

在Dagger模块中初始化房间:

@Provides
@ApplicationScope
public static Database provideDatabase(Context context) {
    return Room
            .databaseBuilder(context, Database.class, "database.db")
            .allowMainThreadQueries()
            .addMigrations(Migrations.MIGRATION_25_to_26)
            .addMigrations(Migrations.newDummyMigration(26, 27))
            .build();
}

迁移代码:

public class Migrations {
    public static final Migration MIGRATION_25_to_26 = new Migration(25, 26) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            Timber.d("Migrating to room");
            Timber.d("Migration - creating new tables");
            // Create the new table - it fails on first call to db
            database.execSQL(
                    "CREATE TABLE IF NOT EXISTS users_new (_id INTEGER PRIMARY KEY AUTOINCREMENT, user_name TEXT NOT NULL, user_birth_date INTEGER, is_diabetes INTEGER NOT NULL)");
            ...

            Timber.d("Migration - completed");
        }
    };

    public static Migration newDummyMigration(int from, int to) {
        return new Migration(from, to) {
            @Override
            public void migrate(@NonNull SupportSQLiteDatabase database) {

            }
        };
    }
}

2 个答案:

答案 0 :(得分:3)

问题在于迁移本身。它首次以静默方式(Rx)吞下一个异常崩溃,然后每个下一个调用都在关闭的数据库上执行迁移。

答案 1 :(得分:0)

检查您进行更改的实体。它需要与迁移更改后完全相同。确保您没有忘记向实体添加任何索引或属性。

@Entity(indices = {@Index(value = {"name"})}, tableName = "mytable")
public class MyEntity  implements Serializable {

    @PrimaryKey(autoGenerate = true)
    public int id;
    @NonNull
    @ColumnInfo(collate = ColumnInfo.NOCASE)
    public String name;
  
相关问题