I have got this crash report in firebase crashlytics, titled - SQLiteConnection.java android.database.sqlite.SQLiteConnection.nativeExecute
and the stacktrace lists these error:
Fatal Exception: java.lang.IllegalStateException
The file system on the device is in a bad state. WorkManager cannot access the app's internal data store.
androidx.work.impl.utils.ForceStopRunnable.run (ForceStopRunnable.java:128)
androidx.work.impl.utils.SerialExecutor$Task.run (SerialExecutor.java:91)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1162)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:636)
previous items in the stack
Caused by android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
at android.database.sqlite.SQLiteConnection.nativeExecute(SQLiteConnection.java)
at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:569)
at android.database.sqlite.SQLiteSession.beginTransactionUnchecked(SQLiteSession.java:323)
at android.database.sqlite.SQLiteSession.beginTransaction(SQLiteSession.java:298)
at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:539)
at android.database.sqlite.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:450)
at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.beginTransaction(FrameworkSQLiteDatabase.java:69)
at androidx.work.impl.WorkDatabase$2.onOpen(WorkDatabase.java:163)
at androidx.work.impl.WorkDatabase_Impl$1.onOpen(WorkDatabase_Impl.java:113)
at androidx.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:136)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.java:195)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:349)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:238)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:145)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:106)
at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:622)
at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:399)
at androidx.work.impl.model.SystemIdInfoDao_Impl.getWorkSpecIds(SystemIdInfoDao_Impl.java:120)
at androidx.work.impl.background.systemjob.SystemJobScheduler.reconcileJobs(SystemJobScheduler.java:298)
at androidx.work.impl.utils.ForceStopRunnable.cleanUp(ForceStopRunnable.java:249)
at androidx.work.impl.utils.ForceStopRunnable.forceStopRunnable(ForceStopRunnable.java:215)
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:110)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
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)
SO the SQLiteDatabaseLockedException is happening at SystemIdInfoDao_Impl.getWorkSpecIds(SystemIdInfoDao_Impl.java:120) line, while working with workmanager internal table SystemIdInfo and ForceStopRunnable.forceStopRunnable(ForceStopRunnable.java:215) line, forceStopRunnable method of ForceStopRunnable is calling that.
I wanted to understand more on when these function are called. The description "WorkManager is restarted after an app was force stopped. Alarms and Jobs get cancelled when an application is force-stopped. To reschedule, we create a pending alarm that will not survive force stops." is not clear to me.
Can someone help me understand these and help me fix the crash?
EDIT - As the sqliteLockException is thrown at forceStopRunnable() metjod of forceStopRunnable class and it passes the exception to the InitializationExceptionHandler of the workmanager if workmanager is configured to have one. I have passed an implementation for InitializationExceptionHandler to workmanager, while initializing. This could be possible FIX. Will update here, once confirmed.
The problem is you are not closed previous actions and try to do another actions in the database. you have to close your SQLiteDatabase after perform some actions.By using this code
then start your next CRUD actions