Android Room数据库发生了几种不同的崩溃-没有想法为什么

时间:2018-10-19 23:09:35

标签: android android-room android-cursor android-livedata

最近我们在使用Android的Room数据库时遇到了一些麻烦。

崩溃仅发生在某些用户上,但是我无法在测试设备或仿真器上重现崩溃。 (这有时也会在Firebase测试设备上发生)

似乎有一些例外情况都与之相关。

所有这些异常都发生在Room生成的Dao的不同行中。

(可以查询,插入,更新或删除)。

CursorWindowAllocationException: Cursor window allocation of 2048 kb failed.

IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: ‘/whatever/database’

SQLiteDiskIOException: disk I/O error (code 266)

SQLiteDiskIOException: disk I/O error (code 778)

SQLiteDatabaseCorruptException: file is encrypted or is not a database (code 26)

SQLiteException: Failed to change locale for db ‘/whatever/database' to 'fr_FR'.

我尝试过的事情:

  • 具有“ detectLeakedClosableObjects()”的严格模式
  • 增加光标的WindowSize
  • 将几乎所有数据库调用都移至顺序执行器(除LiveData的查询实现以外的所有调用)

这些方法都无济于事,并且该错误仍然存​​在。

LiveData的查询是唯一一个查询,它没有卸载默认的数据库执行程序,因为我找不到解决方法。 我怀疑这可能是问题所在,但我不确定。

这是LiveData查询

@Query("SELECT * FROM alarms WHERE deleted =0 ORDER BY isActive DESC, hourOfDay, minuteOfHour")
LiveData<List<Alarm>> getLiveAlarmList();

例外示例:

Fatal Exception: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. 
       at android.database.CursorWindow.(CursorWindow.java:108)
       at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
       at android.database.sqlite.SQLiteCursor.clearOrCreateWindow(SQLiteCursor.java:300)
       at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:138)
       at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:132)
       at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:219)
       at android.database.AbstractCursor.moveToNext(AbstractCursor.java:268)
       at android.arch.persistence.room.InvalidationTracker$1.checkUpdatedTable(SourceFile:358)
       at android.arch.persistence.room.InvalidationTracker$1.run(SourceFile:329)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
       at java.lang.Thread.run(Thread.java:762)

这似乎源自Room的LiveData InvalidationTracker类。

Fatal Exception: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=1 (# cursors opened by this proc=1)
        at android.database.CursorWindow.(CursorWindow.java:108)
        at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:225)
        at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:149)
        at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:143)
        at com.sofaking.moonworshipper.database.room.AlarmDao_Impl.getAllAlarms(SourceFile:458)
        at com.sofaking.moonworshipper.database.AlarmHelper.getAllAlarms(SourceFile:29)
        at com.sofaking.moonworshipper.registration.DefaultAlarmRegistrationImpl.onCancelPendingAlarms(SourceFile:36)
        at com.sofaking.moonworshipper.registration.BaseAlarmRegistrationImpl$start$runnable$1.run(SourceFile:26)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)

虽然这来自查询“所有警报”的服务。请注意,“ AlarmDao_Impl.getAllAlarms”是由Room生成的,确实会在完成后关闭游标。

Fatal Exception: android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 266)
       at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(SQLiteConnection.java)
       at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:1015)
       at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
       at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
       at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:147)
       at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:136)
       at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
       at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
       at android.arch.persistence.room.RoomOpenHelper.checkIdentity(SourceFile:127)
       at android.arch.persistence.room.RoomOpenHelper.onOpen(SourceFile:115)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(SourceFile: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(SourceFile:96)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(SourceFile:54)
       at android.arch.persistence.room.RoomDatabase.query(SourceFile:233)
       at com.sofaking.moonworshipper.database.room.AlarmDao_Impl$4.compute(SourceFile:555)
       at com.sofaking.moonworshipper.database.room.AlarmDao_Impl$4.compute(SourceFile:541)
       at android.arch.lifecycle.ComputableLiveData$2.run(SourceFile:100)
       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)

另一个LiveData查询崩溃。

Fatal Exception: android.database.sqlite.SQLiteDiskIOException: disk I/O error (code 778)
       at android.database.sqlite.SQLiteConnection.nativeExecute(SQLiteConnection.java)
       at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:561)
       at android.database.sqlite.SQLiteSession.endTransactionUnchecked(SQLiteSession.java:437)
       at android.database.sqlite.SQLiteSession.endTransaction(SQLiteSession.java:401)
       at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:528)
       at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.endTransaction(SourceFile:90)
       at android.arch.persistence.room.RoomDatabase.endTransaction(SourceFile:261)
       at com.sofaking.moonworshipper.database.room.AlarmDao_Impl.update(SourceFile:188)
       at com.sofaking.moonworshipper.registration.DefaultAlarmRegistrationImpl.onCalculateAlarms(SourceFile:58)
       at com.sofaking.moonworshipper.registration.BaseAlarmRegistrationImpl$start$runnable$1.run(SourceFile:37)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
       at java.lang.Thread.run(Thread.java:764)

这来自更新对象实例(也由Room处理)

Fatal Exception: android.database.sqlite.SQLiteDatabaseCorruptException: file is encrypted or is not a database (code 26)
       at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(SQLiteConnection.java)
       at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:845)
       at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
       at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
       at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143)
       at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:132)
       at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:219)
       at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:258)
       at android.arch.persistence.room.RoomOpenHelper.hasRoomMasterTable(SourceFile:155)
       at android.arch.persistence.room.RoomOpenHelper.checkIdentity(SourceFile:123)
       at android.arch.persistence.room.RoomOpenHelper.onOpen(SourceFile:115)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(SourceFile: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(SourceFile:96)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(SourceFile:54)
       at android.arch.persistence.room.RoomDatabase.query(SourceFile:233)
       at com.sofaking.moonworshipper.database.room.AlarmDao_Impl$4.compute(SourceFile:321)
       at com.sofaking.moonworshipper.database.room.AlarmDao_Impl$4.compute(SourceFile:307)
       at android.arch.lifecycle.ComputableLiveData$2.run(SourceFile:100)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
       at java.lang.Thread.run(Thread.java:761)

同样,似乎来自LiveData的AlarmDao_Impl $ 4.compute()方法

Fatal Exception: android.database.sqlite.SQLiteException: Failed to change locale for db '/data/user_de/0/com.sofaking.moonworshipper/databases/alarm-database' to 'fr_FR'.
       at android.database.sqlite.SQLiteConnection.setLocaleFromConfiguration(SQLiteConnection.java:402)
       at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:223)
       at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:198)
       at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
       at android.database.sqlite.SQLiteConnectionPool.tryAcquireNonPrimaryConnectionLocked(SQLiteConnectionPool.java:899)
       at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:609)
       at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348)
       at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:586)
       at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
       at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37)
       at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
       at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1353)
       at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1328)
       at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.query(SourceFile:161)
       at android.arch.persistence.room.RoomDatabase.query(SourceFile:233)
       at com.sofaking.moonworshipper.database.room.AlarmDao_Impl.getAllAlarms(SourceFile:438)
       at com.sofaking.moonworshipper.database.AlarmHelper.getAllAlarms(SourceFile:29)
       at com.sofaking.moonworshipper.registration.DefaultAlarmRegistrationImpl.onCancelPendingAlarms(SourceFile:36)
       at com.sofaking.moonworshipper.registration.BaseAlarmRegistrationImpl$start$runnable$1.run(SourceFile:26)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
       at java.lang.Thread.run(Thread.java:761)

与第二个CursorWindowAllocationException相同的区域,但是崩溃不同

1 个答案:

答案 0 :(得分:1)

最后,错误与数据库本身无关。 我有一些图像资产未调整为MDPI,HDPI或XHDPI。它们仅以XXHDPI提供。

要从另一个线程引用此答案: https://stackoverflow.com/a/25861292/8198740

如果您查找fetchJoin()的来源,则会显示:

  

当无法分配CursorWindow时,抛出此异常,   很有可能是由于内存不可用。

显然,由于图像占用了应用程序的大量堆内存,因此光标无法自行分配。

现在我没有那些崩溃了。