Room Persistence 库数据库被锁定(代码 5)

时间:2021-08-02 00:01:02

标签: android kotlin android-room

我一直在努力寻找绕过此问题的解决方案或修复它,但没有成功。

我使用的是最新版本的库 2.4.0-alpha04,因为它提供了 AutoMigration,我发现它非常有用。 简而言之,我跟踪日志的时间很长,似乎只有在从另一个上下文同时访问数据库时才会发生这种情况。

我的应用程序在 MainActivity 和前台服务中使用数据库,这是一个单独的进程。

我一直认为问题出在那里,但我不确定。从日志来看,当服务和应用程序尝试同时写入时会发生问题,服务会不断更新数据库。

companion object {
        private const val DATABASE = "BatteryInfoDatabase"
        private var instance: BatteryInfoDatabase? = null
        fun getDatabase(context: Context?): BatteryInfoDatabase? {
            if (instance == null) {
                synchronized(BatteryInfoDatabase::class.java) {
                    if (instance == null) {
                        instance = Room.databaseBuilder(
                            context!!,
                            BatteryInfoDatabase::class.java, DATABASE)
                            .allowMainThreadQueries()
                            .fallbackToDestructiveMigration()
                            .build()
                    }
                }
            }
            return instance
        }
    }

这是一个日志输出

Fatal Exception: java.lang.RuntimeException
Unable to start activity ComponentInfo{com.paget96.batteryguru/activities.MainActivity}: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode ################################################################# Error Code : 5 (SQLITE_BUSY) Caused By : The database file is locked. (database is locked (code 5): , while compiling: PRAGMA journal_mode) #################################################################
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2695)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1200)

Caused by android.database.sqlite.SQLiteDatabaseLockedException
database is locked (code 5): , while compiling: PRAGMA journal_mode ################################################################# Error Code : 5 (SQLITE_BUSY) Caused By : The database file is locked. (database is locked (code 5): , while compiling: PRAGMA journal_mode) #################################################################
android.database.sqlite.SQLiteConnection.nativePrepareStatement (SQLiteConnection.java)
androidx.room.RoomDatabase.assertNotSuspendingTransaction (RoomDatabase.java:482)
com.paget96.batteryguru.utils.database.batteryinfo.TemperatureHistoryDao_Impl.getTemperatureHistory (TemperatureHistoryDao_Impl.java:106)
com.paget96.batteryguru.utils.database.batteryinfo.BatteryInfoDatabase.getTemperatureHistory (BatteryInfoDatabase.java:76)
com.paget96.batteryguru.fragments.FragmentChargingInfo.temperatureHistoryChart (FragmentChargingInfo.java:247)
com.paget96.batteryguru.fragments.FragmentChargingInfo.initializeViews (FragmentChargingInfo.java:83)
com.paget96.batteryguru.fragments.FragmentChargingInfo.onViewCreated (FragmentChargingInfo.java:218)
androidx.fragment.app.Fragment.performViewCreated (Fragment.java:2985)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1200)

0 个答案:

没有答案