简体   繁体   English

从 ROOM 数据库恢复数据后,部分数据丢失

[英]Some data is lost after recovering data from the ROOM database

I backed up the database by copying the db file, and then recovered the database by overwriting the db file.我通过复制db文件来备份数据库,然后通过覆盖db文件来恢复数据库。 After recovery, the data of one of the tables was lost.恢复后,其中一张表的数据丢失了。 This problem is occasional.这个问题是偶然的。

The recovery process is as follows:恢复过程如下:

  1. Before starting to recover the database, close the currently running database and delete the corresponding database files, including shm and wal files在开始恢复数据库前,先关闭当前运行的数据库,并删除相应的数据库文件,包括shm和wal文件
public void closeDb() {
        if (db != null) {
            if (db.isOpen()) {
                db.getOpenHelper().close();
            }
            db = null;
        }
    }
  1. Delete the currently running database files, including shm and wal files, and copy the backup db files (only db files) to the database directory.删除当前运行的数据库文件,包括shm和wal文件,将备份的db文件(仅限db文件)复制到数据库目录下。 This step uses the Java File API. It has been confirmed that the file operation result is correct本步骤使用Java文件API,已确认文件操作结果正确

  2. Reopen the database using the method of initializing the database.使用初始化数据库的方法重新打开数据库。

public static DBManager init() {
        if (db == null) {
            db = Room.databaseBuilder(application,
                    MyDb.class, dbPath).allowMainThreadQueries().addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6).build();
        }
        return db;
    }

There are no errors in the whole execution process.整个执行过程没有错误。 However, the data of a table in the database cannot be queried.但是查询不到数据库中某个表的数据。 After I try to delete the shm and wal files and restart the app, the data can be queried.我尝试删除shm和wal文件,重启app后,可以查询到数据了。

Seen from the problem phenomenon, it seems to be the problem of shm and wal files, but I can't find a solution.从问题现象看,好像是shm和wal文件的问题,但是找不到解决办法。 I tried to recover the db file, re-open the database, delete the shm and wal files, and then open the database again.我尝试恢复db文件,重新打开数据库,删除shm和wal文件,然后再次打开数据库。 There is still a problem that the data of a table cannot be queried.还有一个问题就是无法查询到一张表的数据。

Has anyone encountered similar problems?有没有人遇到过类似的问题? Please help me.请帮我。 Thank you谢谢

I don't have any ideas to solve this problem我没有任何想法来解决这个问题

Seen from the problem phenomenon, it seems to be the problem of shm and wal files, but I can't find a solution.从问题现象看,好像是shm和wal文件的问题,但是找不到解决办法。

I believe that your issue is that you are not backing up the wal and shm files if they exist and are not empty.我认为您的问题是您没有备份 wal 和 shm 文件(如果它们存在且不为空)。 Such a backup would be an incomplete copy.这样的备份将是一个不完整的副本。

The wal file contains part of the database until it is fully checkpointed. wal 文件包含数据库的一部分,直到它被完全检查点。 If you do not cater for backing up a wal file that has any data, then you will lose data at best you may even corrupt the database.如果您不满足于备份包含任何数据的 wal 文件,那么您充其量只会丢失数据,甚至可能会损坏数据库。

You should always either copy the wal and shm file as well as the database if they exist or fully checkpoint, in which case the wal and shm files will be empty or deleted (should be the latter).您应该始终复制 wal 和 shm 文件以及数据库(如果它们存在)或完全检查点,在这种情况下 wal 和 shm 文件将为空或删除(应该是后者)。

The easiest way to fully checkpoint the database is to close it and then backup the file (still best to be safe and see if the wal and shm file exists and back them up if there size is not 0 bytes).完全检查数据库的最简单方法是关闭它然后备份文件(仍然最好是安全并查看 wal 和 shm 文件是否存在并在大小不为 0 字节时备份它们)。

You may wish to refer to https://www.sqlite.org/lang_vacuum.html不妨参考https://www.sqlite.org/lang_vacuum.html

The restore should restore all 3 files if the 3 files were backed up (hence closing the database prior to backup is the way to go).如果备份了 3 个文件,还原应该还原所有 3 个文件(因此在备份之前关闭数据库是可行的方法)。 The restore should also delete the wal and shm just in case they exist (shouldn't as you are closing the database before the restore).还原还应该删除 wal 和 shm 以防它们存在(不应该因为您在还原之前关闭数据库)。

Another backup option would be to to use VACUUM INTO , which I believe would be a wal/shm free copy of the file as the wal data is considered part of the database.另一个备份选项是使用VACUUM INTO ,我认为这将是文件的 wal/shm 免费副本,因为 wal 数据被视为数据库的一部分。

As for recovering you cannot, as there is no way to know what was in the wal file (the shm file isn't so important as it's a wal file for the wal file).至于恢复你不能,因为没有办法知道 wal 文件中的内容(shm 文件并不重要,因为它是 wal 文件的 wal 文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM