如何在 Room 持久化库中捕获未处理的异常 [英] How to catch unhandled exceptions in the Room persistence library

查看:111
本文介绍了如何在 Room 持久化库中捕获未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我在我的 Android (Java) 项目中使用 Room 持久性库来支持本地数据缓存.Room 在查询或保存数据时在专用线程上运行.

I am using the Room persistence library in my Android (Java) project to support local caching of data. Room operates on dedicated threads when querying or saving data.

问题:

如果在 Room 管理的这些线程之一中抛出异常,那么整个应用程序就会崩溃.这可能发生在数据不一致的情况下,例如数据与当前模式不匹配.这是非常成问题的.我宁愿自己处理此类异常并擦除本地数据库中的所有数据 - 这比让用户使用完全损坏且无法修复的应用程序要好.

If an exception is thrown in one of these threads managed by Room, then the whole application crashes. This can happen in case of data inconsistencies such as data not matching the current schema. This is highly problematic. I would rather prefer to handle such exceptions on my own and wipe all data in the local database - this is better than leaving a user with a completely broken and irrepairable app.

示例异常:

2020-01-22 12:45:08.252 9159-11043/com.xyz E/AndroidRuntime: FATAL EXCEPTION: arch_disk_io_1
    Process: com.xyz, PID: 9159
    java.lang.RuntimeException: Exception while computing database live data.
        at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
        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)
     Caused by: java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "primary" (class com.xyz.model.remotedatasource.sampleApi.entities.ProfileImage), not marked as ignorable (2 known properties: "isPrimary", "url"])
        at [Source: (byte[])":)
    ... -1, column: 402] (through reference chain: com.xyz.model.remotedatasource.sampleApi.entities.Candidate["profileImages"]->java.util.ArrayList[0]->com.xyz.model.remotedatasource.sampleApi.entities.ProfileImage["primary"])
        at com.xyz.model.localdatasource.Converters.deserialize(Converters.java:113)
        at com.xyz.model.localdatasource.Converters.toCandidate(Converters.java:73)
        at com.xyz.model.localdatasource.LocalDao_Impl$4.call(LocalDao_Impl.java:270)
        at com.xyz.model.localdatasource.LocalDao_Impl$4.call(LocalDao_Impl.java:217)
        at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:90)
            ... 3 more

示例数据访问对象 (DAO):

Example data access object (DAO):

public interface LocalDao {
    @Query("SELECT * FROM Match")
    LiveData<List<Match>> getMatches();

    @Insert(onConflict = REPLACE)
    void saveMatches(List<Match> matches);
}

问题

由于 Room 在后台线程中执行许多操作,我希望有一种注册自定义错误处理程序的方法.你知道如何实现这一目标吗?如果没有,如果出现此类异常,您是否有其他建议如何自动擦除数据库?

Since Room performs many operations in background threads I would expect a way to register a custom error handler. Do you know how to achieve this? If not, do you have any other suggestions on how to wipe the database automatically if such exceptions occur?

推荐答案

该目标可以通过注册一个执行具有自定义异常处理程序的自定义线程来实现.

The goal can be achieved by registering a custom thread executing having a custom exception handler.

我想出了以下解决方案:

I came up with the following solution:

public abstract class LocalDatabase extends RoomDatabase {
    private static final String TAG = LocalDatabase.class.getSimpleName();
    private static final Object syncObj = new Object();
    private static LocalDatabase localDatabase;
    private static ConcurrentHashMap<Integer, String> dbToInstanceId = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<Long, String> threadToInstanceId = new ConcurrentHashMap<>();

    public abstract LocalDao getDao();

    public static LocalDatabase getInstance() {
        if (localDatabase == null) {
            localDatabase = buildDb();
        }
        return localDatabase;
    }

    private static LocalDatabase buildDb() {
        // keep track of which thread belongs to which local database
        final String instanceId = UUID.randomUUID().toString();

        // custom thread with an exception handler strategy
        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(runnable -> {
            ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
            Thread thread = defaultThreadFactory.newThread(runnable);
            thread.setUncaughtExceptionHandler(resetDatabaseOnUnhandledException);
            threadToInstanceId.put(thread.getId(), instanceId);
            return thread;
        });

        LocalDatabase localDatabase = Room.databaseBuilder(App.getInstance().getApplicationContext(),
                LocalDatabase.class, "LocalDatabase")
                .fallbackToDestructiveMigration()
                .setQueryExecutor(executor)
                .build();
        dbToInstanceId.put(localDatabase.hashCode(), instanceId);
        return localDatabase;
    }

    static Thread.UncaughtExceptionHandler resetDatabaseOnUnhandledException = new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            Log.e("", "uncaught exception in a LocalDatabase thread, resetting the database", throwable);
            synchronized (syncObj) {
                // there is no active local database to clean up
                if (localDatabase == null) return;

                String instanceIdOfThread = threadToInstanceId.get(thread.getId());
                String instanceIdOfActiveLocalDb = dbToInstanceId.get(localDatabase.hashCode());
                if(instanceIdOfThread == null || !instanceIdOfThread.equals(instanceIdOfActiveLocalDb)) {
                    // the active local database instance is not the one that caused this thread to fail, so leave it as is
                    return;
                }

                localDatabase.tryResetDatabase();
            }
        }
    };

    public void tryResetDatabase() {
        try {
            String dbName = this.getOpenHelper().getDatabaseName();

            // try closing existing connections
            try {
                if(this.getOpenHelper().getWritableDatabase().isOpen()) {
                    this.getOpenHelper().getWritableDatabase().close();
                }
                if(this.getOpenHelper().getReadableDatabase().isOpen()) {
                    this.getOpenHelper().getReadableDatabase().close();
                }
                if (this.isOpen()) {
                    this.close();
                }
                if(this == localDatabase) localDatabase = null;
            } catch (Exception ex) {
                Log.e(TAG, "Could not close LocalDatabase", ex);
            }

            // try deleting database file
            File f = App.getContext().getDatabasePath(dbName);
            if (f.exists()) {
                boolean deleteSucceeded = SQLiteDatabase.deleteDatabase(f);
                if (!deleteSucceeded) {
                    Log.e(TAG, "Could not delete LocalDatabase");
                }
            }

            LocalDatabase tmp = buildDb();
            tmp.query("SELECT * from Match", null);
            tmp.close();

            this.getOpenHelper().getReadableDatabase();
            this.getOpenHelper().getWritableDatabase();
            this.query("SELECT * from Match", null);


        } catch (Exception ex) {
            Log.e("", "Could not reset LocalDatabase", ex);
        }
    }

这篇关于如何在 Room 持久化库中捕获未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆