2017-08-23 160 views
2

當我使用1.0.0-alpha5版本的房間持久性庫時,一切正常。但是,更新到1.0.0-alpha9版本後,我得到這個錯誤:房間持久性庫:嘗試重新打開已關閉的對象

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/package_name/databases/app_db

我該如何解決這個問題?

編輯:

我使用的MVP架構匕首2.我有DatabaseHelper類初始化RoomDatabase:

@Singleton 
public class DatabaseHelper { 

private final AppDatabase appDatabase; 

@Inject 
public DatabaseHelper(@ApplicationContext Context context) { 
    appDatabase = Room.databaseBuilder(context, AppDatabase.class, "app_db").build();   
} 

public List<DifficultyEntity> getAllDifficulties() { 
     return appDatabase.difficultyDao().getAllDifficulties(); 
} 

我RoomDatabase:

@Database(version = 22, entities = { 
     DifficultyEntity.class, // .... 
}) 
@Singleton 
public abstract class AppDatabase extends RoomDatabase { 

    public abstract DifficultyDao difficultyDao(); 
    //.... 
} 

當我打電話getAllDifficulties方法,我收到該錯誤。

這裏的logcat的:

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/package_name/databases/app_db 
     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55) 
     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1783) 
     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1729) 
     at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.execSQL(FrameworkSQLiteDatabase.java:241) 
     at android.arch.persistence.room.RoomOpenHelper.createMasterTableIfNotExists(RoomOpenHelper.java:130) 
     at android.arch.persistence.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:106) 
     at android.arch.persistence.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:99) 
     at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$1.onOpen(FrameworkSQLiteOpenHelper.java:64) 
     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:106) 
     at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:82) 
     at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:191) 
     at uz.iumarov.ieltsspeaking.data.local.database.daos.RecordingDao_Impl.getAllRecordings(RecordingDao_Impl.java:167) 
     at uz.iumarov.ieltsspeaking.data.local.database.DatabaseHelper.getAllRecording(DatabaseHelper.java:361) 
     at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:77) 
     at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:74) 
     at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) 
     at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 
     at rx.Observable.unsafeSubscribe(Observable.java:10151) 
     at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48) 
     at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 
     at rx.Observable.unsafeSubscribe(Observable.java:10151) 
     at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94) 
     at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$1.call(EventLoopsScheduler.java:172) 
     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:818) 

編輯2:

一段時間後,我開始收到不同的錯誤:

java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number. 
     at android.arch.persistence.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:118) 
     at android.arch.persistence.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:99) 
     at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$1.onOpen(FrameworkSQLiteOpenHelper.java:64) 
     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:106) 
     at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:82) 
     at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:191) 
     at uz.iumarov.ieltsspeaking.data.local.database.daos.RecordingDao_Impl.getAllRecordings(RecordingDao_Impl.java:167) 
     at uz.iumarov.ieltsspeaking.data.local.database.DatabaseHelper.getAllRecording(DatabaseHelper.java:361) 
     at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:77) 
     at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:74) 
     at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) 
     at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 
     at rx.Observable.unsafeSubscribe(Observable.java:10151) 
     at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48) 
     at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 
     at rx.Observable.unsafeSubscribe(Observable.java:10151) 
     at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94) 
     at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$1.call(EventLoopsScheduler.java:172) 
     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:818) 

而當我更新的版本號,我獲取遷移錯誤:

java.lang.IllegalStateException: A migration from 22 to 23 is necessary. Please provide a Migration in the builder or call fallbackToDestructiveMigration in the builder in which case Room will re-create all of the tables. 
     at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:81) 
     at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$1.onUpgrade(FrameworkSQLiteOpenHelper.java:49) 
     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256) 
     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) 
     at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:106) 
     at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:82) 
     at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:191) 
     at uz.iumarov.ieltsspeaking.data.local.database.daos.RecordingDao_Impl.getAllRecordings(RecordingDao_Impl.java:167) 
     at uz.iumarov.ieltsspeaking.data.local.database.DatabaseHelper.getAllRecording(DatabaseHelper.java:361) 
     at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:77) 
     at uz.iumarov.ieltsspeaking.ui.main.recordingsFragment.RecordingsPresenter$1.call(RecordingsPresenter.java:74) 
     at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) 
     at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 
     at rx.Observable.unsafeSubscribe(Observable.java:10151) 
     at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48) 
     at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) 
     at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) 
     at rx.Observable.unsafeSubscribe(Observable.java:10151) 
     at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94) 
     at rx.internal.schedulers.EventLoopsScheduler$EventLoopWorker$1.call(EventLoopsScheduler.java:172) 
     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:818) 

請有人幫助我!

解決

我刪除了我的應用程序並重新安裝。現在,一切工作正常與alpha9 verison。

+0

請提供[mcve]。這將包括整個Java堆棧跟蹤以及觸發堆棧跟蹤的代碼。 – CommonsWare

+0

@CommonsWare我更新了我的問題。請看一看。 – Umarov

+0

@CommonsWare我解決了它。感謝您的關注! – Umarov

回答

5

你必須提供Migration.In Live App你不能告訴每個人,請刪除應用程序並重新安裝應用程序。

室內新版本1.0.0-alpha9室增加了對NOT NULL約束的支持。

這將改變Room生成的模式。因爲它改變了模式,所以它也改變了數據庫的identityHash,Room使用它來唯一標識每個數據庫版本。因此,我們需要遷移

要從1.0.0-alpha5遷移到1.0.0-alpha9,請訪問以下文章。非常好的文章從舊版本遷移到1.0.0-alpha9版本。

https://medium.com/@manuelvicnt/android-room-upgrading-alpha-versions-needs-a-migration-with-kotlin-or-nonnull-7a2d140f05b9

相關問題